[Cuis-dev] clever or silly idea?

Vanessa Freudenberg vanessa at codefrau.net
Tue Jun 11 20:56:43 PDT 2024


On Tue, Jun 11, 2024 at 4:29 PM Mark Volkmann via Cuis-dev <
cuis-dev at lists.cuis.st> wrote:

> A thought occurred to me that I could override the instance method
> `doesNotUnderstand` in a class to avoid writing a bunch of basic accessor
> methods. Setting aside whether it's ever a good idea to have a getter and
> setter method for every instance variable in a class, I thought I could do
> it like this:
>
> doesNotUnderstand: aMessage
>     "gets or sets an instance variable"
>
>     | argCount getters key setters value |
>
>     argCount := aMessage numArgs.
>     argCount > 1 ifTrue: [ ^super doesNotUnderstand: aMessage ].
>
>     key := aMessage keywords first.
>     getters := self class allInstVarNames.
>     setters := getters collect: [ :name | name, ':' ].
>     (getters, setters includes: key) ifFalse: [ ^super doesNotUnderstand:
> aMessage ].
>
>     argCount = 0 ifTrue: [ ^self perform: key ].
>
>     value := aMessage arguments first.
>     self perform: key with: value
>
> But this goes into an infinite loop because sending the `perform:` message
> ends up hitting `doesNotUnderstand` again. Is there a way I could make this
> work?
>

Yes. The problem is that you don't have getters or setters implemented but
you can only "perform" methods that exist.

You need a different way to access the instance variables. You could use
the same methods that the debugger uses to get and set the instance
variables: "instVarAt:" and "instVarAt:put:".

Since you like the challenge I will not paste the "solution" here but put
it in a gist
https://gist.github.com/codefrau/3dc9e1a3020bb92864c57c4323041aec

–Nessa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240611/b8461db3/attachment.htm>


More information about the Cuis-dev mailing list