Stream: beginners

Topic: ✔ Can someone explain what Num.intCast does


view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:11):

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.

view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:11):

It's currently undocumented: https://www.roc-lang.org/builtins/Num#intCast which is why I'm posting here :D

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:34):

Yeah, so if you have a U8 you can turn it into a U16

view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:38):

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

view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:41):

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:

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:41):

I'm guessing you can only cast between things of the same signedness

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:41):

Like signed I32 to I64 or unsigned U8 to U32/U128 etc

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:42):

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)

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:42):

» x : U8
… x = Num.intCast 355u16

99 : U8

It looks like it just throws information away

view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:44):

Oh so does intCast know what type you want to cast to somehow?

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:44):

Yeah, it's done at compile time (well the types are resolved and concrete etc).

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:44):

All types in roc are fully inferred if you don't give an annotation

view this post on Zulip Nathan Kramer (Sep 29 2024 at 07:45):

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

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:45):

That would be super

view this post on Zulip Luke Boswell (Sep 29 2024 at 07:48):

This is where you would put the doc comments https://github.com/roc-lang/roc/blob/587d0afd462994edc15fcceb8514bc0d2662b12b/crates/compiler/builtins/roc/Num.roc#L551

view this post on Zulip Nathan Kramer (Sep 29 2024 at 08:12):

https://github.com/roc-lang/roc/pull/7131

I think I've done it right... :D

view this post on Zulip Brendan Hansknecht (Sep 29 2024 at 14:58):

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

view this post on Zulip Notification Bot (Sep 30 2024 at 10:11):

Nathan Kramer has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC