Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!scramble.lm.com!news.math.psu.edu!chi-news.cic.net!nntp.coast.net!zombie.ncsc.mil!news.mathworks.com!tank.news.pipex.net!pipex!dish.news.pipex.net!pipex!mk2!
From: dpeachey@vmark.co.uk (Dan)
Subject: Re: What is wrong with this code?
Organization: VMark Software Ltd.
Date: Tue, 5 Mar 96 09:55:44 GMT
Message-ID: <1996Mar5.095544.22970@vmark.co.uk>
X-Newsreader: Forte Free Agent 1.0.82
References: <4hdmdb$9eq@news.accent.net>
Sender: @vmark.co.uk
Lines: 104

kovaciks@infobahnos.com (Peter Kovacik) wrote:

>Hi,

>I am a PL1 programmer trying to learn something new - the Smalltalk language.
>This is my problem:
>I created a class SetCache as a subclass of Collection with these class methods:

>SetCache class>> new
>   ^self new: 20.

>SetCache class>>new: cacheSize
>   ^super new initCacheSize: cacheSize.

>I added some more instance methods, like:

>initCacheSize: anInt
>   contents := Array new: anInt.


>Then I tested my code by running these statements:

>X:= SetCache new.
>X add:1.
>...

>However, this didn't work because of the error message:
>"Instance method 'add' is not in class SmallInteger".

>Then I performed some experiments with my #new:  method by rewriting it.
>SetCache class>>new2: cacheSize
>   |obj|
>   obj:= super new.
>   obj initCacheSize: cacheSize.
>   ^obj.

>Now everything works. Why?

>I continued backwards and changed my #new2 method again:
>SetCache class>>new3: cacheSize
>   |obj|
>   obj:= super new.
>   ^obj initCacheSize: cacheSize.

>My error message was the same as with the #new: method ("... not in 
>class SmallInteger."). Why this error? 

>I continued again and wrote
>SetCache class>>new4: cacheSize
>   |obj|
>   obj:= super new.
>   obj initCacheSize: cacheSize.

>My error message this time was: "Class method '#add' is not in class
>SetCache". 

>I don't understand the difference between #new3 and #new4.
>If no return value is specified, the return value is the value of the
>last statement. For me, both methods are the same. What I am doing wrong?  

>Could somebody explain me this behavior of the ENFIN 4.0 ?
>The code example can be find in the 'Using ENFIN Smalltalk', Chapter 12.4.
>(If you need the complete code, I can e-mail it to you.)


>Peter Kovacik
>ISM Montreal, Quebec, Canada
>Email: kovaciks@infobahnos.com
>Phone: (514) 422-6242


Peter,

First thing.  If no return value is specified,  the return value is
the reciever of the message,  not the value of the last statement.

So  for new4

X := SetCache new4: 20.  

If you inspect the value of X you will see it is SetCache class, since
no return value was specified from the new message,  and SetCache was
the reciever.

So X add: 1 is the same as SetCache add: 1, which is why you get the
error: Class Method #add not found in class SetCache.

In your first new method,  for some reason you where returning an
Instance of SmallInteger.  Check to see what values are being returned
from a method.

Also Collection is an abstract class.  You will need to implement all
the functionality such as add: etc.  Better to subclass a concrete
collection instead.

Hope this helps

Dan

ObjectStudio (ENFIN) Support.
VMark Software

My opinions are my own, and not those of my employers, or my mum.

