1

I'm reading a book using VisualWorks and I try to write the code in GNU Smalltalk. I have this:

OrderedCollection subclass: Stack [
    push: anObject [
         self addLast: anObject.
    ]

    pop [
        self isEmpty
           ifTrue: [^nil]
           ifFalse: [^self removeLast].
    ]
]

| st |
st := Stack new.
st push: 'a'.
Transcript show: st pop.

but it doesn't work. Can someone please explain me what am I doing wrong?

caisah
  • 1,959
  • 1
  • 21
  • 31
  • Can you be more specific about what is not working and how exactly? – Uko Jan 29 '14 at 21:30
  • I got Object: Stack error: should not be implemented in this class, use #basicNew instead. Stuart's response was what I needed. In the future I will try to be more specific and also add the messages i get. – caisah Jan 30 '14 at 09:45

3 Answers3

3

I'm assuming you're getting Object: Stack error: should not be implemented in this class, use #basicNew instead?

If so, then it looks like you need to add <shape: inherit> in the body of your subclass.

See:

That seems like a bit of a leaky abstraction to me - but I guess it is what it is.

Community
  • 1
  • 1
Stuart Herring
  • 899
  • 6
  • 9
  • That was it . Can you please explain to me what does this exactly do? I don't quite understand from the documentation. Why don't we use it on all the classes we create by inheriting? – caisah Jan 30 '14 at 09:47
  • 1
    the pragmas are a GNU Smalltalk specific thing, and I'm not very familiar with it. But I suspect it's just a design tradeoff - it will be providing special information to the compiler to tell it that it's a special type of class - and given that it's pretty rare that you want to subclass one of those types it was probably considered a reasonable tradeoff to simplify the implementation. – Stuart Herring Feb 01 '14 at 23:02
0

The primary problem is a design one. When learning smalltalk and object orientation, you will be happier if you choose composition rather than inheritance by default. A stack has a very limited interface

push: anElement
pop
isEmpty

so why not make a class Stack with an instance variable stackData

Stack>>initialize
    stackData := OrderedCollection new

An OrderedCollection has a very wide interface, and this way you get a Stack that only responds to a very narrow interface. That makes it much easier to understand and use.

Stephan Eggermont
  • 15,847
  • 1
  • 38
  • 65
  • I understand what you mean, and I've heard about: "Always favour composition over inheritance". This is my first oo language and with time, I guess I will sharpen my design skills. But the snippet above was just a simple/dummy example and it was never intended to be used in real applications. – caisah Jan 30 '14 at 17:41
0

There is nothing wrong with your code. It's GNU-ST which does wierd things.

In other Smalltalks, this works like a charm. Here is what I typed into a Smalltalk/X workspace, and it does the expected (shows 'a' on the Transcript):

OrderedCollection 
    subclass: #MyStack
    instanceVariableNames:''
    classVariableNames:''
    poolDictionaries:''.

MyStack compile:'
    push: anObject
         self addLast: anObject.
'.

MyStack compile:'
    pop
        self isEmpty
           ifTrue: [^nil]
           ifFalse: [^self removeLast].
'.

| st |

st := XStack new.
st push: 'a'.
Transcript show: st pop.
blabla999
  • 3,130
  • 22
  • 24