Scripting with Variant from std.variant: parameter passing

Steven Schveighoffer schveiguy at gmail.com
Fri Feb 2 19:22:22 UTC 2024


On Friday, 2 February 2024 at 08:22:42 UTC, Carl Sturtivant wrote:
> It seems I cannot pass e.g. an int argument to a Variant 
> function parameter. What's the simplest way to work around this 
> restriction?

You'd have to implement the function that accepts the parameters 
and wraps in a Variant.

This is the best I can come up with, which should be 
copy/pasteable to other shims:

```d
void foo(Variant x, Variant y) { ... }

import std.meta : allSatisfy;

enum isVariant(T) = is(T == Variant);

// this is going to suck at CTFE but...
string argsAsVariants(size_t count)
{
    import std.format;
    import std.range;
    import std.alglorithm;
    import std.array;
    return iota(count).map!(i => format("Variant(args[%s])", 
i).join(",");
}

// shim
auto foo(Args...)(Args args) if (!allSatisfy!(isVariant, Args))
{
     mixin("return foo(", argsAsVariants(args.length), ");");
}
```

-Steve


More information about the Digitalmars-d-learn mailing list