0
1
Question: can you design a Builder Pattern API which verifies at compile time that every field is set exactly once?
To be eligible, the size of the compiler output should not be exponential in the number of fields. The best solution will be the shortest implementation for a class of 22 fields.
Example of a possible surface API in java
Given an class for immutable data structure such as this one:
public class Human {
private final String name;
private final Int age;
private final Double money;
public Human(String name, Int age, Double money) {
this.name = name;
this.age = age;
this.money = money;
}
public String getName() { return name; }
public Int getAge() { return age; }
public Double getMoney() { return money; }
}
The goal is to design an API to construct instances this Humain
class with the following requirements, checked at compile time (the code in example is just a possible design, you can come up with anything that is equivalent in term of functionally):
Possible to set parameters in any order:
Human h = HumanBuilder().withName("n").withMoney(0.15).withAge(1).build(); Human g = HumanBuilder().withName("n").withAge(1).withMoney(0.15).build();
Possible to set parameters in different places of the code:
// Given appropriate takesCareOfTheMan & takesCareOfTheMoney methods Human h = takesCareOfTheMan(takesCareOfTheMoney(HumanBuilder())).build(); Human g = takesCareOfTheMoney(takesCareOfTheMan(HumanBuilder())).build();
Impossible to set the same parameter twice:
// This should not compile: // Human h = HumanBuilder().withName("n").withAge(1).withAge(2).withMoney(0.15).build();
Impossible to omit a parameter:
// This should not compile: // Human h = HumanBuilder().withName("n").withMoney(0.15).build();
5
Welcome to Programming Puzzles & Code Golf! Please note that challenges without an objective winning criterion are offtopic on this site. So you should edit this challenge asap otherwise it is gonna get closed very soon. For future challenges I recommend using the sandbox to get feedback from the community first before you post a challenge.
– Denker – 2016-02-16T09:44:15.243possible typo, everything in your constructor is set to
name
– Katenkyo – 2016-02-16T10:00:03.290The question seems to contain the design, so what are you asking for when you ask people to make a design? – Peter Taylor – 2016-02-16T10:29:10.157
1@DenkerAffe, ok added an objective winning criterion! – OlivierBlanvillain – 2016-02-16T10:59:19.523
@Peter Taylor: the design was just an example, you can come up with anything that is equivalent in term of functionally. – OlivierBlanvillain – 2016-02-16T11:00:44.663
I'm still not sure what you're asking for: the question says that you want a language design, but I can't tell whether your self-posted answer is code, pseudocode, or something else. – Peter Taylor – 2016-02-16T12:59:36.917
@PeterTaylor: Sorry I rephrased the question, I'm not looking for a language design but rather for an API (wrongly called domain-specific language before my last edit). The linked wikipedia page has a few examples of what it could look like, except that they do not provide the verifications I'm looking for. – OlivierBlanvillain – 2016-02-16T13:10:30.953
I'm still confused. Let's try to make things a bit more concrete; you used Java as the example, and it's a language I'm familiar with if a bit out of touch, so... If I wrote a Java answer, what would you expect? A source-to-source compiler which takes the .java file for a class and produces a .java file for a builder? A bytecode-to-source generator which uses reflection? A custom classloader? An annotation processor (if they're sufficiently powerful - I never really grokked apt)? Any of the above? Something else? – Peter Taylor – 2016-02-17T08:19:15.863
As stated, I think this problem is impossible to solve in Java. source-to-source compiler would likely lead to an output exponential in the number of fields, reflection cannot be used not at compile time, custom classloader/annotation processor is pretty much a compiler plugin, which might be considered too close to "building a new language" to solve the problem :) – OlivierBlanvillain – 2016-02-17T08:58:07.590