Named constructors
Atila Neves
atila.neves at gmail.com
Mon Jan 14 16:04:26 UTC 2019
On Monday, 14 January 2019 at 14:31:09 UTC, aliak wrote:
> On Thursday, 10 January 2019 at 18:27:59 UTC, Atila Neves wrote:
>>> Look at the Builder pattern, it's pretty much a workaround
>>> for lack of named/keyword arguments.
>>
>> Java doesn't have variadic templates, D does. No builder
>> pattern needed.
>
> Not sure how variadic templates solves the builder pattern? How
> do you deal with a mixture of arguments of similar and
> different types with some being optional and not?
>
> auto a = Builder()
> .accountNumber(202)
> .firstName("Bob")
> .lastName("Who")
> .branch("N-2")
> .interest(20.0)
> .balance(10.0)
> .build()
>
> ??
auto a = Customer(AccountNumber(202),
FirstName("Bob"),
LastName("Who"),
Branch("N-2"),
Interest(20.0),
Balance(10.0));
struct Customer {
this(A...)(auto ref A args)
if(constraintVerifyingNumberAndTypesOfArgs!A) {
this.accountNumber = args[staticIndexOf!(AccountNumber,
A)];
// etc.
}
}
If staticIndexOf is negative, assign a default value.
Again, I want to write a library that does this for me based on a
declaration that looks like a regular function / template.
>> I don't know of any language that does that and has
>> overloading. Walter knows way more about this than I do.
>
> Swift. Named arguments. Overloading. Executed perfectly IMO:
I can't comment. I don't know anything about Swift.
> Unless he means specifically with using named arguments with
> any order. Then yeah nah. Swift no can do. But named arguments
> followed by unnamed and vice versa plus overloading all good.
Probably? I don't really know.
More information about the Digitalmars-d
mailing list