1. variable declaration

I'm old & tired and now drawing a blank. Can someone give me a clue? Euphoria Version 4.01

 
include std/console.e 
integer size = 24 
integer start = 3 
if start + 24 > size then 
	integer last = size 
else 
	last = start + 24 
end if 
any_key() 

I can think of no reason why the compiler (token generator) cannot figure out that 4 bytes need be allocated for the variable last. It complains about error 74 (failure to declare last) in lines 6 & 8 (there is a blank line at the start). Since I have never been able to find an error list anywhere in the docs I guess I will have to assume that this is do having the declaration inside the if block. What I find worrying is that this is an artifact of JIT allocation. Any thoughts would help. Like I said, I'm old and brain dead.

Regards & thanks, jd

new topic     » topic index » view message » categorize

2. Re: variable declaration

This is a scope problem.

The declaration

	integer last = size  

only goes as far as the else statement.

The following last is in a different scope.

_tom

new topic     » goto parent     » topic index » view message » categorize

3. Re: variable declaration

You could add "integer" before "last" after the "else", but the compiler may not like it. I usually force a likely result before the conditional "if"

Try

include std/console.e  
integer size = 24  
integer start = 3  
integer last = start + 24  
if start + 24 > size then  
	last = size  
end if  
any_key()  

or

include std/console.e  
integer size = 24  
integer start = 3  
integer last = size  
if start + 24 <= size then  
	last = start + 24  
end if  
any_key()  
new topic     » goto parent     » topic index » view message » categorize

4. Re: variable declaration

Yes, you are oh so correct. I did not know this about if blocks, but it makes sense if one thinks about it.

Thanks much, jd

new topic     » goto parent     » topic index » view message » categorize

5. Re: variable declaration

jessedavis said...

Yes, you are oh so correct. I did not know this about if blocks, but it makes sense if one thinks about it.

Thanks much, jd

And yet, the way you first programmed the if block made sense to you at the time. When they added scopes to Eu I altered the parser of my Orac compiler to obviate them. Maybe it was too long ago when they had the discussion (if at all) about scopes but I can't recall what the supposed benefits of scopes were. I would be interested to see a real-life example of scopes in action that was genuinely superior to the less regimented schema.

Spock

new topic     » goto parent     » topic index » view message » categorize

6. Re: variable declaration

the terms are static scope vs. dynamic scope.

Shawn

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu