Pretty please: Named arguments
bearophile
bearophileHUGS at lycos.com
Mon Feb 28 12:15:36 PST 2011
Adam Ruppe:
> Size size;
> size.width = 10;
> size.height = 20;
>
> Instead of Size(10, 20).
This adds more clutter compared to argument names and requires more lines...
> Another alternative is to give each element their own struct...
>
> struct Width { int width; alias width this; }
>
> foo(Width(10), Height(20));
Time ago in D there was typedef for this. There are many problems with this solution:
- You can't use this to give the arguments in a different order, or specify only part of the default arguments.
- this can't be used with functions written by other people that don't already use structs for arguments
- This forces you to always use those names at the calling point, while named arguments are usually optional at the calling point.
- you need to define a new struct for each argument you may want to call with a name, this requires code and increases binary size for nothing.
- Maybe there are problems with padding, like if the wrapped argument is an array ubyte[5].
- I think currently alias this is not fully transparent
Bye,
bearophile
More information about the Digitalmars-d
mailing list