Re: enumerated types: best way to fake them?
Re: enumerated types: best way to fake them?
- Subject: Re: enumerated types: best way to fake them?
- From: has <email@hidden>
- Date: Sat, 2 Nov 2002 12:36:39 +0000
Michael Sullivan wrote:
[on DIY enumerated types]
>
Sigh. I know I should just use strings, but I can't help myself.
Not at all; if an enumerated type is what you need then that's what you
should use. AS may not have them built in, but you can emulate them just as
well, no guilt attached;).
Paul Berkowitz wrote:
>
property apple : 1
>
property orange : 2
>
property banana : 3
Gets my vote. (Tip: properties are also good for DIY named constants.)
To make your pseudo-enumerated types more robust, I'd recommend using
records rather than integers (or other basic types); eg:
property apple : {enumFruit:1}
property orange : {enumFruit:2}
property banana : {enumFruit:3}
Unlike integers, a record like {enumFruit:n} isn't going to be used for any
other purpose, avoiding problems if an inappropriate value is passed by
accident; eg:
[using integers]
eat(1) --> "Not bad but a bit mealy" -- oops!
[using unique record]
eat(1) --> [Nothing] -- expected result
Pretty much foolproof. Note that the speed difference is trivial, so isn't
a worry.
Lastly, some DIY House Rules to ensure your DIY types continue to run
smoothly...:)
1. Even though AS doesn't allow properties/values to be declared immutable,
your scripts should not attempt to modify these properties or their
contents.
2. Repeating the same literal values elsewhere in your script defeats the
purpose of the exercise, so always remember to refer to values by name.
3. Add some comments to your property declarations so anyone reading your
code knows what's going on (not all ASers have heard of enumerated types,
or may not realise you're emulating them here).
HTH
has
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
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.