1. variable declaration
- Posted by jessedavis Apr 08, 2016
- 1383 views
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
2. Re: variable declaration
- Posted by _tom (admin) Apr 08, 2016
- 1348 views
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
3. Re: variable declaration
- Posted by unsteady Apr 09, 2016
- 1333 views
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()
4. Re: variable declaration
- Posted by jessedavis Apr 09, 2016
- 1317 views
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
5. Re: variable declaration
- Posted by Spock Apr 10, 2016
- 1305 views
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
6. Re: variable declaration
- Posted by SDPringle Apr 10, 2016
- 1312 views
the terms are static scope vs. dynamic scope.
Shawn