Is there kind of "associative tuple" - like syntax in D?

Justin Whear justin at economicmodeling.com
Fri Feb 21 10:40:26 PST 2014


On Fri, 21 Feb 2014 17:57:57 +0000, Uranuz wrote:

> My idea is that pass these options using syntax like this:
> 
> alias Foo!( [
>    OptionType.optType1 : 100, OptionType.optType2 : "example",
>    "someOtherOpt1": "someOtherOptValue", "someOtherOpt2": true
>   ]  ) MyFoo;
> 
> Using [] brackets it's just for example. It could be changed to {} (Like
> Object in JavaScript). Of course we can create Pair template:
> 
> template P( alias first, alias second )
> {  alias first key;
>     alias second value;
> }
> 
> So we will rewrite previous code like that:
> 
> alias Foo!(
>    P(OptionType.optType1, 100), P(OptionType.optType2, "example"),
>    P("someOtherOpt1", "someOtherOptValue"),
>    P("someOtherOpt2", true)
> ) MyFoo;
> 

You could do something like this:

alias Foo!(
    OptionType.optType1, 100,
    OptionType.optType2, "example,
    ...etc...
) MyFoo;

To see how to parse this sort of list, take a look at the implementations 
of std.typecons.Tuple and std.getopt.


More information about the Digitalmars-d-learn mailing list