3 ms·
It says Zig eliminates "implicit conversions unless they are guaranteed to be safe (for example, assigning a u8 value to a u16 variable cannot fail or lose data
by logicchains 5y ago
It says Zig eliminates "implicit conversions unless they are guaranteed to be safe (for example, assigning a u8 value to a u16 variable cannot fail or lose data)", but then suggests that @as should be used when "casting an int to a larger-size int of the same sign". Why should @as be used if it's safe to implicitly convert?
- flohofwoe 5y agoHmm, this indeed doesn't make much sense. Assigning to a wider type with or without different signedness doesn't require a cast as long as all bits fit into the new type: https://www.godbolt.org/z/x8a65sPcr https://www.godbolt.org/z/x8a65sPcr
- AndresNavarro 5y agoWhat I took from the article is that it's useful when combined with type inference. So in Zig you could do: var x : u8 = 5; var y = @as(u32, x); OR var x : u8 = 5; var y : u32 = x; The cast is needed in the first case to force the inference y: u32, otherwise it would be y: u8