Re: script object property contamination
Re: script object property contamination
- Subject: Re: script object property contamination
- From: email@hidden
- Date: Thu, 14 Feb 2002 22:09:27 -0500
On Thu, 14 Feb 2002 14:14:56 +0000, has <email@hidden> wrote,
>
Handlers are objects in AS (just like everything else), so can be
>
manipulated as such. What you do with AS objects is your own business, but
>
when something breaks that's the AS team's business. I do recommend putting
>
in a bug report - it can't do any harm, and might do some good.
[...]
>
>As far as I know AppleScript is not statically scoped but it is a dynamic
>
>bound language. It's not like Java where at compile time the compiler looks
>
>for the object and method you desire to invoke (if neither exists Java will
>
>throw a compiler error).
>
>
There's elements of both. A trawl through the archives would no doubt throw
>
up more than a few gems c/o Scott Norton on this subject. [Wondering where
>
Scott is right now anyway... this sorta thread should be right up his
>
street.]
I've been quiet because I'm suspicious about the whole use of handlers as
dynamic, assignable values. It smells to me like a partially implemented thing.
Its not documented, and it only works for certain circumstances. It makes me
think that the detailed issues of scoping have not been fully thought out, and
unexpected behavior (like the bug in question) is likely. Bug reports would
probably be ignored. At best, its an undocumented feature, and might change or
go away in the future. Perhaps its better called an accidental feature, and may
accidentally disappear in the future.
But more importantly, its just as effective to use a script object, and that
approach is documented by Apple, with the scoping rules described in detail.
Now, perhaps its a little more verbose to say,
script foo
on callback()
-- do the desired work
end callback
end script
[...]
set backhook to foo
[...]
tell backhook to callback()
Than to just say
on foo()
-- do the desired work
end foo
global backhook
set backhook to foo
backhook()
But I'd have to look at the application specifics more to say what the best way
to use a script object is. It might be that there's a better way than simply a
handler in a script object. If you want to provide a parametric function that a
script will call, it may be better to extend a script object with a child that
contains the desired function, and then use that script object to do the work.
Here is what I hope is a short example. We'll consider a script object that
alternates between two messages, and also takes a "transformation" function that
will change each message before it is returned. First, a non-script-object
implementation.
property msg : {"Hello", "Hola"}
property currentMsg: 1
global transformation
to getMsg()
set currentMsg to (currentMsg+1) mod 2 + 1
return transformation for item currentMsg of msg
end getMsg
on exampleTransformation for s
return (reverse of characters of s) as text
end exampleTransformation
set transformation to exampleTransformation
getMsg()
--> result: "aloH"
Next, a naive script object substitution
property msg : {"Hello", "Hola"}
property currentMsg: 1
global transformation
to getMsg()
set currentMsg to (currentMsg+1) mod 2 + 1
return product of transformation for item currentMsg of msg
end getMsg
script exampleTransformation
on product for s
return (reverse of characters of s) as text
end transformation
end script
set transformation to exampleTransformation
getMsg()
--> result: "aloH"
Finally, the truly object-oriented approach
script AltMessage
property msg : {"Hello", "Hola"}
property currentMsg: 1
to getMsg()
set currentMsg to (currentMsg+1) mod 2 + 1
return item currentMsg of msg
end getMsg
end script
script TransformMessage
property parent : AltMessage
to getMsg()
continue getMsg()
return (reverse of characters of result) as text
end getMsg
end script
tell TransformMessage to getMsg()
--> result: "aloH"
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden
_______________________________________________
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.