Named constructors

aliak something at something.com
Mon Jan 14 14:31:09 UTC 2019


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()

??

>
>> Also, named/keyword arguments shouldn't be an issue in regards 
>> to overloading and argument order. Every language I know that 
>> has named/kw arguments allows either only keyword arguments or 
>> keywords arguments at the end of the argument list. You can do:
>>
>> foo(x=10)
>> foo(10, 20, z=30)
>>
>> but you can't do:
>>
>> foo(x=10, 20)
>>
>> because it'd be ambiguous.
>
> 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:

For the case mentioned above:

func f(x: Int, _ y: Int) {}

func main() {
     f(x: 10, 20)
}

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.



More information about the Digitalmars-d mailing list