#Peer type resolution

1 messages · Page 1 of 1 (latest)

shell lake
#

Why can't I sum an f32 with an u8? The following code gives error.
const a : u8 = 9; var b : f32 = 20; b+=a;
I expected that peer type resolution automatically converts the u8 into the f32 type before the sum.

lime olive
#

Floats and integers are a different beast, you have to explicitly convert between them.

shell lake
#

ok so I'm supposed to use @intToFloat function to explicitly do the conversion in these cases right?

narrow crow
#

Yep

#
const a: u8 = 9;
var b: f32 = 20;
b += @intToFloat(f32, a);
shell lake
#

and what I'm supposed to do when b is a generic type that could be both int or float (of any size)? is there an easy way to check if a type is a float?

narrow crow
#

Sure, std.meta.trait.is(.Float)(T)