Page 1 of 1
Strict Compilation Mode
Posted: Thu Aug 04, 2011 11:31 pm
by jharris
Hi everyone,
If I have strict compilation mode on I keep getting "local: name shadows another variable or constant" as an error when I compile.
Is this keeping me from using the same local variable names in multiple handlers?
I usually declare a local variable (tCon) for my database connection ID.
Any help would be much appreciated.
Thanks,
jharris
Re: Strict Compilation Mode
Posted: Thu Aug 04, 2011 11:41 pm
by bn
Hi jharris,
you probably declared a variable with the same name twice. This also happens without strict compilation mode if you declare your variables.
If you declare your variable as a script local variable, i.e. a variable outside of any handler, usually at the top of the script and you then declare the variable inside a handler you would also see this compilation error. If you declared your script local variable (the one outside of any handler) it is not necessary nor possible to declare it inside a handler again.
If you are not using script local variables but just local variables (the ones declared inside a handler and only accessible to this handler) and you get the error then you probably try to declare that variable twice.
I hope this makes sense
Kind regards
Bernd
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 4:00 am
by jharris
Hi Bernd,
Thanks for replying. I am sure I don't have any duplicate local variables within the same handler. I have only one script local variable in a card script.
I had to start changing variable names with each handler to something unique and the script would compile without errors. I eventually
created some more handlers and the errors started again. I was in my office and finally copied the project to a USB drive and went home.
When I got home, I opened the project and haven't had any errors since I started working. I've been working for about two hours and no
problems so far.
I had been working in the LiveCode IDE steady for about 10 hours. I wonder if the problem could have been within the IDE. I think the next time
it happens I will close the IDE and reopen the project and see if that helps.
Regards,
JHarris
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 5:04 am
by BarrySumpter
Hi j,
At home, did you have strict compile turned on?
May be the difference between preferences at home and work.
For my style of coding I feel I make faster progress with it turned off.
Maybe scoping issues.
Script level, Global level, and local level
LOL, nope its a bug.
Code: Select all
-- Global x -- script level
-- Local x -- script level
on mouseUp
local x
end mouseUp
on mySubRoutine
local x
end mySubRoutine
FIrst time apply all was well.
uncommented script level Local x
error on mouse up local x
re commented local x
all ok again
uncommented script level GLOBAL x
error on mouse up local x
recommented global x
same error - i think this is a bug or at least where the problem lies
started a new stack
added button and with mouse up local x only
same error
Probably one of the reasons I have it turned off.
LOL
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 7:37 am
by SparkOut
J, I wonder if you have any global variable defined with the same name in any other card or stack? Even if the global is defined in a stack from an unrelated project, if it is in memory then it will be *globally* available. Closing the IDE and reopening may well be a good idea, as you suggested, to make sure that everything is purged from memory that should be.
@Barry, that is not a bug at all. If you try to define a script local variable *and* a local variable within a handler, if the IDE allowed it to compile then when your handler runs, how would the engine know which context to use for "put 1 into theVariable" - should that be the script (or global) scope, or the local (within handler) scope?
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 8:00 am
by BarrySumpter
If someone was having issue swith scoping I'd suggest
Global gTaxNo
Local sTaxNo
Local lTaxNo
But it would suprise me if someone using LiveCode hasn't already sorted out prefixing for scope.
If that global causes an error.
Then I remove that global and the error remains?
Thats not a bug?
Having to close the ide and restart sounds like a workaround to a bug to me.
Too many queries for something I don't use.
Too many errors severely restricts my productivity with unnecessary distractions.
Someone smarter that me will have to put up with it.
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 8:35 am
by bn
Hi Barry
If that global causes an error.
Then I remove that global and the error remains?
Thats not a bug?
I reproduced what you describe: if strict compilation mode is turned on then uncommenting and recommenting the global var declaration still does not let me compile. Turning strict compilation mode off again restores the expected behavior (without restart).
Mind you once you create a global variable it stays until you quit Livecode. So it may be that strict compilation mode errs on the safe side and remembers that. It is probably a matter of debate whether that is a bug or not.
If someone was having issue swith scoping I'd suggest
Global gTaxNo
Local sTaxNo
Local lTaxNo
I also prepend my variables with a letter indicating their scope. Helps to read scripts.
you might want to have a look at Richard Gaskins (FourthWorld) insightful thoughts on this. Many follow the principles he describes.
http://www.fourthworld.com/embassy/arti ... style.html
Kind regards
Bernd
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 9:19 am
by BarrySumpter
Thanks for the confirmation bn.
I've read the supurb content in Richard's article many times
and its on my list of monthly readings that I never seem to get to.
I'm pretty close with most of the suggestions and not stressing the ones I'm not.
Thanks for the support everyone.
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 9:43 am
by bn
One thing to have in mind regarding:
script local variables and local variables
if you declare a script local variable later in a script and not at the top of the script it is only available to the handlers that follow the declaration of the script local variable. Can be confusing if you dont declare them at the top of the script.
Example:
Code: Select all
on mouseUp
local x
end mouseUp
Local x -- script level
on mySubRoutine
local x
end mySubRoutine
without strict compliation mode only the second occurence of "local x" is flagged as "shadowed" since the first is not in the scope of the script local variable. If you turn on strict compilation mode the first one is also flagged as "shadowed".
The main point here is declare your script local variables at the top of a script, not later. Else it gets confusing.
Kind regards
Bernd
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 12:22 pm
by jharris
Thanks for the replies everyone,
I am sure I've had no variables with the same name within any handler. I have one script local variable (sDepts) and 3 global varaiables (gName1, gName2, gName3).
I always declare my variables at the top of the handler or script for script local.
Barry, I had Strict Compilation on at home also. But I didn't experience the error I was getting at work.
When the errors started I had declared variables in handlers like this:
Code: Select all
-- Start of script
-- No globals
-- one script local variable
local sCurDeptName
on GetDeptsData
local tCon, tSql, tData
local tDeptKey, tDeptName, tDept
--Code below here
put OpenDatabase() into tCon
-- More code below to put data in Data Grid
end GetDeptsData
on GetUsersData
local tCon, tSql, tData
local tUserKey, tUserName
--Code below here
put OpenDatabase() into tCon
-- More code below to put data in Data Grid
end GetUsersData
-- End of script
Ok, that is just a quick example. As you can see tCon, tSql and tData are used more than once, but not within the same handler.
The compiler complained about the first variable (tCon) shadowing another in the first handler. I changed tCon to tCon1. Then the compiler
complained tCon is not declared, which is expected behavior. I changed all tCons to tCon1. Click the compile button and the
compiler complains (tSql) shadowing another variable, so I rename it to tSql1 and all references to it. Then the compiler
complains about the next variable in line (tData). After I do the renaming everything seems ok.
I am planning on working with LiveCode most of the day. (Getting ready to ride motorcycles with friends for a few hours.) If the errors
start again today, maybe I can post the script for everyone to look at. Maybe someone can spot something wrong from the example
I posted.
Not sure what is happening, but it is a pain in the... well you know.
Thanks everyone for the help and comments.
Regards,
jharris
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 12:49 pm
by Dixie
Hi...
Why keep declaring your variables ? Just put them at the top of the script.
Code: Select all
local sCurDeptName,tCon, tSql, tData,tDeptKey, tDeptName, tDept,tUserKey, tUserName
on GetDeptsData
put OpenDatabase() into tCon
-- More code below to put data in Data Grid
end GetDeptsData
on GetUsersData
put OpenDatabase() into tCon
-- More code below to put data in Data Grid
end GetUsersData
Does this help ?
Dixie
Re: Strict Compilation Mode
Posted: Fri Aug 05, 2011 2:07 pm
by jharris
Dixie wrote:Hi...
Why keep declaring your variables ? Just put them at the top of the script.
Dixie
Because I'm a LiveCode newbie... hadn't thought about that.
Thanks for the suggestion. It would probably help.
jharris