Zig pipe operator

The problem is that Zig currently requires explicit casting in many situations that wouldn’t lose information. It’s a known issue though, and tbf, Rust is much worse in that regard.

Personally I would put the ‘casting noise’ very high on the list of issues that need to be fixed, because all that required explicit casting basically doesn’t let you see the forest for the trees (e.g. it becomes much harder to read what an integer math expression actually does).

1 Like

I don’t want to ask you to do look it up, but do you happen to have an example or link to an issue handy?

Yes, I was able to remove a lot of casting when porting code from Rust to Zig, mostly because Rust doesn’t have implicit integer widening and Zig does.

The one case where I notice I have to cast more than expected in Zig (because I did not have to cast in Rust) is when I want a for (0..non_usize_val) |x| loop over something that is not a usize. I have seen the outstanding issues on that one, so perhaps it will be improved.

Yes, I’ve also experienced that issue. Also I’ve wanted to iterate over ranges that include negative values.

I listed a couple examples in the linked blog post. For instance here it’s clear that the result fits into a u4, but a cast is still necessary (e.g. this doesn’t compile without an @intCast):

fn trunc4(val: u8) u4 {
  return val & 0xF;
}

…same with a rightshift, the result fits into an u4 without data loss, but a cast is still needed, e.g. this also doesn’t compile:

fn bla(val: u8) u4 {
  return val >> 4;
}

…and here’s another one: the loop counter fits into an u4, but still needs to be casted:

for (0..16) |_i| {
  const i: u4 = @intCast(_i);
}

(PS: that link I talked about was in another reply… Zig and Emulators , search for Bit Twiddling and Integer Math can be awkward)

1 Like

Thanks for this info!

1 Like

It’s a known issue though,

I have hopes that it might get solved one day:

1 Like

Im sure you are aware, but for others not.

should address these cases.