#31bikeshed: what syntax should be used for variables?

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    currently the language uses let and var. before the language was started, there was a very heated argument as for if to adopt the js const/let, the rust let/let mut, or the zig const/var, or another paradigm like val/var

    i want to note the current semantics of let and var, because they are technically two very different things.

    • let x = y is an immutable reference to y. Transform doesnt actually emit any instructions for this besides the instructions for y. When x is used later, the instruction ref for y is inserted in place.

    • var x = y creates a .memory instruction that holds y as it's initial value. the .assign instruction is able to re-assign the memory location, and usages of x use the instruction ref of the memory. semantic analysis passes along the .memory and codegen will give it a js variable declaration

    it should also be noted that runtime memory locations can only store runtime-supported types, such as int, and float. this means stuff like this is a warning:

    // warn: not sure which type numeric literal `0` is
    // add a type annotation, defaulting this to `float`
    var count = 0;
    
    var count: int = 0; // ok
    

    Milestone: 0.3.0.

  • avatar~clover changed the status to Todo · 2 years ago
  • avatar~clovercommented · 2 years ago
    Author
    Owner

    If #60 is implemwnted, the semantics of var are changed and my personal opinion of separating "let" and "var" would no longer apply as constant proportion would exist in both forms.

    encouraging constant values over mutable ones via shorter syntax might be a smart choice.

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    To resolve this, I want to add bikeshed feature flags. the three variants i can think of are:

    --unstable-local-keyword=let-mut
    --unstable-local-keyword=let-var
    --unstable-local-keyword=const-var
    
  • avatar~jeff-hykincommented · 2 years ago

    Sorry to add another option but its possible to reduce the syntax by changing the operator

    // const
    a = 10
    // mutable
    b := 10
    // alternatively "penalize" mutable (and make it more visible)
    var b := 10
    

    I personally find const/var to be the most verbally clear, but I agree it would be better to make const-things easier than mutable-things. Maybe the operator change can help with that

  • avatar~clovercommented · 2 years ago
    Author
    Owner

    a = 10 is ambiguous with reassignment. but the approach i'll consider is using x := 1 for the declarations, using var x := 1 to declare the mutable.

    i dont think i'll enjoy this variant but to be thorough it should be an available option

  • avatar~jeff-hykincommented · 2 years ago

    a = 10 is ambiguous with reassignment

    Sorry my original message was miseleading, I was assuming = would ONLY work with constants. E.g.

    a = 10
    var b := 10
    b = 11 // error
    b := 12 // no error
    

    The thought being that = is kind of like a math equality, and := is assignment.