arguments to functions in types not declared

Previous topic - Next topic

aonyn

Hi,

I just encountered some strange behavior.

I have several LOCAL variables, defined in my program after my TYPE definitions.
These type definitions contain functions, some with arguments.

The strange thing is, I added many functions with arguments, using the same names as some of the local variables outside and after the types, without trouble.
However, I just added another function to the last defined type, using two argument names which had already been used in other functions, and the compiler began to complain that those variables were not defined in this scope.

I was able to correct the problem two ways, but still I think there is some bug in the compiler.
The first way to correct the problem was to define the LOCAL variables to the main program BEFORE my type definitions - compiler problem gone, program works as expected.
The second way to correct the problem was to change the names of the arguments in the embedded function which caused the problem to new and unique names. - compiler problem gone, program works as expected. The strange thing being that earlier embedded functions use the existing argument names without trouble.

I was able to work around the problem, so it is not a showstopper.
However the behavior seems odd, and perhaps suspect, so I decided to post about it here.

regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

MrTAToad

Make sure that you use self appropriately!

Can you make the source availiable too ?

aonyn

Hi again MrTAToad,

Here is sample code, just a small sample from my project, but enough to demonstrate the bug.
Please notice a few things.

The offsetX% and screenWidth% LOCAL variables defined below the TYPE definition.
In both embedded functions, offsetX% is an argument. The function called testInput() has no problem using the argument name offsetX%.
However the next function called draw() will not allow it.
This is the compile error:
Code (glbasic) Select

_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.8.044 SN:380c888f - 3D, NET
Wordcount:11 commands
compiling:
C:\DOCUME~1\jasonl\LOCALS~1\Temp\glbasic\gpc_temp0.cpp: In function `int __GLBASIC__::__MainGameSub_()':
C:\DOCUME~1\jasonl\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:93: error: `screenWidth' was not declared in this scope
C:\DOCUME~1\jasonl\LOCALS~1\Temp\glbasic\gpc_temp0.cpp:97: error: `offsetX' was not declared in this scope
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 1.4 sec. Time: 17:28
Build: 0 succeeded.
*** 1 FAILED ***


Also, please note I duplicated the draw() function and commented it out again, but with the suspect argument names changed to new unique names.
If you comment the original draw(), and uncomment the altered draw(), you will see changing the names fixes the problem.
Note that the testInput() function is still using the original argument offsetX% successfully.

Here is the sample code:
Code (glbasic) Select

// --------------------------------- //
// Project: TypeBugSample
// Start: Wednesday, September 15, 2010
// IDE Version: 8.085

SYSTEMPOINTER TRUE

SETCURRENTDIR("Media")

TYPE bowl
file$
id%
id2%
chnInst% = 4
chn% = 0
chn2% = 0
vol# = 0.8
dur%
rep%
sTime%
cTime%
phase%
x%
y%
width%
height%
color%

FUNCTION testInput: touchX, touchY, offsetX%
IF (touchX >= (self.x% + offsetX%)) AND (touchX <= (self.x% + self.width% + offsetX%)) AND (touchY >= self.y%) AND (touchY <= (self.y% + self.height%))
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION

FUNCTION draw: screenWidth%, offsetX%
IF (self.x% + offsetX% + self.width% > 0) AND (self.x% + offsetX% < screenWidth%)
DRAWRECT self.x% + offsetX%, self.y%, self.width%, self.height%, self.color%
ENDIF
ENDFUNCTION

// FUNCTION draw: scrWidth%, offset_X%
// IF (self.x% + offset_X% + self.width% > 0) AND (self.x% + offset_X% < scrWidth%)
// DRAWRECT self.x% + offset_X%, self.y%, self.width%, self.height%, self.color%
// ENDIF
// ENDFUNCTION
ENDTYPE

LOCAL screenWidth% = 480
LOCAL offsetX% = 0


Regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

Slydog

Moving the last two 'LOCAL' declarations to the top of the code seems to make it work.
But you're right, and my guess is that it seems like a scope issue / bug.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

aonyn

Hi Slydog,

Yes, I noticed your solution works as well, and had mentioned it in my original post.
I am sorry I forgot to add that solution with comments as well to the sample code.
And yes, it does seem to be a scope bug, fortunately in my case, not a showstopper, I can easily work around it, and I am still moving forward.
However, perhaps it could be a showstopper in a more sophisticated project than the one I am working on.
I just wanted to make sure Gernot is aware of it, since I stumbled on it, and did not know if it is a known issue.
Perhaps I can plant my flag and call it my discovery.  :giveup: (I know, wrong context for this emoticon, but it sort of works in a convoluted way  :S)

regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9

MrTAToad

Unfortunately, parameter variables aren't local (to the function), which means if you define them elsewhere you get the problem - the LOCAL statements are the cause.

We did have a poll awhile ago about how we would like parameter variables to be defined - hopefully at some point the winning result (if I remember correctly, local to the function) will be implemented  :nana:

The obviously way around is to make sure that variables outside a type are named differently in some way.

aonyn

Hi MrTAToad,

Thanks once again, at least now I understand what has happened, and just need to write my code accordingly, which of course the solution I already discovered.

regards,
Dave
For by grace are ye saved through faith, and that not of yourselves: it is the gift of God: Not of works, lest any man should boast. -Ephesians 2:8-9