Scripting with Variant from std.variant: parameter passing
Danilo
codedan at aol.com
Sat Feb 3 08:04:40 UTC 2024
On Friday, 2 February 2024 at 11:31:09 UTC, Anonymouse wrote:
> 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));
> }
> ```
To be honest, this doesn't make sense.
`if (!is(T : Variant))` returns true for inputs like 42, "hello",
3.14f, but the input is not a Variant but a random type.
Yes, it's nice that it works in this case. It's just not logical,
it doesn't make sense because 42 just simply isn't a Variant,
it's an `int`.
More information about the Digitalmars-d-learn
mailing list