Idea: extend typesafe variadic functions
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sat Mar 31 20:26:59 PDT 2007
One of the "cool features you probably didn't know about" with typesafe
variadic functions is that you can make a variadic class parameter which
will be constructed with the parameters to the function, like so:
class A
{
this(int x, float y){}
}
void func(A a...)
{
}
...
func(3, 4.5);
This will implicitly call the constructor for A when you call func() and
will forward those parameters to the class constructor.
What would be really cool is to have this happen for an array of classes or
structs as well:
class A
{
this(int x) {}
this(float x) {}
this(char[] s) {}
}
void func(A[] args...)
{
}
...
func(1, 2.3, "hi");
Right now, it says "cannot implicitly convert 1 to A" or something, but with
this new feature, this would construct three instances of A, one using the
int constructor, one using the float, and one using the string. Of course,
for sanity's sake, it would only try to construct As using single-parameter
constructors.
This would work similarly for structs:
struct S
{
static S opCall(T)(T value)
{
S s;
return s;
}
}
void func(S[] args...)
{
}
...
func(1, 2.3, "hi");
(See if you can figure out why I think this would be cool.. ;) )
More information about the Digitalmars-d
mailing list