0

I have a u64 value that I need to cast to i64. @as, @intCast, and @truncate all give compiler errors because not all u64 values fit into i64. That's fair enough but in this case I need an i64 and I'm willing to accept an error/crash in the unlikely event that the value doesn't fit. Does Zig have a function or operator to do this?

iafisher
  • 938
  • 7
  • 14

1 Answers1

0

In 0.11, the syntax of @intCast (and some others) has changed slightly.

@intCast no longer takes a type argument. Instead, you must rely on Result Location Semantics:

  • const x: T = e gives e a result type of T
  • @as(T, e) gives e a result type of T
  • return e gives e a result type of the function's return type
  • S{ .f = e } gives e a result type of the type of the field S.f
  • f(e) gives e a result type of the first parameter type of f

For example:

const result: i64 = @intCast(value);

or

const result = @as(i64, @intCast(value));
sigod
  • 3,514
  • 2
  • 21
  • 44