tags:

views:

43

answers:

2

I'm trying to implement a new class in Squeak, and for some reason when I run a test I get a MessageNotUnderstood error, even though the message is defined. Class code:

Object subclass: #ClassAnalyzer
instanceVariableNames: 'theClasses'
classVariableNames: ''
poolDictionaries: ''
category: 'OOP3'!

!ClassAnalyzer methodsFor: 'initialize-release' stamp: 'NK 11/30/2009 22:50'!
initialize: setOfClasses
theClasses := setOfClasses.! !

!ClassAnalyzer methodsFor: 'initialize-release' stamp: 'NK 11/30/2009 22:49'!
newWith: input
[input isKindOf: Collection]
 ifFalse: [^ClassAnalyzer new: input].
^ClassAnalyzer new: (input asSet).! !

And here's the test I ran:

| abcd z |
z:=1class.
abcd:= ClassAnalyzer newWith: z.

Any idea what I'm doing wrong? My current theory is that since when you invoke a constructor there's no object yet, so it should be a bit different (maybe there should be a predefined function of the same name in Object or protoObject), but if that's the deal, then how would one go about defining a none standard named constructor without changing Object?

+1  A: 

You should define the constructor on the class side. In the browser there are three buttons [ instance ][ ? ][ class ], click on the class button to add methods on the class side. Don't forget to switch back to the instance side after you are done.

Adrian
A: 

Note that this line of yours is likely to contain a mistake:

 [input isKindOf: Collection]
        ifFalse: [^ClassAnalyzer new: input]

Instead, you probably want to call ifFalse on a boolean, so you used the wrong sort of braces. Instead do:

 (input isKindOf: Collection)
        ifFalse: [^ClassAnalyzer new: input]

The rest was covered by Adrian (newWith: should be on the class side. In Java parlance, you'd call it a static method).

nes1983