Initializing local properties

Richard Gaskin ambassador at fourthworld.com
Wed Apr 15 12:11:50 EDT 2009


David Bovill wrote:

> 2009/4/15 Richard Gaskin wrote:
>> Why are globals so unloved?
>>
>> Most programming languages support them so they can be accessed globally.
>>  If you need global access, why not use a global? 
> 
> True - some of my allergy to them is not rational :) In this case I'm trying
> to speed up the script so the behaviors of the "view library" I'm working
> on. I have about 50 views at the moment, which I'm going through and
> tidying. Each "view" has a complex geometry / behavior script which refers
> to controls within the view - on average maybe 4 controls for each view. I'd
> expect the library will eventually have a few hundred widgets in - using
> globals would mean creating say 800 global variables or I guess an array
> with 800 entries. Then I'd have to make sure there were no name clashes...
> etc

I wonder if v3.5's behaviors would help with that. One script, one set 
of var declarations, instantiated as needed for every object that uses it.

> But most of all it does not solve the problem of initializing the variables.
> I don't want to create a complex mechanism that loads and maintains this
> stuff for every script, writing preopen card handlers and getting errors
> every time I miss one out. I want to define them in the context of the only
> place they are used - the script of the behavior - and I want them to be
> initialised when and only they are used - simply by me defining them in the
> script.

While we wait for the ability to initialize vars from a function call, a 
single Init handler in a behavior script may do the trick for now.

If you used a script-local var as a flag to see if it's been 
initialized, you could ensure it's initialized only once even if called 
from a preOpenCard handler:

local sInited

on preOpenCard
   if sInited is empty then
     -- do initialization stuff
     put true into sInited
   end if
   pass preOpenCard
end preOpenCard


--
  Richard Gaskin
  Fourth World
  Revolution training and consulting: http://www.fourthworld.com
  Webzine for Rev developers: http://www.revjournal.com



More information about the use-livecode mailing list