Named arguments via struct initialization in functions

Edwin van Leeuwen via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 8 23:30:31 PST 2016


On Sunday, 6 March 2016 at 17:35:38 UTC, Seb wrote:
> Hey all,
>
> Using structs is not ideal, because one can't require 
> parameters, but this can be solved by having those parameters 
> as normal ones like `sliced(4, {allowDownsize: true})` and it 
> creates some maybe unnecessary overhead.
> However it is probably the easiest solution right now.

In ggplotd I often use named tuples as and "anonymoous" struct:
Tuple!(double,"x")( 0.0 )

I also added a merge function that will return a tuple containing 
merged named tuples:

Tuple!(double,"x",string,"colour")(-1, 
"black").merge(Tuple!(double,"x")(0.0))

returns:
Tuple!(double,"x",string,"colour")(0, "black");

As an aside the merge function also works with structs so you can 
do the following:

struct Point
{
   double x;
   double y;
}

Tuple!(double,"x",string,"colour")(-1, "black").merge(Point(1,2))

returns:
Tuple!(double,"x",double,"y",string,"colour")(1, 2, "black");

It works reasonably well, except that the tuples require a lot of 
typing.



More information about the Digitalmars-d mailing list