I am playing with Amber trying to port a small project that I use when
learning a new language. In this I tried to subclass the Array class but it does not seem to work. When I inspect an instance of my subclass it is an Array not my subclass of array. I am not a Smalltalk expert but I did the same thing in Pharo and it worked as I would have expected. Is this a bug or a limitation of the Amber implementation? Is there a workaround? Thanks Julian |
Hi Julian!
Ahhh, there are a few classes that you shouldn't subclass, unless you know what you are doing. Array is one of them. Do not subclass it. It won't work in JavaScript anyway. There are some funny techniques to try to subclass array in JS, but they're far from being perfect :) In general, don't subclass Classes that map to a JavaScript constructor. See js/boot.js https://github.com/NicolasPetton/amber/blob/master/js/boot.js#L559 Cheers, Nico On Fri, 2012-01-20 at 02:01 -0800, julian wrote: > I am playing with Amber trying to port a small project that I use when > learning a new language. In this I tried to subclass the Array class > but it does not seem to work. When I inspect an instance of my > subclass it is an Array not my subclass of array. I am not a > Smalltalk expert but I did the same thing in Pharo and it worked as I > would have expected. > > Is this a bug or a limitation of the Amber implementation? > > Is there a workaround? > > Thanks > Julian |
OK, thanks, I thought it might be something like that. I've come
across problems with subclassing array in JS before - it works OK in some JS implementations but not others. I had hoped that Amber might hide that nastiness but I suppose you can't without compromising efficiency too much. Julian On Jan 20, 11:19 am, Nicolas Petton <[hidden email]> wrote: > Hi Julian! > > Ahhh, there are a few classes that you shouldn't subclass, unless you > know what you are doing. > > Array is one of them. Do not subclass it. It won't work in JavaScript > anyway. There are some funny techniques to try to subclass array in JS, > but they're far from being perfect :) > > In general, don't subclass Classes that map to a JavaScript constructor. > See js/boot.js > > https://github.com/NicolasPetton/amber/blob/master/js/boot.js#L559 > > Cheers, > Nico > > > > > > > > On Fri, 2012-01-20 at 02:01 -0800, julian wrote: > > I am playing with Amber trying to port a small project that I use when > > learning a new language. In this I tried to subclass the Array class > > but it does not seem to work. When I inspect an instance of my > > subclass it is an Array not my subclass of array. I am not a > > Smalltalk expert but I did the same thing in Pharo and it worked as I > > would have expected. > > > Is this a bug or a limitation of the Amber implementation? > > > Is there a workaround? > > > Thanks > > Julian |
On 01/20/2012 12:25 PM, julian wrote:
> OK, thanks, I thought it might be something like that. I've come > across problems with subclassing array in JS before - it works OK in > some JS implementations but not others. I had hoped that Amber might > hide that nastiness but I suppose you can't without compromising > efficiency too much. But you can subclass OrderedCollection though. regards, Göran |
On Fri, 2012-01-20 at 12:54 +0100, Göran Krampe wrote:
> On 01/20/2012 12:25 PM, julian wrote: > > OK, thanks, I thought it might be something like that. I've come > > across problems with subclassing array in JS before - it works OK in > > some JS implementations but not others. I had hoped that Amber might > > hide that nastiness but I suppose you can't without compromising > > efficiency too much. > > But you can subclass OrderedCollection though. Well...Not for long, I was planning to make OrderedCollection an alias for Array. Cheers, Nico > > regards, Göran |
On 2012-Jan-20, at 7:23 , Nicolas Petton wrote: > On Fri, 2012-01-20 at 12:54 +0100, Göran Krampe wrote: >> But you can subclass OrderedCollection though. > > Well...Not for long, I was planning to make OrderedCollection an alias > for Array. Will it have at:ifAbsentPut: ? I admit I've been following Amber from afar to date, so its OrderedCollection may not have this, but is is so nice for dynamic programming algorithms! What about addFirst/removeFirst/addLast/removeLast? ../Dave |
On Tue, 2012-01-24 at 21:50 -0500, Dave Mason wrote:
> On 2012-Jan-20, at 7:23 , Nicolas Petton wrote: > > > On Fri, 2012-01-20 at 12:54 +0100, Göran Krampe wrote: > >> But you can subclass OrderedCollection though. > > > > Well...Not for long, I was planning to make OrderedCollection an alias > > for Array. > > Will it have at:ifAbsentPut: ? I admit I've been following Amber from afar to date, so its OrderedCollection may not have this, but is is so nice for dynamic programming algorithms! > > What about addFirst/removeFirst/addLast/removeLast? I'll check these methods. If there are missing I'll add them. Cheers, Nico > > ../Dave |
In reply to this post by julian
On 1/20/12, julian <[hidden email]> wrote:
> I am playing with Amber trying to port a small project that I use when > learning a new language. In this I tried to subclass the Array class > but it does not seem to work. When I inspect an instance of my > subclass it is an Array not my subclass of array. I am not a > Smalltalk expert but I did the same thing in Pharo and it worked as I > would have expected. > > Is this a bug or a limitation of the Amber implementation? > > Is there a workaround? > > Thanks > Julian Hi Julian Instead of subclassing array just subclass object and have an instance variable holding the array. Many people consider this to be better OO-style -- use delegation instead of subclassing. Kind regards Hannes |
In reply to this post by Nicolas Petton
On 1/25/12, Nicolas Petton <[hidden email]> wrote:
> On Tue, 2012-01-24 at 21:50 -0500, Dave Mason wrote: >> On 2012-Jan-20, at 7:23 , Nicolas Petton wrote: >> >> > On Fri, 2012-01-20 at 12:54 +0100, Göran Krampe wrote: >> >> But you can subclass OrderedCollection though. >> > >> > Well...Not for long, I was planning to make OrderedCollection an alias >> > for Array. >> >> Will it have at:ifAbsentPut: ? I admit I've been following Amber from >> afar to date, so its OrderedCollection may not have this, but is is so >> nice for dynamic programming algorithms! >> >> What about addFirst/removeFirst/addLast/removeLast? > > I'll check these methods. If there are missing I'll add them. Smalltalk JavaScript myArr addFirst: anElement myArr.unshift(anElement); myArr removeFirst myArr.shift(); myArr addLast: anElement myArr.push(anElement) myArr removeLast myArr.pop() This may be easily verified e.g. in Firefox 9 with menu 'Tools' / 'Web developer' / 'Web console' Regards Hannes > Cheers, > Nico > >> >> ../Dave > > > Array-shift-unshift-push-pop.PNG (62K) Download Attachment |
In reply to this post by Hannes Hirzel
On Jan 25, 2:13 pm, "H. Hirzel" <[hidden email]> wrote:
> Instead of subclassing array just subclass object and have an instance > variable holding the array. Many people consider this to be better > OO-style -- use delegation instead of subclassing. Yes, that's exactly what I have done. I agree over use of inheritance is not a good thing. Julian |
Yes, this is a very prominent recommendation.
First chapter of the 'gang of four' book: "Favor 'object composition' over 'class inheritance'." (Gang of Four 1995:20) http://en.wikipedia.org/wiki/Design_Patterns_(book) HJH On 1/26/12, julian <[hidden email]> wrote: > On Jan 25, 2:13 pm, "H. Hirzel" <[hidden email]> wrote: >> Instead of subclassing array just subclass object and have an instance >> variable holding the array. Many people consider this to be better >> OO-style -- use delegation instead of subclassing. > > Yes, that's exactly what I have done. I agree over use of inheritance > is not a good thing. > > Julian > |
Free forum by Nabble | Edit this page |