[Newbies] Populate a new array.

Michael van der Gulik mikevdg at gulik.co.nz
Thu Mar 15 09:54:01 UTC 2007

Blake wrote:

> Here's another one:
> I've created a method that returns an array of its own class:
> anArray: aQuantity
>     | a |
>     a := Array new: aQuantity.
>     a size
>         do: [:i | a at: i put: self class new].
>     ^ a
> but, for a while, I was trying to make this a single line:
> ^ (Array new: aQuantity) do: [:item | item := stuff ]

Here, you're constantly assigning the variable "item" to new values, so 
the array remains untouched. I guess in a perfect world, the compiler 
shouldn't allow assignment to block parameters... I'm surprised that the 
Squeak compiler allows this.

> Also, observing that "at:" is actually implemented in Object, which  
> seems...odd!

It is a bit odd. An Object in the virtual machine resembles an array of 
instance variables, and that method assigns to an instance variable by 
number. It's not often useful (and downright dangerous if you ask me), 
but I've used it before (for example) to serialize the state of any 
object in an image.


More information about the Beginners mailing list