Key arguments

baleog maccarka at yahoo.com
Mon Jul 14 08:32:33 PDT 2008


Please look at the code where i tried to implement something like Keys arguments:
---------
U K(T,U,V...)(T key, U default_value, V args) {
  foreach(i,h; args) 
    {
      try {
      	static if(!is(typeof(args[i]) == void[0u]))
	  static if (is(T == typeof(h.keys[0])) && is(U == typeof(h.values[0])))
	    return h[key];
      } catch (Exception e) {
	;
      }
    }
  return default_value;
}

void f(T...)(int x, T args)
{
  
  int y = K("y",0, args);
  double z = K("z", 10.0, args);
  writefln(y);
  writefln(z);

}

unittest
{
  f(1,["y":2], ["z":3.1]); // 2 3.1
  f(1); // 0 10
}
-----
What do you think about this attempt?



More information about the Digitalmars-d-learn mailing list