Re: problems with the scoping in eu4
- Posted by ChrisB (moderator) Jan 07, 2011
- 2167 views
Hi
1 Shadowing:
One cannot redefine variable names eg
integer f = 0 ... sequence f = {}
is an error
As it should be - why would you want to redifine a variable within the the same block / scope level?
likewise :
for i = 1 to 10 do puts(1,sprintf("%d, ",2*i) end for ... for i = 1 to 20 do .. <= error
No it doesn't, I use for i loops all over the place, sometimes one after the other.
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
This is expected behaviour - these are two separate scope levels, the newly defined variable takes precedence over the one defined in the scope level above. Just use more meaningful variable names if you want clarity.
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
Again, I would expect this to happen - i is repeatedly being redefined, and consequently reset. What behaviour would you expect?
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
Don't you need a label for the entry? Doesn't the entry just define a start point for the first loop run?
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.
This is how Ada does stuff, not Euphoria.
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.
Not quite sure I follow that. Aren't the case options defined at the time the switch is executed, so why would you want to make a = x+1, this would equate to saying always do case (x+1), and never do the others.
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
Thats how Ada does stuff, not Euphoria.
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]
And I'm not saying that Ada should change to follow the cleaner(and more logical to me) rules of Euphoria either, but I use Euphoria, and I don't use Ada. I hope Euphoria never does become Ada, because then all we'll have is Ada.
Kindest regards
Chris