Scripting with Variant from std.variant: parameter passing

Anonymouse zorael at gmail.com
Sat Feb 3 10:04:59 UTC 2024


On Saturday, 3 February 2024 at 08:04:40 UTC, Danilo wrote:
> 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`.

I read it several times but I don't think I understand what you 
mean.

The constraint `if (!is(T : Variant))` is true for every input 
that is not a `Variant`, yes. The point of it is to let calls to 
`someFunction(myVariant)` resolve to the non-templated `auto 
someFunction(Variant)`.

Is your argument that it's wrong to assume an `int` *can be* 
wrapped in a `Variant`, because it isn't one? That in turn 
doesn't make sense -- your example does the same, just explicitly.

```d
void main() {
     f( Variant(42)    );
     f( Variant(2.5)   );
     f( Variant("Hi!") );
}
```

How is this different from the following?

```d
void main() {
     (v){ f(Variant(v)); }(42);
     (v){ f(Variant(v)); }(2.5);
     (v){ f(Variant(v)); }("Hi!");
}
```

And how is that different from the following?

```d
void main()
{
     auto g(T)(T t)
     {
         f(Variant(t));
     }

     g(42);
     g(2.5);
     g("Hi!");
}
```

Which is in what way different from the following?

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

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

void main()
{
     g(42);
     g("hello");
     g(3.14f);
     g(true);
}
```

And how is that not the same as my original example?


More information about the Digitalmars-d-learn mailing list