Variable Bindings proposal discussion

Now this is a much more reasonable request.

Since default is ref, there are still two ways to express the same thing. The issue isn’t merely about saving characters but about the parsing rules for a ref. In any case, I’m simply describing what happens (you pointed out “inconsistency” where I see none).

Though maybe something like ref a, b should be parsed as ((ref a), b) so we have best of both worlds.

Food for thought as a stdlib contributor: if we were to go with the “binding only associated with a single name” approach, then, given the style guide’s “always declare variables” rule, we would be forced to spell out each one of them always.

ref is a parametric mutable reference type, which means it inherits the mutability of the type it references, for example:


fn get_first_element(ref my_list: List[Int]) -> ref [my_list] Int:
    ref first = my_list[0] # first is parametric mutable reference which inherits mutability from my_list
    return first

fn f(read immutable_list: List[Int], mut mutable_list: List[Int]):
    ref x = get_first_element(mutable_list) # x is mut
    x = 10 # this modifies the first element of mutable_list

    ref y = get_first_element(immutable_list) # y is read
    y = 20 # error: cannot assign to immutable reference

    # the same thing happens with for loops
    for ref element in mutable_list:
            element = 30 # this modifies the current element of mutable_list
    for ref element in immutable_list:
            element = 40 # error: cannot assign to immutable element

Thank you for the clarification.

1 Like