[Cuis-dev] basic question about methods

Hernán Wilkinson hernan.wilkinson at 10pines.com
Sat Jun 1 16:50:22 PDT 2024


Hi Mark,

On Sat, Jun 1, 2024 at 8:23 PM Mark Volkmann via Cuis-dev <
cuis-dev at lists.cuis.st> wrote:

> Just getting started with Smalltalk ...
>
> I defined the class VRectangle as follows:
>
> Object subclass: #VRectangle
>     instanceVariableNames: 'height width'
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'Playground'
>
> Then I added the class method height:width: like this:
>
> height: aHeight width: aWidth
>     | r |
>     r := self basicNew.
>     r height: aHeight.
>     r width: aWidth
>

Good! I would make some changes, instead of sending basicNew just send new.
The message basicNew is there is case you override new, it is not common to
use it.
I would also create an initialization message on the instance side, so you
do not need to have setters and communicate the idea that a Rectangle is
immutable
And the error you are having is because you are not returning r, and when
there is no explicit return, self is returned that in this case is
VRectable class (that is the class, not an instance of it).
Therefore, I would implement that message this way:
height: aHeight width: aWidth

    ^self new initializeHeight: aHeight width: aWidth

>
>
> Then I added these instance methods:
>
> height: aNumber
>     height := aNumber
>
> width: aNumber
>     width := aNumber
>

No need for these with my suggestion, just:
initializeHeight: aHeight width: aWidth
    height := aHeight.
    width := aWidth

Doing, as I said above, you are sending the message that instances of
VRectangle are immutable. Of course they are not really because the
initializeHeight:... can be sent and change the instance, but culturally
you are suggesting is immutable and initialization messages should only be
sent from the class side when instantiating a class (also a cultural
heuristics).


> area
>     ^ height * width
>
> Then in a Workspace I did this:
>
> r := VRectangle height: 2 width: 3.
> r area.
>
> I get MessageNotUnderstood from the last line, but I don't understand why.
>

That is you forgot the ^r in the instance creation message.


> I can inspect r and it looks good.
>

You should be seeing the class, not the instance...
Return r or implemente that message with my suggestion and it should work
fine.

Cheers!
Hernan.

>
> --
> R. Mark Volkmann
> Object Computing, Inc.
> --
> Cuis-dev mailing list
> Cuis-dev at lists.cuis.st
> https://lists.cuis.st/mailman/listinfo/cuis-dev
>


-- 

*Hernán WilkinsonAgile Software Development, Teaching & Coaching*
*Phone: +54-011*-4893-2057
*Twitter: @HernanWilkinson*
*site: http://www.10Pines.com <http://www.10pines.com/>*
Address: Alem 896, Floor 6, Buenos Aires, Argentina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240601/7f44ea42/attachment-0001.htm>


More information about the Cuis-dev mailing list