Re: Modifications and Variables
Re: Modifications and Variables
- Subject: Re: Modifications and Variables
- From: Axel Luttgens <email@hidden>
- Date: Mon, 06 Jan 2003 11:39:53 +0100
(Preliminary note: this thread has become rather convoluted, and we are
at the risk to soon face a "Tragedy of Errors" - (c)Paul Skinner).
Paul Berkowitz wrote:
>
On 1/5/03 11:49 AM, "Axel Luttgens" <email@hidden> wrote:
>
>
>
>>The muddy area is the statement that all top-level variables are global. In
>
>>fact, they're not global unless they're explicitly declared global, either
>
>>at the top level itself, or in individual handlers as you've been doing.
>
>>
>
>No, I really believe they are globals, as any other explicitely declared
>
>global.
>
>By writing statement 'set x to 3' at the script's top level, without any
>
>'property x...' or 'global x' statement, I am really declaring, creating and
>
>initializing a global variable.
>
>This would be the "first valid occurence" of identifier 'x', hence the
>
>declaration.
>
>And that variable exists, without needing to be referenced in another handler
>
>through a 'global x' statement.
>
>But the fact that I may reference it from within a handler through such a
>
>statement just means it is a global.
>
>And because it exists and is a global, it will persist.
>
>
>
>Now, this is just (what I hope to be) a logical deduction, without any
>
>technical background, and I could of course be completely wrong.
>
>Perhaps do you have a reference that establishes a true/technical difference
>
>between a genuine global and a "semi-global"?
>
>
>
>>
>
>>
>
>
>
Script A:
>
>
---------------------
>
global x
>
>
set x to 3
>
>
myHandler()
>
>
on myHandler()
>
display dialog (x as string)
>
end myHandler
>
>
--------------------
>
>
>
Script B:
>
>
---------------------
>
>
set x to 3
>
>
myHandler()
>
>
on myHandler()
>
display dialog (x as string)
>
end myHandler
>
>
--------------------
>
>
>
>
--ERROR: The variable x is not defined.
>
>
>
If x is a global, then why is it undefined?
>
>
I would guess that you usually use top level properties instead of
globals for storing data to be shared throughout your scripts, don't you?
Allow me to start from your example scripts and to agrement them with
some comments, then to present some variants:
-- Script A
global x -- declare a global
set x to 3 -- (create and) initialize that global
myHandler()
on myHandler()
x as string -- declare (and create) and use
-- a variable local to myHandler
--> x is undefined
end myHandler
-- Script B
set x to 3 -- declare (and create) and initialize a global
myHandler()
on myHandler()
x as string -- declare (and create) and use
-- a variable local to myHandler
--> x is undefined
end myHandler
In THE ABSENCE OF ANY PROPERTY (that could have been called x too),
scripts A and B are equivalent; the declaration and initialization are
just split in script A.
(note that in the following, I will allways assume the ***absence*** of
properties [1]).
The same way, script B is equivalent to:
-- Script B bis
set x to 3 -- declare (and create) and initialize a global
myHandler()
on myHandler()
local x -- declare a variable local to myHandler
x as string -- try to use that uninitialized
-- local variable
--> x is undefined
end myHandler
That is, an identifier declared in a regular handler (by contrast to an
explicit run handler) is by default local.
Unless one uses a 'global' statement:
-- Script B ter
set x to 3 -- declare (and create) and initialize a global
myHandler()
on myHandler()
global x -- declare (and link) a global
x as string -- use that global's current value
end myHandler
--> "3"
This is just an application of the scoping rules as defined by the
language (back to this thread's previous posts ;-) )
And my initial point was that those rules make me think that even
following (extremely short) script:
-- Script C
set x to 3
indeed defines, creates and initialize a genuine global variable. So
that, according to other parts of the language definition, that global
HAS TO BE persistent (once instantiated/initialized).
Thus, and please excuse me in advance for my tenacity, my question
again: in what sense would global variable x in Script C have a special
status?
Axel
[1] One seemingly confusing behavior is the one of a property declared
at a script's top level.
Such a property may indeed have a greater visibility than a global
(unless hidden by a 'local' or 'global' statement) and show the behavior
you were expecting in Scripts A and B:
-- Script D
property x: 3 -- declare (and create) and initialize a property
myHandler()
on myHandler()
x as string -- use the value of existing property
end myHandler
--> "3"
but this is conditioned by lexical scoping:
-- Script D bis
myHandler()
on myHandler()
x as string -- declare and use an uninitialized local
--> variable x is undefined
end myHandler
property x: 3 -- declare (and create) and initialize a property
or may be hindered by an explicit declaration:
-- Script D ter
property x: 3 -- declare (and create) and initialize a property
myHandler()
on myHandler()
local x -- declare a variable local to myHandler
x as string -- try to use that uninitialized
-- local variable
--> variable x is undefined
end myHandler
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.