On Sun, 25 Mar 2012 22:45:57 +0200, bls <bizp...@orange.fr> wrote:
How do I "call" opDispatch(string name, E...)(E elements) ?
What I want to archive is to call f.i. fm.list with an arbitrary number
of arguments without using
fm.list(1, "abc", 4L, 3.33);
Instead I would prefer
fm.list = (1, "abc", 4L, 3.33);
Is this somehow possible ?
No. However, this is:
fm.list = tuple( 1, "abc", 4L, 3.33 );
In that case, you probably want to use the overload
opDispatch(string name, E...)(Tuple!E elements)
and mark the other:
opDispatch(string name, E...)(E element) if ( !(E.length == 1 &&
isTuple!(E[0])))
tuple, Tuple, and isTuple are found in std.typecons.
import std.variant;
import std.conv;
....
auto fm = FlexMap();
fm.ten = 10;
fm.ten = ["Ten", "Zehn", "Tein"];
fm.list = [20, 10, 2, 2, 44 ] ;
fm.list = "Hello opDispatch";
struct FlexMap
{
Variant[] [string] map;
Variant[] opDispatch(string name)()
{
return map[name];
}
Variant[] opDispatch(string name, E...)(E elements)
{
foreach(element; elements)
map[name] ~= to!Variant(element);
return map[name];
}
Variant[] opDispatch(string name, T) (T t)
{
map[name] ~= to!Variant(t);
return map[name];
}
}
Another question :
How do I bring in :
opDispatch(string name, T) (T[] t)
into FlexMap ?
It's possible to do with template constraints:
import std.traits : isArray;
void opDispatch(string name, T)(T t) if ( isArray!T )
{
// Array specific.
}
void opDispatch(string name, T)(T t) if ( !isArray!T)
{
// Single element.
}
One could also use static if:
Variant[] opDispatch(string name, T)(T t)
{
static if ( is( T U : U[] ) )
{
map[name] ~= to!
}
else
{
// Whatever you want to do differently here.
}
}