tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
45 stars 9 forks source link

raise error on variable shadowing #3204

Open michaellilltokiwa opened 2 weeks ago

michaellilltokiwa commented 2 weeks ago

example

ex is

  x := 1
  x := 2

  say x

Variable shadowing is most likely not programmers intent.

Inspired by: https://flix.dev/principles/

fridis commented 2 weeks ago

Fuzion is alredy picky on ambiguous accesses to routines as in

  ex is
    x => 1
    inner =>
      x => 2
      say x
    inner

which results in

 > ./build/bin/fz ex_inner.fz 

/home/fridi/fuzion/work/ex_inner.fz:5:11: error 1: Ambiguous targets found for call to 'x'
      say x
----------^
Found several possible call targets within the current feature at different levels of outer features:
in 'ex.inner' found 'ex.inner.x' defined at /home/fridi/fuzion/work/ex_inner.fz:4:7:
      x => 2
------^
and in 'ex' found 'ex.x' defined at /home/fridi/fuzion/work/ex_inner.fz:2:5:
    x => 1
----^
To solve this, you may qualify the feature using 'ex.inner.this.x' or 'ex.this.x'.

one error.

while the same with fields

  ex is
    x := 1
    inner =>
      x := 2
      say x
    inner

works. I have the impression that code like

  speed := miles / hours
  speed := speed * 1.6   # km/h

is something we do not use often and maybe even should not use, allowing this was probably a bad idea from the beginning. It is time to treat fields and routines the same way.