enumerated types: best way to fake them?
enumerated types: best way to fake them?
- Subject: enumerated types: best way to fake them?
- From: email@hidden (Michael Sullivan)
- Date: Fri, 1 Nov 2002 15:34:23 -0500
- Organization: Society for the Incurably Pompous
My statement here assumes that there is not enumerated type support in
AS, but I don't know that this is accurate. All I know is that searches
on enum, enumerate, etc. in the ASLG failed, and I don't *know* how to
do it already.
What I'd like is the ability to have a variable that can take one of
three values, and would be treated by AS in the same way that enumerated
values of app objects are.
So I'd like to be able to do something like:
set fruit to apple
eat(fruit)
on eat(food)
if food = apple
display dialog "Not bad but a bit mealy"
else if food = banana
display dialog "I fling feces at you."
else if food = orange
display dialog "Ah! Refreshing!"
end if
end eat
I realize I could do this with strings, but I'm hoping there's a better
solution. Especially one where I can restrict at definition time the
possible values of fruit to a few specific things -- in a way that
leaves the resulting code more, rather than less, readable.
One path I've considered is to declare each possibility as a boolean
true. the problem then is with function passing. to make it work, I
have to use a bunch of references and the code looks like:
set fruit to a reference to orange
eat(fruit)
on eat(food)
if food = (a reference to apple) then
display dialog "Not bad but a bit mealy"
else if food = (a reference to banana) then
display dialog "I fling feces at you."
else if food = (a reference to orange) then
display dialog "Ah! Refreshing!"
end if
end eat
This is actually kind of cool, because it (surprisingly at first), works
just fine without ever defining apple, banana or orange. But all those
"reference" lines make it perhaps not significantly better than using
strings. also, the fact that I don't have to define each term means
that there's not real advantage over using strings. It is kind of cool
that you can do this though...
I also considered the idea of trying to use records with labels of each
fruit with boolean values, but that seemed even more convoluted than
this and possible to mess up by having multiple fruits set to true.
Another possibility would be a script object, with properties with a
bunch of handlers for setting the values. I haven't fleshed that one
out yet, but my gut says there's not going to be a way to do what I'm
looking for without a fair bit of hassle, and client code that is
nowhere near as simple as the pseudocode above that I'd like to have it
look like (and would -- if I defined the values in an osax or app and
used a tell block).
Sigh. I know I should just use strings, but I can't help myself.
Working with lisp a bit lately is making me yearn for a symbol type in
AS.
Has anyone come up with a better idea for this sort of thing? Or do you
just use integer flags or strings. What I'm really hoping is that
someone will tell me that there *is* enumerated type support in AS (even
C style would be okay), and I just didn't know where to look to find the
syntax.
Anyone got any comments at all on the subject?
Michael
--
Michael Sullivan
Business Card Express of CT Thermographers to the Trade
Cheshire, CT 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.