Re: Variable Ponderance
Re: Variable Ponderance
- Subject: Re: Variable Ponderance
- From: email@hidden
- Date: Fri, 1 Oct 2004 11:14:19 -0400
At 9:34 PM +0100 9/30/04, has wrote:
Ben Waldie wrote:
I think I have noticed this before, but just never questioned it.
In the following examples, I set a local variable to a value, and then
pass that variable down to a handler as a parameter.
set x to {1, 2, 3}
doSomething(x)
You're not passing a variable (x). You're passing the object that's
stored in it.
on doSomething(y)
set item 1 of y to 2
end doSomething
And here you're not modifying the variable (y), you're modifying the
content of the object that's stored in it. Which is the _same
object_ as is stored in variable x.
Ech, this one bites everybody sooner or later. (Not helped by the
ASLG's lame attempt to explain it - some vague rubbish about "data
sharing" buried somewhere in the middle). Short answer: follow the
objects, not the variables.
The way I like to say this (I think it's equivalent) is that some
objects (anything other than a number IIRC) are never stored in
variables -- only a reference (pointer if you prefer, but reference
is more general) is stored in the variable binding.
So when you pass the variable, you are passing just a reference to
the object, not the object itself. Think of objects as a wiki
webpage, when you set a variable to them, what gets stored in the
variable is a URL to that page (an address). There are then some
functions you can perform that will parse said URL and use the
information to read or make changes to the page.
Now, if you wrote something like
set x to [URL: "http://foo.com/wiki/"] -- assume this is syntax for URLs
displayPage(x)
doSomething(x)
displayPage(x)
on doSomething(y)
wikiAdd(subversiveComment, y)
end doSomething
You would not expect the wiki to remain unchanged simply because y
was a local variable.
Footnote: There's really only two key concepts you need to get to
understanding how AppleScript works: objects and messages. I'd
suggest googling for an introduction to the basic principles of the
Smalltalk language, which is all about creating objects and sending
messages between them. AppleScript nicks most of its good ideas from
Smalltalk, so if you can understand how Smalltalk operates then
AppleScript will make much more sense to you.
The behavior that is described here is remarkably similar to what
happens in most lisps. I had a really hard time wrapping my head
around the idea when I first got introduced to lisp in college, but
after working with applescript for a while, I've got used to it. Now
that I've done some lisp again, I don't mind it anymore, and can see
the point to working this way.
I'm pretty sure that applescript actually got many of it's good ideas
(including this one) from lisp through Dylan, a lisp dialect with
more traditional (infix math, no parens) syntax that Apple did a lot
of work on in the early 90s, concurrently with a lot of applescript
development. I'd be shocked if there wasn't some cross-pollenation
there. I'd expect most of the Dylan folks to have been familiar with
Smalltalk as well. The general idea of strong but dynamic typing is
common to both Smalltalk and Dylan, and I consider it one of the best
things about AS (though I'd like either the ability to subvert it, or
a suite of fast tools comparable to lisp's).
Michael
--
Michael Sullivan 613 West Johnson Avenue
Cheshire, CT 06410
Business Card Express 800-962-4030
Thermographers to the Trade email@hidden
_______________________________________________
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