Sorry if it's obvious. My guess is that it casts to int, but the type signature takes Int a and returns Int b, I don't really get what that means.
It's currently undocumented: https://www.roc-lang.org/builtins/Num#intCast which is why I'm posting here :D
Yeah, so if you have a U8
you can turn it into a U16
Interesting, I tried passing a U8 and got a compiler panic
thread 'main' panicked at crates/compiler/gen_dev/src/generic64/mod.rs:4742:22:
not yet implemented: int cast from U8 to I64
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
it works with an i8
. The type that comes back is Int b
» x = Num.toI8 4
4 : I8
» Num.intCast x
4 : Int b
What is an Int b? :sweat_smile:
I'm guessing you can only cast between things of the same signedness
Like signed I32
to I64
or unsigned U8
to U32
/U128
etc
I'm not sure if you can cast down, it might be dissallowed or lose information or panic (1 sec I'll test in the repl)
» x : U8
… x = Num.intCast 355u16
99 : U8
It looks like it just throws information away
Oh so does intCast
know what type you want to cast to somehow?
Yeah, it's done at compile time (well the types are resolved and concrete etc).
All types in roc are fully inferred if you don't give an annotation
Aha. That explains it. Quite magical :D
Maybe I can try opening a PR updating the docs now that I understand it a bit more
That would be super
This is where you would put the doc comments https://github.com/roc-lang/roc/blob/587d0afd462994edc15fcceb8514bc0d2662b12b/crates/compiler/builtins/roc/Num.roc#L551
https://github.com/roc-lang/roc/pull/7131
I think I've done it right... :D
Nathan Kramer said:
Interesting, I tried passing a U8 and got a compiler panic
thread 'main' panicked at crates/compiler/gen_dev/src/generic64/mod.rs:4742:22: not yet implemented: int cast from U8 to I64 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
We probably should add an extra note to these error messages that they are dev backend specific (so only hit in the repl currently). That would help clarify to users that they can do any intcast in standard apps
Nathan Kramer has marked this topic as resolved.
Last updated: Jul 06 2025 at 12:14 UTC