problems with the scoping in eu4
- Posted by bill Jan 07, 2011
- 2192 views
1 Shadowing:
One cannot redefine variable names eg
integer f = 0 ... sequence f = {}
is an error
likewise :
for i = 1 to 10 do puts(1,sprintf("%d, ",2*i) end for ... for i = 1 to 20 do .. <= error
however this is different:
integer f = 2 function ff(sequence f = "ok") return f end function puts(1,sprintf("%s\n",ff())) => ok puts(1,sprintf("%d, ",f) => 2
2 while do problems:
Cannot initialise a counter inside a loop
integer r = 4 while r > 0 do integer i = 0 puts(1,sprintf("%d, ",i) i += 1 r -= 1 end while
prints: 0, 0, 0, 0
while r > 0 with entry do integer i = 0 puts(1,sprintf("%d, ",i) r -= 1 entry i += 1 <= at runtime i has no value end while
3 while with entry seems quite unnecessay given exit (using ada loop, end loop & exit when).
sequence ret = {} loop from = find_from (x, source, from) exit when from = 0 ret &= from from += 1 end loop
Exit can take a label.
Now the code executes in order, there is no jump into scope.
4 another reason not to use while with entry (readability).
It places the entry code below any inner loop
5 Switch has problems as well.
switch a case x then integer i = 0 puts(1, sprintf("%d",i)) i += 1 case x+1 then puts(1, sprintf("%d",i)) i += 1## ...
This should compile but cannot work with a = (x+1)
Any declaration before case else allow a jump into scope.
6. Commentary:
ada has anonymous blocks syntax
eg an in-line swap of integers x, y
declare t integer := x begin x := y y := t end
A possible while syntax could be
while condition declare type id [= value] do .. end
I am not saying this is how things should be done but these aspects of Euphoria make the new features difficult to impossible to use.
[matt: added eucode tags]