Typesafe variadic functions requiring at least one argument

Michael Coulombe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 7 07:09:03 PDT 2016


On Wednesday, 6 July 2016 at 19:50:11 UTC, pineapple wrote:
> I'd like to do something like this but it doesn't seem to be 
> legal -
>
>     void test(int[] ints...) if(ints.length){
>         // stuff
>     }
>
> Not being able to specify this interferes with how I'd like to 
> define my method overloads. What's the best way to achieve what 
> I'm looking for?

Is this close enough?

void test(int first, int[] rest...) {
     auto ints = only(first).chain(rest);
     // stuff
}
void main() {
     //test(); // fails to compile
     test(1);
     test(1,2,3);
}


More information about the Digitalmars-d-learn mailing list