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

Uranuz neuranuz at gmail.com
Fri Feb 21 09:57:57 PST 2014


In my template functions, classes it's necessary to write 
variadic template parameter list, where elements are options to 
this class/function  changing it's behaviour. But they are 
optional and may be not set at all. These options may be folowed 
by variadic template parameter list that will be processed in 
static foreach loop. What I'm thinking about is creating some 
sort of key-value tuple syntax like in associative arrays. For 
example I could set components or options of my template in 
easy-to-read manner.

I have option types in enum:

enum OptionType { optType1, optType2, optType3 };

Now I write something like this:

// 1.
class Foo( OptionType opt1, int value1, TL.. )
    if( opt1 == OptionType.optType1 )
{}

// 2.
class Foo( OptionType opt1, string value1, TL.. )
    if( opt1 == OptionType.optType2 )
{}

class Foo( OptionType opt1, string value1, OptionType opt2, int 
value2, TL.. )
    if( opt1 == OptionType.optType2 && opt2 == OptionType.optType1 
)
{}

// 4. Order of options can variate but will not change behaviour
class Foo( OptionType opt1, int value1, OptionType opt2, string 
value2, TL.. )
    if( opt1 == OptionType.optType1 &&  )
{}

What I want is to set and parse these options using universal 
template signature

class Foo( Opts... )
{}

and parse it using forech over tuple and `static if`.

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;

But this is not very elegant at my point of sight. Another way 
that could help in this case is to have named parameters. I don't 
remember the correct name of this term. Pascal and Python and 
other languages have it. Syntax is that you can name parameter 
names for parameters that you pass when calling function (or in 
the case above when you are instantiating template). It could be 
realized using `=` character. For example:

alias Foo!( option1 = 100, option2 = "example", option3 = true ) 
MyFoo;

In this case some of these options (template parameters) can be 
missed and this should be able to handle in template body using 
`static if`, `foreach`.

May be all of these trick are sophisticated, hard-to-implement 
or/and break paradigm of D language. But these proposals could 
(maybe) improve readability of programms with huge amount of 
template code.



More information about the Digitalmars-d-learn mailing list