Typesafe variadic functions requiring at least one argument
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 6 20:52:40 PDT 2016
On Wednesday, July 06, 2016 19:50:11 pineapple via Digitalmars-d-learn 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?
That's not legal, because template constraints only go on templates, and
that function isn't templated. It's using what the spec calls a typesafe
variadic function. If you want to restrict the length at compile time, you'd
need to use a variadic template function (which is what most D code seems to
use when a variadic function is required). e.g.
void test(Args...)(Args args)
if(args.length > 0)
{
// stuff
}
But that function will take arguments of any type and does not give you an
array. If you wanted to restrict it to ints, you'd have to do something like
template isInt(T) { enum isInt = is(std.traits.Unqual!T == int); }
void test(Args...)(Args args)
if(args.length > 0 && std.meta.allSatisfy!(isInt, Args))
{
// stuff
}
If you want to just use a "typesafe" variadic function as in your example,
you're going to need to do a runtime check. e.g.
void test(int[] ints...)
{
assert(!ints.empty);
// stuff
}
However, it looks like you can combine those two types of variadic functions
to get more or less what you want (albeit more verbosely). e.g.
template isInt(T) { enum isInt = is(std.traits.Unqual!T == int); }
void test(Args...)(Args args)
if(args.length > 0 && std.meta.allSatisfy!(isInt, Args))
{
test2(args);
}
void test2(int[] ints...)
{
// stuff
}
or
template isInt(T) { enum isInt = is(std.traits.Unqual!T == int); }
void test(Args...)(Args args)
if(args.length > 0 && std.meta.allSatisfy!(isInt, Args))
{
static void test2(int[] ints...)
{
// stuff
}
test2(args);
}
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list