• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: *Very* strange script / global variable behavior
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: *Very* strange script / global variable behavior


  • Subject: Re: *Very* strange script / global variable behavior
  • From: Neil Faiman <email@hidden>
  • Date: Sun, 13 Mar 2005 21:19:23 -0500

I have appreciated and enjoyed the lengthy discussion that my rather baroque problem has spawned. I'd like to make a couple of follow-up comments, though.

First, I'm well aware that my examples, as posted, were useless at best. They were the result of distilling some unexpected behavior in a substantial AppleScript Studio program down into a minimal reproducer. Minimal reproducers, by their nature, tend to be useless and odd-looking, since their only reason for existence is to illustrate unexpected behavior, usually in code at the language fringes (since the unexpected behavior has usually been fixed in the more mainstream constructs.)

Second, I appreciate that this simply isn't going to work as I expected, and I've cheerfully recoded, along lines suggested by contributors to this discussion, to make it work.

But, third, I'm still convinced that this ought to be considered an AppleScript bug. I understand that Matt Neuburg's _Definitive Guide_ is not, in fact, truly definitive, in the sense that he is not writing on behalf of Apple, and is simply attempting to describe AppleScript as it actually behaves, rather than presenting the formal or official language definition. None the less, it is the closest thing that I have seen to a comprehensive explanation of the rules that AppleScript works by, so my inclination is to treat it as definitive.

To recap, here is my (third and simplest) example program:

	property theState : 0

	script Inner
		on showTheState()
			display dialog "State " & theState
		end showTheState
	end script

	script Outer
		Inner
	end script

	set x to Outer

	set theState to 1
	tell Inner to showTheState()
	set theState to 2
	tell Inner to showTheState()

Applying the rules from "Scoping of Variables" in Chapter 7 of _The Definitive Guide_, we can start with the one unequivocally accurate one: "Scoping of variables in AppleScript is extraordinarily complicated". But progressing to specifics:

1. "A variable declared global is visible subsequently in the same scope as the declaration, and within all handlers and scripts defined subsequently in the same scope, to an infinite depth." ("Global Declarations: The Downward Effect", p. 123).

2. A script property is a script-level global variable with initialization. ("Script Properties", p.131).

3. "A property is a kind of global variable, and a property declaration has the same downward effect as a global definition." ("Scoping of Properties", p. 132).

4. "An entity defined outside a handler or script object but globally visible within it, and not overshadowed by a declaration of the same name, is called a 'free variable' with respect to that handler or script object. ... A free variable takes its value within the handler or script object at the time the code is run, not at the time the handler or script object is defined." ("Free Variables", p. 129).

By those rules, theState is a property, which is a kind of global variable, and its declaration has the same downward effect as a global definition (rule 3), which is to say, it is visible within all handlers and scopes defined subsequently (i.e., following its declaration) in handler showTheState of script Inner (rule 1). Since it is defined outside handler showTheState of script Inner but globally visible within it, it is a free variable, and therefore it takes its value within showTheState at the time that showTheState is run.

But obviously it doesn't.

The fact that the behavior varies with whether theState is declared as a property or a global, and with whether you add a "global theState" declaration within showTheState, neither of which ought to have any effect, just goes to reinforce the impression that this behavior is simply a bug, not a particularly obscure language feature.

Regards,

	Neil Faiman

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: *Very* strange script / global variable behavior (From: Paul Berkowitz <email@hidden>)

  • Prev by Date: Found records count in Filemaker 6/OS X
  • Next by Date: Re: Scripting Energy Saver to Sleep Display Now?
  • Previous by thread: Re: *Very* strange script / global variable behavior
  • Next by thread: Re: *Very* strange script / global variable behavior
  • Index(es):
    • Date
    • Thread