Search code examples
classbooleansmalltalksqueak

testing for instance of a class


I have a question in squeak, I'm completely new to it so I'm asking about the basics. I created a class

Object subclass: #Course
instanceVariableNames: 'id name day time isTwoHoursLong'
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Objects'

which implements the following method:

isTwoHoursLong: aBoolean
(aBoolean isMemberOf: Boolean) 
    ifFalse: [self error: 'invalid input value']
    ifTrue: [isTwoHoursLong:=aBoolean.].

aBoolean has to be true or false (an instance of Boolean). now I tried using the method:

|c1|
c1:=Course new.
c1 isTwoHoursLong:true.

but for some reason I fall into the ifFalse option which sends an error. can someone maybe help clear things up?


Solution

  • Try isKindOf: instead of isMemberOf:.
    True and False are subclasses of Boolean and isKindOf: tests whether the argument is the class or a superclass of the receiver.

    But I would probably omit this manual typecheck altogether.