## digitalmars.D.learn - casting from variant

• cal (8/8) Aug 04 2012 In the following code:
• Philippe Sigaud (7/12) Aug 04 2012 I think it's a property-like problem: you need () after .get!()
• cal (2/17) Aug 05 2012 Ah thanks!, I didn't even think of that.
• Philippe Sigaud (6/12) Aug 05 2012 Btw, std.variant.Variant also offers a `.coerce` method:
• cal (2/17) Aug 05 2012 Even better :)
"cal" <callumenator gmail.com> writes:
```In the following code:

Variant j;
j = 1.0; // make it a double
auto x = cast(float)(j.get!(double)); // fail: cannot cast
j.get!(double)
auto y = cast(float)x; // fine

The first attempt to cast doesn't compile, but the second
less-direct cast is OK. Is that a bit strange?
```
Aug 04 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
```On Sun, Aug 5, 2012 at 5:40 AM, cal <callumenator gmail.com> wrote:
In the following code:

Variant j;
j = 1.0; // make it a double
auto x = cast(float)(j.get!(double)); // fail: cannot cast j.get!(double)
auto y = cast(float)x; // fine

I think it's a property-like problem: you need () after .get!()
I had this problem a few days ago.

Variant j;
j = 1.0; // make it a double
auto x = cast(float)(j.get!(double)()); // fine
auto y = cast(float)x; // fine
```
Aug 04 2012
"cal" <callumenator gmail.com> writes:
```On Sunday, 5 August 2012 at 06:31:37 UTC, Philippe Sigaud wrote:
On Sun, Aug 5, 2012 at 5:40 AM, cal <callumenator gmail.com>
wrote:
In the following code:

Variant j;
j = 1.0; // make it a double
auto x = cast(float)(j.get!(double)); // fail: cannot cast
j.get!(double)
auto y = cast(float)x; // fine

I think it's a property-like problem: you need () after .get!()
I had this problem a few days ago.

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.
```
Aug 05 2012
Philippe Sigaud <philippe.sigaud gmail.com> writes:
```On Sun, Aug 5, 2012 at 6:34 PM, cal <callumenator 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.

http://dlang.org/phobos/std_variant.html#convertsTo
```
Aug 05 2012
"cal" <callumenator gmail.com> writes:
```On Sunday, 5 August 2012 at 18:04:41 UTC, Philippe Sigaud wrote:
On Sun, Aug 5, 2012 at 6:34 PM, cal <callumenator 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.