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

Philippe Sigaud philippe.sigaud at gmail.com
Fri Feb 21 13:06:04 PST 2014


Justin:
> alias MyFoo = Foo!(opt1(23), opt2("foo"));

That's also my preferred solution. I find it easy to read and it's quite
typesafe (also, it allows for more complex possibilities like n-params
options).

Another solution could be to use an associative array literal for each
option (you have to use one AA for each option, because they can have
different types for keys and values):

alias MyFoo = Foo!([Option1 : 123], [Option2 : "foo"]);

Where each AA literal is passed as a template alias parameter (or an
element in a template tuple parameter):

... Foo(Options...)

I'm typing this on a pad, I cannot show some working code, but they idea is
to check Options elements with

// Option[i] is an AA, for some Key and some Value
is(typeof(Options[i]) == Value[Key], Value, Key)

and then act on the only key/value pair stored in Options. You can get the
only key with Options[i].keys[0] and the associated value with
Options[i][Options[i].keys[0]]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140221/a5421c5e/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list