opened 08:33PM - 06 Sep 23 UTC
kind:breaking
parser
kind:feature
This is inspired by https://github.com/JuliaLang/julia/issues/51107 and https://…github.com/JuliaLang/julia/issues/51183 where there is ambiguity between defining and reassigning. I still dislike that when writing `x =` inside a function I have to worry about whether `x` is defined in the enclosing scope.
I like how Fortress distinguished reassignment with `:=` from defining with `=` so it couldn't happen by accident (and as a one-character "tax" on reassignment). In Scheme/Racket assignment is `(set! x 2)`.
Suppose I want to always be explicit about whether I'm defining a new variable or changing the old one and have a linter disallow ambiguous cases. For defining a new variable there is `local x`. Could Julia also have a syntax for explicitly reassigning, like `x := 2` ?
Example
```jl
julia> function f()
x = 1
function g()
x = 2
end
g()
x
end
f (generic function with 1 method)
julia> f()
2
```
---
Update: Pointed out on [Discourse](https://discourse.julialang.org/t/on-using-vs-for-assignment/110302), a problem with the proposed syntax is that in Golang the rule is the other way around: `:=` is definition and `=` is assignment. This could potentially cause confusion for some users. However, only 3% of Julia users use Go ([2023 Julia survey](https://julialang.org/assets/2023-julia-user-developer-survey.pdf)) and, sociologically speaking, afaict Go's market doesn't have too much overlap with Julia's. Moreover, SQL and Mathematica, which share far more users with Julia, already use `=` for different meanings without problem. Definition syntax is so ubiquitous everybody's gonna understand very quickly what the rule is. So I suspect it’s relatively unlikely to cause excessive confusion in practice. Also, having the slightly shorter `=` syntax be for definition rather than reassignment is a nice mild encouragement in the right direction.
Stefan suggested an opt-in to requiring `:=` for reassignment, or using `<-` for reassignment.