Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let syntax to distinguish bindings vs body #51183

Open
jariji opened this issue Sep 4, 2023 · 1 comment
Open

let syntax to distinguish bindings vs body #51183

jariji opened this issue Sep 4, 2023 · 1 comment
Labels
speculative Whether the change will be implemented is speculative

Comments

@jariji
Copy link
Contributor

jariji commented Sep 4, 2023

The documentation says

Additionally, the syntax has a special meaning for comma-separated assignments and variable names that may optionally appear on the same line as the let....The variables introduced on this line are local to the let block

These look similar but do very different things:

function foo()
    x = 1
    let x = 2
        x
    end
    x
end
foo() == 1


function bar()
    x = 1
    let
        x = 2
        x
    end
    x
end
bar() == 2

I request a change to this syntax to make it easier to

  • notice the difference between binding and assignment
  • put bindings on separate lines for readability without risk of mutating the enclosing scope (currently done with commas, but not very readable if the expressions are long)

One way to do this is adding a token to separate the place where = makes bindings from where it makes assignments, such as within:

let x = 1 # binding
    y = 2 # binding
within 
    x = 2 # assignment
    x + y
end

This could be a 2.0 change but ideally there is change to address the issues in 1.x, allowing within.

Other languages

In Scheme, bindings are separated with parentheses and assignments are made with set!:

(let 
  ((x 1) ;; binding
   (y 5)) ;; binding
  (+ x y))

In OCaml, in separates the bindings component from the expression that produces the value:

let n = 2
in n * n

and likewise in Nix:

let 
  x = 1;
  y = 1;
in
  x + y
@jariji jariji changed the title More distinguishable let syntax for binding vs assignment let syntax to distinguish bindings vs value-expression Sep 8, 2023
@jariji jariji changed the title let syntax to distinguish bindings vs value-expression let syntax to distinguish bindings vs body Sep 8, 2023
@brenhinkeller brenhinkeller added the speculative Whether the change will be implemented is speculative label Sep 9, 2023
@jariji
Copy link
Contributor Author

jariji commented Nov 29, 2023

Separating the bindings from the body also means we can use ; for multiple bindings instead of ,, which currently is not nice:

julia> let a,b = (1,2); (a,b) end
ERROR: UndefVarError: `a` not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

2 participants