A weird thing about Arrays

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A weird thing about Arrays

Ricardo Moran
Hi, guys

I'm sorry if what I'm about to ask is a newbie question but I'm puzzled about a behavior I found today regarding Arrays. 

a1 := #(0 0 0 0).
a2 := #(0 0 0 0).
a1 == a2

If I select the whole expression above and execute it on a workspace it returns true (meaning that a1 and a2 are the same object). If I execute each line separately it returns false. 
I don't understand why this happens. Is this expected? or is it a bug?

This only happens when I use the literal syntax, using Array >> #new:withAll: works fine regardless of the way I execute it. I'm inclined to think this is a bug, but I wanted to know what you think about it.

Cheers
Richo



Reply | Threaded
Open this post in threaded view
|

Re: A weird thing about Arrays

Levente Uzonyi-2
On Fri, 12 Mar 2010, Ricardo Moran wrote:

> Hi, guys
>
> I'm sorry if what I'm about to ask is a newbie question but I'm puzzled
> about a behavior I found today regarding Arrays.
>
> a1 := #(0 0 0 0).
> a2 := #(0 0 0 0).
> a1 == a2
>
> If I select the whole expression above and execute it on a workspace it
> returns true (meaning that a1 and a2 are the same object). If I execute each
> line separately it returns false.
> I don't understand why this happens. Is this expected? or is it a bug?
>
> This only happens when I use the literal syntax, using Array >>
> #new:withAll: works fine regardless of the way I execute it. I'm inclined to
> think this is a bug, but I wanted to know what you think about it.

It's an optimization by the compiler. If the same literal object appears
twice or more, only one object will be created.


Levente

>
> Cheers
> Richo
>

Reply | Threaded
Open this post in threaded view
|

Re: A weird thing about Arrays

Ricardo Moran
Oh, I wish I knew that before!
I lost a lot of time thinking some magic stuff was messing with my arrays :P


On Fri, Mar 12, 2010 at 6:45 PM, Levente Uzonyi <[hidden email]> wrote:
On Fri, 12 Mar 2010, Ricardo Moran wrote:

Hi, guys

I'm sorry if what I'm about to ask is a newbie question but I'm puzzled
about a behavior I found today regarding Arrays.

a1 := #(0 0 0 0).
a2 := #(0 0 0 0).
a1 == a2

If I select the whole expression above and execute it on a workspace it
returns true (meaning that a1 and a2 are the same object). If I execute each
line separately it returns false.
I don't understand why this happens. Is this expected? or is it a bug?

This only happens when I use the literal syntax, using Array >>
#new:withAll: works fine regardless of the way I execute it. I'm inclined to
think this is a bug, but I wanted to know what you think about it.

It's an optimization by the compiler. If the same literal object appears twice or more, only one object will be created.


Levente


Cheers
Richo