Why are signed and unsigned integers inherently incompatible with each other? I find myself adding say a usize and an i8 together often. Why are these two incompatible?
Like sure, you could argue “oh, its cuz if the usize value is less than 255 it’ll underflow”, and the compiler wants to protect you from that happening. But the compiler is not doing anything but adding more work to do.
I need to add a whole @as(T, @intCast()). The compiler is not protecting against anything here. I’d argue this also makes it more unreadable. It would be better if it was easier to type, but no, I have to surround the expression in two parenthesis, type in @ and some intrinsic names, it’s not easy to type.
And I have to do this INCREDIBLY often. Maybe my choice of types is poor, and I don’t know how to do it in idiomatic Zig, but the this weird behavior just makes me put signed types everywhere for convenience, even if I know they will never be negative, just to dodge having to put more @as and @intCast.
You use unsigned values when you know they will never be negative, obviously, and when something will be negative, obviously you use signed. Like, say I have a car with a position, which will always be positive, and maybe some wheel of the car has a negative position relative to the car. If I have to get the world position of the wheel, I need to add signed and unsigned positions to each other. I don’t care if maybe it underflows, I can handle that when it happens. But @intCast will not help in handling that.
And also, why are result types not propagated to the operands of an operation? This is also a source of annoyance for me. If
const a: u16 = 9;
const b: u8 = @intCast(a);
works, why shouldn’t
const a: u16 = 9;
const b: u8 = 1 + @intCast(a);
work?
I have to do this instead, which just adds to the visual clutter and it’s hard to read anything.
const b: u8 = 1 + @as(u8, @intCast(a));
How do I solve these problems? I genuinely can’t think of a way to structure the types so that they lessen the need of these intrinsices. The fact that loops always have a usize capture does not help either.