References, equals, and repeats (Was: Coerce string to strin
References, equals, and repeats (Was: Coerce string to strin
- Subject: References, equals, and repeats (Was: Coerce string to strin
- From: email@hidden
- Date: Thu, 14 Feb 2002 22:36:29 -0500
On Thu, 14 Feb 2002 08:07:15 -0800, Paul Berkowitz <email@hidden> asked,
On 2/14/02 6:14 AM, "has" <email@hidden> wrote:
>
> References are so damn
>
> schizophrenic in behaviour- definitely not AppleScript's finest moment.
>
I agree. I wonder what the rationale or justification for that was meant to
>
be. I imagine it must have been decided that one single tool - the 'equals'
>
operator - had to be kept 'pure' for such situations as when one truly
>
needed to discriminate between reference and object, but that all others
>
(well almost all others) could be coerced in a user-friendly, "English-like"
>
fashion. It certainly is confusing when such inconsistencies are allowed to
>
rule.
Most languages seem to have this same strict definition of equality. For
example, Java and Javascript both consider two reference types to be equal only
if they refer to the same object. C certainly doesn't think &x == x (except
coincidentally). Visual Basic only allows "=" for numbers and strings, but
provides "Is" as a "refers to the same object" strict equality operator.
The real pitfall isn't the strict behaviour of "equal" by itself, though. I
think if a programmer wrote,
set b to a reference to a
if a equals b ...
it would not be surprising that the reference isn't the same as the value.
Also, if you think about the AppleScript language mostly as a way to operate on
application objects, value-based comparisons don't seem natural.
if document "My Novel" is front document...
In such a case, you really want this to be based on whether they refer to the
same document, not if the two documents have the same contents.
The real booby trap is the behavior of the "repeat with x in L" statement, and
its use of reference values. What catches everyone is using "equals" in a
repeat block like this:
repeat with x in L
if x equals "Target" then display dialog "Found It"
end repeat
Which, of course, never succeeds, because X is the reference form, 'item 1 of
{"K-Mart", "Target", "Wal-Mart", "Sears"}.' What is needed is 'if contents of x
equals "Target"...'
Why, then, does the repeat loop do this wacky reference stuff? So you can act
on the item of the list through the iteration variable!
repeat with x in L
if contents of x equals "Target" then set contents of x to "Enron"
end repeat
L
--> result: {"K-Mart", "Enron", "Wal-Mart", "Sears"}
So, both useful features, but put them together and they trip up everyone.
--
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.