Re: problems with the scoping in eu4

new topic     » goto parent     » topic index » view thread      » older message » newer message
jeremy said...
bill said...

About delarations in loops I am clearly of the opinion it is a mis-feature. Perhaps I am in a minority of one thinking this. Also I think while with entry is a mis-feature again it is my opinion.

If I use Euphoria I will not use these features.

Quite a few other languages must be in err as well, C/C++, Java, D for a few. Not that we are trying to copy those, but when you declare a variable inside of a loop, it just makes sense that the declaration occur each iteration of the loop.

For example:

integer cnt = 1 
for i = 1 to 10 do 
    cnt += 1 
    ? cnt 
end for 

You would expect that cnt += 1 would execute 10 times, no? Not 1 time, 5 or 8 times, correct? Why, then, would:

for i = 1 to 10 do 
    integer ten_i = i * 10 
    ? ten_i 
end for 

be any different? Each iteration, just as cnt += 1 is executed, integer ten_i = i * 10 is executed. To do anything different would be very confusing, bug producing, extremely complicated to explain and use, IMHO. For example, I don't even know what the above program should do if it didn't initialize ten_i each iteration as the code says.

You are missing the point.

What has been created is a block scope which finishes at the end of the loop. What has not been created is a block scope which is the whole of the loop.

If the scope was the whole of the loop then clearly the line integer ten_i could only be run once (at initialisation of the while). Thus it could be given a value.

I am not saying that doing this is good, I'm saying declaring variables inside loops as it is done in Euphoria with Euphoria semantics is very odd particularly so with the use of with entry

The problem is highlighted by this code:

1 while f > 0 with entry do 
2   integer sum = 0 
3   ... 
4 entry 
5   f = ... 
6   sum +=  
7 end while 


It compiles because the reference to sum is in scope. It dies because line 2 has not been executed.

You may say this is an abuse of with entry. But who is to say what needs to be done before the test is made? And you cannot limit scope to the lines before entry without making while with entry a special case.

You don't have block scope.
You don't have loop scope.
You have buggy here to end of loop scope.

Any variable declared inside a loop is in scope for any while, switch, if, function, procedure which comes after the declaration because scope is to the end. And is out of scope for lines after with entry on the first iteration. This is why Euphorias while scope is bad. Because it is unstructured.

The problem can be partly resolved retaining locality by writing this

if TRUE then                    -- begin block 
  integer sum = 0 
  while f > 0 with entry do 
    ... 
  entry 
    f = ... 
    sum += f 
  end while 
end if                           -- end block 

Which is better written as:

if TRUE then 
  integer sum = 0 
  while TRUE do 
    f = ... 
    sum += f 
  exit when ... (or if f=0 then exit end if) 
    ... 
  end while 
end if       

If I want a block inside the while I can do the same there

if TRUE then                    -- begin block 
  integer sum = 0 
  while f > 0 with entry do 
    ... 
    if TRUE then                 
      integer x           -- declaration in the if-block not the while 
      ...                 -- will this pollute the while's namespace? 
    end if 
    ... 
  entry 
    f = ... 
    sum += f 
  end while 
end if                           -- end block 

These declarations are much more useful than declarations in while .. end while scope.
However it is a pain to use clumsy notation like this.

Really what was needed (and only this):

declare 
  [declarations] 
begin 
  [run this code] 
end 


Better code could be this:

declare  
  integer sum = 0 
begin 
  loop 
    f = ... 
    exit when f = 0 
    sum += f 
    declare                 
      integer x  
    begin	   
     ...                      
    end 
    ... 
  end loop 
end 


Now, the with entry, I feel it's a great option. About jumping around, a loop is nothing but a conditional goto, the with entry is also nothing but a goto. Both make sense to me and both make my existing programs simpler. The less code I write as a developer means the more productive I will be (within reason of course). Thus, nice shortcuts like this make me as a developer more productive thus as an employee more profitable. In todays programming market, I enjoy being more profitable to my clients. Yes, something as simple as with entry makes a difference in clarity or code and profitability.

Jeremy

You are wrong. If a loop is nothing but a goto then there is nothing sepecial about a loop. It is true a loop could be:

:loop 
  if f = 0 goto end 
  ... 
  goto loop 
  ... 
:end 

And you can merrily jump around through your code. You had better have global scope and a good debugger as you need both.

It is like that in sed and other obscure languages - (obscure as in difficult to understand).

However Euphoria loops have scope. There your argument falls down.

Declaring variables in while loops is not helpful.
With entry is an unneeded kludge presumably brought into existence by the lack of an exit statement.
With/without fallthru is undesirable - switch should have.

Surely

loop 
  [run this code] 
  [check for exit] 
  [run more code] 
end loop 

is less cumbersome, difficult to undestand and problematic than

goto x 
loop  
  [check for exit] 
  [run more code] -- declare and use loop declared variables here 
:x 
  [run this code] -- no loop variables here 
end loop 

Bill

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu