Skip navigation
This discussion is archived

Back to basics- reference data types and copy vs. set

1575 Views 11 Replies Latest reply: Apr 7, 2009 4:32 PM by stephen_42 RSS
Thomas Camilleri Level 2 Level 2 (310 points)
Currently Being Moderated
Apr 1, 2009 10:44 AM
My understanding is that lists and records are reference data types, which means that variables set to a list or record value contain not actual values but references to a literal record or list.

After running the below snippet, shouldn't variable B have a value of {1,2,3,4,5,6}? It's value is actually {1,2,3,4,5}.

set A to {1, 2, 3, 4, 5}
set B to A
set A to {1, 2, 3, 4, 5, 6}
B

This result would be expected if "set B to A" were to be replaced by "copy A to B", which code would create a copy of the literal {1,2,3,4,5} and place a reference to it in variable B. What am I missing?

Any insights appreciated.
G4 dual 1G
  • Bernard Harte Level 4 Level 4 (3,025 points)
    Neither lists nor records (which are lists with labels) are references to anything but their current data - unless, of course, you store a reference in them.

    In your example, when B gets set to A it takes the then current values in A and returns those in the last line. It is NOT affected by the subsequent change to the values in A.
    MacBook Pro, Mac OS X (10.5.6), High-Res 17" 2.5GHz 4GB
  • Camelot Level 8 Level 8 (45,670 points)
    If you do want to have B reference A, then say so:

    set A to {1, 2, 3, 4, 5}
    set B to a reference to A
    set A to {1, 2, 3, 4, 5, 6}
    B's items


    Now B literally points to A. If you 'return B' you'll actually get 'A', which is why there's the additional redirection to get 'B's items'.
    Mac OS X (10.5.6)
  • hhas Level 2 Level 2 (190 points)
    Thomas Camilleri wrote:
    My understanding is that lists and records are reference data types, which means that variables set to a list or record value contain not actual values but references to a literal record or list.


    I think you're getting confused by muddled jargon.

    1. All values in AppleScript are first-class objects. (Ditto in languages such as Smalltalk, Python and Ruby. Different to languages such as C, ObjC, and Java, where some fixed-size values, e.g. ints and floats, are stack-allocated primitives while other values are heap-allocated objects.)

    2. A variable is just a name, or identifier, bound to an object. You can bind one or more identifiers to a single object. You can also rebind an existing identifer to a different object, e.g.:

    set A to {1, 2, 3, 4, 5}
    set B to A
    set A to {1, 2, 3, 4, 5, 6} -- rebinds identifier A to a different object
    B
    --> {1, 2, 3, 4, 5}


    3. Some objects are mutable - i.e. you can modify their contents. Specifically dates, lists, records and script objects. Example:

    set A to {1, 2, 3, 4, 5}
    set B to A
    set end of A to 6 -- modifies the contents of an existing object
    B
    --> {1, 2, 3, 4, 5, 6}


    4. 'copy' makes a deep copy of an existing object, then binds an identifier to the new object. Example:

    set A to {1, 2, 3, 4, 5}
    set B to A -- duplicates an existing object and binds identifier B to the new object
    set end of A to 6 -- modifies the contents of an existing object
    B
    --> {1, 2, 3, 4, 5}


    HTH
  • stephen_42 Calculating status...
    Wait. Now I'm confused.

    set A to {1, 2, 3, 4, 5}
    set B to A
    set A to {5, 4, 3, 2, 1}

    B = 1, 2, 3, 4, 5

    I understand that.

    set A to {1, 2, 3, 4, 5}
    set B to A
    set third item of A to "X"

    B = 1, 2, X, 4, 5

    Why did B change when the value of A was modified? It did not change when the value of A changed on the above sample.
    2.4Ghz 20" iMac, Mac OS X (10.5.2), 1.3Ghz 17" PowerBook & iPhone!
  • red_menace Level 6 Level 6 (14,275 points)
    Reread hhas' post - in your examples, one makes a new object, the other is changing an existing one.

    This (re)binds a variable to a new object:
    set A to {1, 2, 3, 4, 5} -- bind the variable 'A' to the list object {1,2,3,4,5}
    set B to A -- bind the variable 'B' to the same object as A
    set A to {5, 4, 3, 2, 1} -- rebind the variable 'A' to the list object {5,4,3,2,1}
    log B -- the variable 'B' is still bound to the list object {1,2,3,4,5}


    This modifies an existing object:
    set A to {1, 2, 3, 4, 5} -- bind the variable 'A' to the list object {1,2,3,4,5}
    set B to A -- bind the variable 'B' to the same object as A
    set third item of A to "X" -- modify the third item of the object that 'A' (and 'B') is bound to
    log B -- the variable 'B' is still bound to the object that is now {1,2,X,4,5}
    MacBook Pro, Mac OS X (10.5.6), /  G4 Digital Audio, G4 mini [Tiger 10.4.11]  /  G3 MiniTower [OS 9.2]
  • stephen_42 Level 1 Level 1 (125 points)
    I guess my confusion comes from never having run into this before... It never occurred to me that you could define "A", define "B" as "A", change the definition of "A", and affect "B" without specifically redefining "B" as "A".

    And hhas' post is still puzzling me. His number 3 and number 4 example scripts say the same things, but with different outcomes. I guess I'm not reading carefully enough.
    2.4Ghz 20" iMac, Mac OS X (10.5.2), 1.3Ghz 17" PowerBook & iPhone!
  • red_menace Level 6 Level 6 (14,275 points)
    You might have better luck thinking of it as: define an object, bind the variable "A" to it, bind the variable "B" to the same object, create a new object and bind the variable "A" to it - now you have two different objects.

    I think there was a typo (or overzealous use of paste) in hhas' example 4 - it should have been something like:
    set A to {1, 2, 3, 4, 5} -- bind the variable 'A' to the list object {1,2,3,4,5}
    copy A to B -- bind the variable 'B' to a new copy of the object bound to A ({1,2,3,4,5})
    set third item of A to "X" -- modify the third item of the object that 'A' is bound to
    log A -- the variable 'A' is bound to the (changed) object {1,2,"X",4,5}
    log B -- the variable 'B' is bound to the (unchanged) object {1,2,3,4,5}
    MacBook Pro, Mac OS X (10.5.6), /  G4 Digital Audio, G4 mini [Tiger 10.4.11]  /  G3 MiniTower [OS 9.2]
  • stephen_42 Level 1 Level 1 (125 points)
    I think I understand. I'm not sure this ever has come up before; I need to check some of my scripts to make sure.
    Now the "copy" makes sense to me. I thought that was the way it was working all along. I feel like such a beginner!

    Thank you. Very interesting.
    2.4Ghz 20" iMac, Mac OS X (10.5.2), 1.3Ghz 17" PowerBook & iPhone!

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.