handler local variables

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

handler local variables

Post by sturgis » Thu Aug 16, 2012 8:58 pm

Ok, I have a question that didn't occur to me before. If a variable is declared script local, it is available to all handlers and functions in that script in a persistant state.

If it is declares as a handler local variable, it does NOT seem to be persistant.

So first question, why bother declaring it as a local if its not persistant between handler calls?
Is there a way to use a variable that is persistant between handler calls that is only accessible by that handler?

I assume this is normal behavior, it just doesn't seem normal to me so i'm hoping someone can explain why it is this way.

TIA!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: handler local variables

Post by bn » Thu Aug 16, 2012 9:23 pm

Hi Sturgis,

declaring handler local variable is not necessary when you just use them. Declaring handler local variables, also called temporary variables as in "not persistent", is a means to write clean code. If you turn on the strict compilation mode in the preferences you have to declare handler local variables and the compiler barks if you us a undeclared variable. It is also a means to avoid typos in variable names that can be hard to track down.
But you probably know all this.
Is there a way to use a variable that is persistant between handler calls that is only accessible by that handler?
I don't know your use case for that but you can trick your way around:
A script local variable declared at the top of a script is accessible by all handlers. If you declare a script local variable later, say just above handler 6 of 10 in the script only handler 6 to 10 can access the script local variable. Handler 1 to 5 can not access that script local variable.
Now if you make the handler that should have unique access to a persistent script local variable then make that handler the last handler in the script and declare the script local variable right above that handler.

For whatever that may be good

Kind regards
Bernd

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: handler local variables

Post by sturgis » Thu Aug 16, 2012 9:31 pm

Hey, thats a neat trick, thanks! As far as setting explicit vars, yep did know that. As far as my use case.. well I have never declared a local in a handler but was looking at code today and my brain kicked me in the shins about this until I finally asked the question. I'll surely file the trick away for future potential use!
bn wrote:
I don't know your use case for that but you can trick your way around:
A script local variable declared at the top of a script is accessible by all handlers. If you declare a script local variable later, say just above handler 6 of 10 in the script only handler 6 to 10 can access the script local variable. Handler 1 to 5 can not access that script local variable.
Now if you make the handler that should have unique access to a persistent script local variable then make that handler the last handler in the script and declare the script local variable right above that handler.

For whatever that may be good

Kind regards
Bernd

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: handler local variables

Post by jacque » Fri Aug 17, 2012 6:29 am

I know that placement trick works on LiveCode server but I didn't think it applied to regular stack scripts. I'm not at a computer right now where I can test it though. Does it really work universally? If no else does, I'll try to remember to try it myself tomorrow.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: handler local variables

Post by bn » Fri Aug 17, 2012 9:06 am

Hi Jacque,
you might want to try this

Code: Select all

on mouseDown
   put sLOCAL && the milliseconds
end mouseDown

local sLOCAL = "warm weather"
on mouseUp
   put return & sLOCAL && the milliseconds after msg
end mouseUp
Kind regards
Bernd

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: handler local variables

Post by sturgis » Fri Aug 17, 2012 2:40 pm

That is just too cool! (ok, so i'm too easily amused) but yes indeed it does function this way.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: handler local variables

Post by jacque » Sat Aug 18, 2012 4:48 am

Thanks for confirming, good to know. A new trick. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply