Re: POSIX and lists question
Re: POSIX and lists question
- Subject: Re: POSIX and lists question
- From: has <email@hidden>
- Date: Fri, 12 Aug 2005 16:45:08 +0100
Gary (Lists) wrote:
> > script a
> > property x : 2
> >
> > on foo()
> > return x
> > end foo
> > end script
> >
> > script b
> > property z : 0
> >
> > property foo : a's foo
> > end script
> >
> > b's foo() --> 0
>
>
>It's clear that b has no such handler 'foo()' (even if one of its properties
>might), but it does have a property named 'foo'.
Incorrect. A script object is just a set of named slots, each containing an object. The implementation is completely uniform, and since all slots allow read+write access you can replace one value with any other value at any time. The only difference is in the syntax used to declare their initial values: 'on <name> ...' for handler objects, 'script <name> ...' for script objects, 'property <name> ...' for any other object type.
>If you are suggesting that AppleScript should return something useful from
>b's foo(), then I'm glad that it doesn't.
It's worse than that: it returns the _wrong_ value. I wouldn't mind if AS treated handlers as a special case, preventing users from assigning or replacing them, since it would eliminate certain gotchas [1] and make the language a bit simpler conceptually. I wouldn't mind if it made them full closures, so that folk like Mark and Matt could write more elegant code when they want to. But AppleScript does neither of these things, instead leaving the issue flapping in the wind. This is a pain for users as it means they need to know about it in order to avoid the bugs it causes without actually being able to do anything useful with it; a complete waste of their time and energy.
>And are these _real_ constructs in people's scripts?
Not in AppleScript because, as I've pointed out, the behaviour is both undocumented and broken. But it's a common idiom in languages that implement first-class function objects/function pointers correctly, and in some (e.g. Smalltalk) it's ubiquitous.
For example, Python lists provide a sort() method that optionally takes a function object that performs comparisons between the items being sorted:
#!/usr/bin/python
people = [
{'first name': 'Jane', 'last name': 'Smith'},
{'first name': 'John', 'last name': 'Brown'},
{'first name': 'Jan', 'last name': 'Smith'},
]
def sortByName(a, b):
return cmp(a['last name'], b['last name']) or cmp(a['first name'], b['first name'])
people.sort(sortByName)
print people
Look up 'closure' and 'callback' on wikipedia.org if you want to learn more.
You can achieve the same end in AppleScript (e.g. see the powerSort command in AppleMods' List library) but you have to pass a script object containing a handler under a pre-determined name, which is rather less compact and elegant.
has
[1] e.g. Classic example:
on foo()
set foo to 3
beep foo
end foo
foo() --> beeps 3 times -- as expected
foo() --> Error: script doesn't understand the 'foo' message -- ouch!
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
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