Named constructors
Atila Neves
atila.neves at gmail.com
Thu Jan 10 18:27:59 UTC 2019
On Thursday, 10 January 2019 at 14:35:33 UTC, JN wrote:
> 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?
No.
> And the real cost is the fact that you have to create all those
> structs meticulously
Meticulously? It's a name and the type you were going to write
anyway. You can always call the field `_` to avoid even more
typing.
> and you can only use that with functions that expect such usage.
Yes. But I'm writing the API.
> With language support, you could have named arguments for every
> code there exists.
I'd love to see a DIP that did that and dealt with all the corner
cases. I suspect that it's not as easy as implied here.
> Anyway, the fact that arguments are named are only a bonus, the
> real strength of named arguments comes with optional arguments.
Again, I want a library for this. I know how to write it, I just
haven't yet.
> 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.
> 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.
More information about the Digitalmars-d
mailing list