const i = someRuntimeComputation(); // will give 0 here in my example
const from align(@alignOf(f32)) = [_]u8{ 1, 2, 3, 4 };
const to: f32 = @bitCast(from[i..i + 4]); // []const u8
with:
error: cannot @bitCast from '[]const u8'
The index are runtime known so we get a slice. If this index had been comptime known, I would have ended up with an array and this is properly handled with .*.
I should also add that you use @bitCast to reinterpret non-pointer values (e.g. f32 to u32), and @ptrCast or @ptrFromInt/@intFromPtr to reinterpret pointers. A slice counts as a pointer.
Slicing an array gives you a slice, but you need an array to bitcast. Deferencing a slice with a comptime-known length will give you an array, like so:
I was thinking about your solution, isn’t it a limitation of zig? I mean from[i..i + 4] and from[i..][0..4] should really be the same. Sure i is a runtime value but the slice boundaries are relative to it.