Scripting with Variant from std.variant: parameter passing

Anonymouse zorael at gmail.com
Fri Feb 2 11:31:09 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?

The easiest thing would be to actually pass it a `Variant` with 
`someFunction(Variant(myInt))`.

The more-involved thing would be to write a template constrained 
to non-`Variants` that does the above for you.

```d
auto someFunction(T)(T t)
if (!is(T : Variant))
{
     return someFunction(Variant(t));
}

auto someFunction(Variant v)
{
     // ...
}

void main()
{
     someFunction(42);
     someFunction("hello");
     someFunction(3.14f);
     someFunction(true);
     someFunction(Variant(9001));
}
```


More information about the Digitalmars-d-learn mailing list