Idea: extend typesafe variadic functions

Davidl Davidl at 126.com
Sat Mar 31 21:52:34 PDT 2007


since the feature is somewhat vialoating the type safe checking, i think  
restriction
may be required in case of misusing of this feature like:
1. class must have more than 1 ctor, and they must all accept only 1  
argument
2. the func using class A as the variadic arg must only envolve the use of  
A, no other type could be accepted as other args
3. a hint when calling the func might be appreciated?
4. maybe the class ctor must be declare of this type like :
class A
{
	super this (int x)
         {}
}

And in my opinion, it's a great solution to the tango IO calling  
evaluation problem , and i think it's quite useful for mangle to provide  
user a great experience of calling Stdout. Coz i dislike the  
("adkfj").(343).("akdsfj") too much :(
And I hope Kris, Lars, Sean could forgive me of that ;)

> 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