• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: "a reference to"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "a reference to"


  • Subject: Re: "a reference to"
  • From: has <email@hidden>
  • Date: Wed, 23 Jan 2008 16:14:09 +0000

On 22 Jan 2008, at 21:41, Scott Babcock wrote:

(I failed to set the correct subject when I sent this the first time...)

No, I mean a reference to *any* object.

OK, not important.


The word "reference" is far too overloaded for my taste.

AppleScript has a 'reference' data type, but it appears also to play with references of another sort, one that it hides.

No, there's only one reference type. The confusion comes from how and when AppleScript constructs and resolves references, since that does differ depending on whether they point to application objects or AppleScript objects.


Here's the example again:

set theList to {{foo:"foo"}}
repeat with itemRef in theList

   set recordRef to (contents of itemRef)
   set foo of recordRef to "ref"
   set localItem to (contents of recordRef)
   set foo of localItem to "local"
   return {itemRef, recordRef, localItem}

end repeat

--> {item 1 of {{foo:"ref"}}, {foo:"ref"}, {foo:"local"}}

* recordRef is a reference to the record that's the first item of theList. This is demonstrated by the fact that the change to the [foo] property of recordRef is reflected in theList.
* localItem is merely a copy of the original record. Changes to its [foo] property don't affect either theList or recordRef.

- The itemRef variable is bound to a reference to item 1 of the list.

- The recordRef variable is bound to the {foo:"foo"} record.

- The localItem variable is bound to a copy of the original {foo:"foo} record. Apparently, asking for the contents of a list or record performs a shallow copy [1][2].

Examples:

	set var1 to {x:1, y:2}
	set x of var1 to 3
	log var1 --> {x:3, y:2}
	log var2 --> {x:3, y:2}


set var1 to {x:1, y:2} set var2 to var1 set x of var2 to 3 log var1 --> {x:3, y:2} log var2 --> {x:3, y:2}


set var1 to {x:1, y:2}
set var2 to contents of var1 -- this shallow-copies the object bound to var1
set x of var2 to 3
log var1 --> {x:1, y:2}
log var2 --> {x:3, y:2}



From this behavior, I infer that list items with non-primitive data types (lists, records, application objects) are stored in the list as references. This makes sense, since it means that complex items in the list are no more difficult to manage than primitive items. However, these references are apparently not presented as 'reference' objects; they seem to be of some internal data type.


Incorrect inference (see above), though you have my sympathies (it took me several years and many wrong turns to figure out how AppleScript works to a reasonable level of accuracy).


BTW, and just to be sure: don't confuse the specific behaviours associated with AppleScript's reference type with the normal behaviours you see in any object-oriented language, i.e. stack- allocated objects and internal pass-everything-by-reference semantics. You don't - and shouldn't - need to think down to that level to understand user-facing behaviours. As far as users are concerned, all values are objects, and variables are simply names that can be bound to objects; c.f. Smalltalk, Python, etc. Thus, for example, in the following script var1 and var2 both point to the same object:


	set var1 to {1, 2, 3}
	set var2 to var1

whereas here var1 and var2 point to different objects (the 'copy' command deep-copies an AppleScript object):

	set var1 to {1, 2, 3}
	copy var1 to var2

HTH

has

[1] With the one exception that if the record contains a 'contents' property it returns the value of that property instead.

[2] I can't be bothered trawling the ASLG to check if this is documented behaviour or not. Personally I would prefer an exception was raised - either way, the current behaviour is obscure and a potential source of confusion - but, hey, this is AppleScript after all.
--
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org


_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
References: 
 >RE: "a reference to" (From: Scott Babcock <email@hidden>)

  • Prev by Date: Re: How to parse CSV
  • Next by Date: Re: How to parse CSV
  • Previous by thread: RE: "a reference to"
  • Next by thread: RE: "a reference to"
  • Index(es):
    • Date
    • Thread