Named constructors

JN 666total at wp.pl
Thu Jan 10 14:35:33 UTC 2019


On Thursday, 10 January 2019 at 13:53:40 UTC, Atila Neves wrote:
>
> What's the _real_ difference between 
> `someFunc(firstName="Alice")` and 
> `someFunc(FirstName("Alice"))`? The former saves one character? 
> Is that really worth the cost of language support? I have a 
> feeling a lot of feature requests neglect that each and every 
> one of them has a cost, and there's only enough budget for so 
> many.
>
Isn't there a performance cost added when using structs like 
that? And the real cost is the fact that you have to create all 
those structs meticulously and you can only use that with 
functions that expect such usage. With language support, you 
could have named arguments for every code there exists.

Anyway, the fact that arguments are named are only a bonus, the 
real strength of named arguments comes with optional arguments. 
Look at the Builder pattern, it's pretty much a workaround for 
lack of named/keyword arguments.

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.


More information about the Digitalmars-d mailing list