The language has always used = for both declaring and assigning to variables. IMO, this introduces a risk of misspelling variable names in assignments.
cd = 4
if condition
cp = 6 # uh oh, dyslexia time
end
Typos are often hard to spot yet easy to make. As a straightforward solution, I think we should have separate syntax for declarations and assignments. A few examples for what the declaration syntax could be:
variable_name := value (basically syntax stolen from Pascal and Go, syntax only needs operator with symbols)var/let variable_name = value (syntax seen in JavaScript, might have issues with code already using var as a variable name)Type variable_name = value (syntax seen in C-like languages, bad because verbose and cannot use uninitialized)This should still work for the sake of simplicity and readability:
if condition
x := 1
end
puts x # x : (Int32 | Nil)
I think it's a little too late in the language's lifecycle to change this, no matter people's opinion.
I'll close this. I'm sure we'll see languages which take some of crystal's ideas and iterate on others in the future :)
To be honest, I agree. A tool that converts existing code wouldn't be that hard but the community probably is too used to using = for everything at this point.
FWIW, we could consider having the compiler warn the programmer for unused/dead/useless assignments.
FWIW, we could consider having the compiler warn the programmer for unused/dead/useless assignments.
That's already ameba's job.
I don't think it belongs in the compiler because variables might just be unused because the code that uses them is excluded by some macro configuration. Also, this kind of error should be easily spotted in unit tests.
Most helpful comment
That's already ameba's job.
I don't think it belongs in the compiler because variables might just be unused because the code that uses them is excluded by some macro configuration. Also, this kind of error should be easily spotted in unit tests.