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 ] This doesn't work (I think) because "do" passes in a value rather than a reference. I didn't see how it could be done with withIndexDo: either. Wondering if I'm missing something. Also, observing that "at:" is actually implemented in Object, which seems...odd! _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Mar 11, 2007, at 10:40 , 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 ] > > This doesn't work (I think) because "do" passes in a value rather > than a reference. I didn't see how it could be done with > withIndexDo: either. > > Wondering if I'm missing something. ^(1 to: aQuantity) collect: [:i | stuff] - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Sun, 11 Mar 2007 04:30:29 -0800, Bert Freudenberg
<[hidden email]> wrote: > ^(1 to: aQuantity) collect: [:i | stuff] Very nice, Bert, thanks! _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Blake-5
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. Michael. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Mar 15, 2007, at 10:54 , Michael van der Gulik wrote:
>> 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. Wrong. Any Squeak object is made of zero or more instance variables ("named variables") plus zero or more numbered variables ("indexed variables"). The difference is that the number of named variables is fixed for all objects of that class, but the number of indexed variables can be specified when creating an instance using #new:, so it can differ from instance to instance. Object>>at: lets you access those indexed variables. How else would you access these? > It's not often useful (and downright dangerous if you ask me) Huh? It provides the very basics of the system! If we Smalltalkers say "everything is an Object" we mean it. Literally. There is no special "array object" that is different from "regular objects". Rather, you can implement an array as an object that happens to have indexed variables: Object variableSubclass: #BertsArray instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Bert-Arrays'. (BertsArray new: 7) at: 5 put: 42 - Bert - _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Bert Freudenberg wrote:
> On Mar 15, 2007, at 10:54 , Michael van der Gulik wrote: > >>> 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. > > > Wrong. Any Squeak object is made of zero or more instance variables > ("named variables") plus zero or more numbered variables ("indexed > variables"). The difference is that the number of named variables is > fixed for all objects of that class, but the number of indexed > variables can be specified when creating an instance using #new:, so > it can differ from instance to instance. Object>>at: lets you access > those indexed variables. How else would you access these? > >> It's not often useful (and downright dangerous if you ask me) > > > Huh? It provides the very basics of the system! If we Smalltalkers > say "everything is an Object" we mean it. Literally. There is no > special "array object" that is different from "regular objects". > Rather, you can implement an array as an object that happens to have > indexed variables: > > Object variableSubclass: #BertsArray > instanceVariableNames: '' > classVariableNames: '' > poolDictionaries: '' > category: 'Bert-Arrays'. > > (BertsArray new: 7) at: 5 put: 42 Thanks, Bert. I can't believe I've been Smalltalking for so many years and not known that! I always just assumed that arrays were some special case handled by the VM, like SmallIntegers. Michael. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |