[Cuis-dev] basic question about methods

Mark Volkmann r.mark.volkmann at gmail.com
Sat Jun 1 17:01:14 PDT 2024


Thank you SO MUCH for the tips! That worked perfectly.

On Sat, Jun 1, 2024 at 6:50 PM Hernán Wilkinson <
hernan.wilkinson at 10pines.com> wrote:

> 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
>


-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240601/044b4ea0/attachment.htm>


More information about the Cuis-dev mailing list