casting from variant

cal callumenator at gmail.com
Sun Aug 5 11:29:42 PDT 2012


On Sunday, 5 August 2012 at 18:04:41 UTC, Philippe Sigaud wrote:
> On Sun, Aug 5, 2012 at 6:34 PM, cal <callumenator at gmail.com> 
> wrote:
>
>>> Variant j;
>>> j = 1.0; // make it a double
>>> auto x = cast(float)(j.get!(double)()); // fine
>>>
>>> auto y = cast(float)x; // fine
>>
>>
>> Ah thanks!, I didn't even think of that.
>
> Btw, std.variant.Variant also offers a `.coerce` method:
>
> auto x = j.coerce!float; // fine
>
> No need of parenthesis on this one.
>
> See also `.convertsTo`
>
> http://dlang.org/phobos/std_variant.html#convertsTo

Even better :)


More information about the Digitalmars-d-learn mailing list