Stream: beginners

Topic: Question about number constants


view this post on Zulip cerk (Aug 06 2026 at 23:00):

I'm on Roc compiler version nightly-2026-08-06-61bbb59.

Here is a small example:

main! = |_args| {
    a = ["Larry", "Curly", "Moe"]
    b = a.swap(0, 2)?
    c = b.get(0)?
    echo!("${Str.inspect(b)}\n")
    echo!("${Str.inspect(c)}\n")
    Ok({})
}

Note that the swap and get functions take U64s.

Here is a larger example:

test_names_2 = "Vyrdax,Drakzyph,Fyrryn,Elarzris"

test_dirs_2 = "R3,L2,R3,L3"

main! = |_args| {
    var $names = test_names_2.split_on(",")
    dirs = test_dirs_2.split_on(",")

    for dir in dirs {
        right = dir.starts_with("R")
        dist = U64.from_str(dir.drop_prefix("R").drop_prefix("L"))
        match dist {
            Ok(d) => {
                index =
                    if right {
                        d.mod_by($names.len())
                    } else {
                        ($names.len() - (d.mod_by($names.len()))).mod_by($names.len())
                    }
                match $names.swap(0.U64, index) {
                    Ok(new_names) => {
                        $names = new_names
                    }
                    Err(OutOfBounds) => {
                        crash "bad"
                    }
                }
            }
            Err(_e) => {
                crash "bad"
            }
        }
    }

    name = match $names.get(0.U64) {
        Ok(s) => s
        Err(OutOfBounds) => {
            crash "bad"
        }
    }

    echo!("${name}\n")
    Ok({})
}

Note that 0.U64 number constants in this larger example (in the swap and get calls). If I don't use the .U64, the compiler complains about not expecting a Dec for those numbers.

When should I use just 0 versus 0.U64?

Thanks!

view this post on Zulip Anton (Aug 07 2026 at 12:23):

It seems like we can improve the compiler so you do not need to add the U64.

What do you think of "Option 1" in the file @Jared Ramirez @Richard Feldman?

FableAnalysis.txt

view this post on Zulip Richard Feldman (Aug 07 2026 at 15:55):

hm I think Option 2 actually sounds better, particularly:

a var that is both signature-reachable and chain-reachable would become shared-monomorphic instead of quantified, which is a subtle behavior change to audit.

I think that's the behavior that fits let-generalization: let's not

view this post on Zulip Richard Feldman (Aug 07 2026 at 15:56):

wanna run that by Fable and see if it thinks I'm missing anything? :smile:

view this post on Zulip Richard Feldman (Aug 07 2026 at 15:57):

maybe there's some edge case I'm not thinking about, but in general we have decided to make literals end up resolving to one monomorphic type for exactly those reasons

view this post on Zulip Richard Feldman (Aug 07 2026 at 15:57):

(actually I think "let's not" suggested we could make an exception for number literals, but as I recall we decided not to make that exception)

view this post on Zulip Anton (Aug 07 2026 at 17:20):

Fable likes option 2 after a deep dive :)

view this post on Zulip Richard Feldman (Aug 07 2026 at 17:21):

cool, wanna make a PR for it? I can review

view this post on Zulip Anton (Aug 07 2026 at 17:23):

Yes, Claude is on it

view this post on Zulip Anton (Aug 07 2026 at 18:38):

Nice and small PR#10671

view this post on Zulip Richard Feldman (Aug 07 2026 at 18:41):

Greptile's comment is accurate :smile:

view this post on Zulip Anton (Aug 08 2026 at 15:27):

I have addressed the comment :)

view this post on Zulip Anton (Aug 11 2026 at 10:46):

cool, wanna make a PR for it? I can review

Is it good to merge @Richard Feldman?

view this post on Zulip Richard Feldman (Aug 11 2026 at 11:15):

I'm still pushing revisions :sweat_smile:

view this post on Zulip Anton (Aug 11 2026 at 11:29):

Richard Feldman said:

I'm still pushing revisions :sweat_smile:

I'm not sure I understand, do you mean you're working on the same code in other PRs?

view this post on Zulip Richard Feldman (Aug 11 2026 at 12:22):

oh oops I thought you were referring to a different PR

view this post on Zulip Richard Feldman (Aug 11 2026 at 12:22):

feel free to merge this one! :+1:


Last updated: Aug 12 2026 at 12:35 UTC