std.sumtype?

Paul Backus snarwin at gmail.com
Mon Aug 30 12:13:56 UTC 2021


On Monday, 30 August 2021 at 11:19:46 UTC, Oleg B wrote:
> On Monday, 30 August 2021 at 10:50:40 UTC, drug wrote:
>> A little tip: you can use just `cast()` to remove qualifiers:
>> ```D
>> return match!(v => staticIndexOf!(typeof(v), 
>> T.Types))(cast()val));
>> ```
>
> I'm not sure what with simple `cast()` not `opCast` nor ctors 
> are called, it depends on code of `SumType`, if it changed they 
> can be called (not sure). Cast through pointer avoid it. Or I 
> mistake?

`SumType` does not define any `opCast` overloads, so you don't 
have to worry about that.

Re: ctors/copying, the [language spec for casts][1] says:

> 10. Casting to a CastQual replaces the qualifiers to the type 
> of the UnaryExpression.
>
> 11. Casting with no Type or CastQual removes any top level 
> const, immutable, shared or inout type modifiers from the type 
> of the UnaryExpression.

In other words, casting to a different qualifier does not create 
a new value; it just causes the compiler to treat the existing 
value as though it had a different type.

[1]: https://dlang.org/spec/expression.html#CastExpression


More information about the Digitalmars-d mailing list