riolet / rix

Rix language combines the power of C language and the convenience of a high level language
GNU General Public License v3.0
729 stars 27 forks source link

Make variable declaration in Ritchie require the := operator #32

Open rrezel opened 8 years ago

rrezel commented 8 years ago

Suggestion: make variable declaration in Ritchie require the := operator instead. That fits into your syntax well but prevents these kinds of errors.

Nim var x = 5

...lots of code...

if someCondition: var x = 10 echo x # prints '10'

...lots of code...

echo x # prints '5' Ritchie x = 5

// ...lots of code...

(someCondition) if x = 10 print x // prints '10'

// ...lots of code...

print x // prints '5' or '10' There are a couple of problems with this scenario in Ritchie: The if condition may have been added by a second programmer who didn't look at the entire code body (or by the same person a long time after they wrote the original code). In Nim, the x var is shadowed inside the condition scope and won't mutate the upper x var.. However, in Ritchie, the top x gets mutated whenever the runtime condition hits? If the if condition is ever removed but it's body kept (again, by someone who didn't fully read all the code and notice the top level x var) then Ritchie will happily overwrite the value of the top x? Whereas Nim will alert the programmer of the mistake at compile-time (since you'd have two var x in the same scope). In short, it's error prone not to have a distinction between variable assignment, and variable declaration. Unless I missed something (which is very possible as I've only just taken a glance at Ritchie) then it's a design mistake. Suggestion: make variable declaration in Ritchie require the := operator instead. That fits into your syntax well but prevents these kinds of errors.