• 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: set myList to myList & ... and unexpected results
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set myList to myList & ... and unexpected results


  • Subject: Re: set myList to myList & ... and unexpected results
  • From: Michael Sullivan <email@hidden>
  • Date: Mon, 25 Jul 2005 10:34:41 -0400

On Jul 24, 2005, at 8:13 PM, Nigel Garvey wrote:
has wrote on Sun, 24 Jul 2005 11:09:40 +0100:\
Ryan Wilcox wrote:
Perhaps someone here can help me with a puzzle. Run the following code:

- --<code language="Applescript>

set myList to {}
set myItem to {1}

set myList to myList & myItem
set item 1 of myList to 123

set myList to myList & myItem

- --</code>

The output of this code is {123, 123}, and NOT {123, 1} like I would
expect.

Could someone explain why I get this result, please?

It's a bug, file a report. The first concatenation returns the right-hand
operand directly instead of creating a new object.

Amazing! The same bug's on my OS 8.6 system, so it's been around since at
least AS 1.3.7, but Ryan's apparently the first person to have noticed it.



This isn't a bug, it's a feature :)

Well, it really is intentional. When you use the "set" command, you are telling AS not to create new items by use references wherever feasible. Unfortunately

So when you say

set myList to myList & myItem
the result looks like this internally

--> {a reference to myItem}

So that when you modify item 1 of myList (the above), you are also modifying myItem.

If you want the item of your list to be a new object and not a reference, you should use "copy" rather than "set"

If I replace the appropriate line in Ryan's code with:

Copy myList & myItem to myList
The result is:
--> {123,1}
as expected.

Remember whenever you "set" a list or record to something else, you are passing a *reference*. If you will be modifying it and need a pristine copy, you must use "copy" instead.

It's in the spec, it's not a bug. Often the existing behavior is quite useful.

Some versions of applescript tutorials use "copy" everywhere that others use "set" to avoid this very confusion. That hurts speed on the many scripts where the difference is a non-issue, but I think it's a useful thing to do so that newbies don't get bitten by this.

I'm really shocked that old hands like you and has don't remember the discussions we've had on this list about this very behavior a few years ago.


Michael

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: set myList to myList & ... and unexpected results (From: "Nigel Garvey" <email@hidden>)

  • Prev by Date: Re: Mount a Firewire disk
  • Next by Date: My first question, please be gentle (stack overflow)
  • Previous by thread: Re: set myList to myList & ... and unexpected results
  • Next by thread: Re: set myList to myList & ... and unexpected results
  • Index(es):
    • Date
    • Thread