<div dir="ltr">Wonderful! Thanks so much for showing me how to improve that code!</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Sun, Jan 26, 2025 at 3:34 PM Juan Vuletich <<a href="mailto:juan@cuis.st">juan@cuis.st</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><u></u>
<div bgcolor="#ffffff">
Hi Mark,<br>
<br>
Interesting exercise!<br>
<br>
Doing `self become: ` is dangerous and tricky. The very method doing
that will continue execution, but now the receiver is of a different
class! This would crash the system if that methods tries to access
instance variables that are not there. The control in #become: that
bothers you is trying to protect you from that risk.<br>
<br>
I suggest slightly different code, to avoid doing `self become:`:
move the sends to #become. to the class side. So:<br>
<br>
class side<br>
on: anObject<br>
| proxy |<br>
proxy := self new on: anObject.<br>
anObject become: proxy.<br>
^proxy<br>
<br>
instance side<br>
on: anObject<br>
history := OrderedCollection new.<br>
object := anObject shallowCopy.<br>
" anObject become: self"<br>
<br>
and class side<br>
restoreAndProvideHistory: aProxy<br>
| result |<br>
result := aProxy history.<br>
aProxy become: aProxy object.<br>
^result<br>
<br>
Also add the two needed getters #history and #object. Now the
workspace example does:<br>
<br>
history := HistoryProxy restoreAndProvideHistory: date.<br>
<br>
This doesn't need any modification to #become:, is more robust and
future proof, and better style.<br>
<br>
Cheers,<br>
<br>
On 1/26/2025 4:56 PM, Mark Volkmann via Cuis-dev wrote:
<blockquote type="cite">
<div dir="ltr">
<div>I recently read the book "Discovering Smalltalk". I know
it's quite dated and it's based on Smalltalk/V instead of
Smalltalk-80, but I still learned a lot from it.</div>
<div><br>
</div>
<div>I implement the Gatekeeper example described on pages
428-430 in Cuis. It demonstrates using the <font face="monospace">becomes:</font> method to track all
messages sent to a given object. I got it working, but I had
to make a small modification to the <font face="monospace">ProtoObject</font>
instance method <font face="monospace">becomes:</font>. I'm
wondering if my change would break other uses of this method.</div>
<div><br>
</div>
<div>I replaced the line</div>
<div><br>
</div>
<div><font face="monospace">selfMethod =
otherObjectMethod ifFalse: [<br>
</font></div>
<div><br>
</div>
<div>with</div>
<div><br>
</div>
<div><font face="monospace">selfMethod ~= nil and:<br>
[otherObjectMethod ~= nil] and:<br>
[selfMethod ~= otherObjectMethod] :: ifTrue: [<br>
</font></div>
<div><br>
</div>
<div>Here's my working Gatekeeper code where I renamed that
class to HistoryProxy.</div>
<div><br>
</div>
<div><font face="monospace">Object subclass: #HistoryProxy<br>
instanceVariableNames: 'history object'<br>
classVariableNames: ''<br>
poolDictionaries: ''<br>
category: 'Demo'<br>
</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">"class method"</font></div>
<div><font face="monospace">on: anObject<br>
^ self new on: anObject<br>
</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">"instance method"</font></div>
<div><font face="monospace">on: anObject<br>
history := OrderedCollection new.<br>
object := anObject shallowCopy.<br>
anObject become: self<br>
</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">"instance method"</font></div>
<div><font face="monospace">doesNotUnderstand: aMessage<br>
"Record the message and reroute it to the object."<br>
</font>
<div><span style="font-family:monospace"> history add:
aMessage.</span><br>
</div>
<font face="monospace"> ^ aMessage sendTo: object.<br>
</font></div>
<div><font face="monospace"><br>
</font></div>
<div><font face="monospace">"instance method"</font></div>
<div><font face="monospace">restoreAndProvideHistory<br>
</font><font face="monospace"> | result |<br>
</font><span style="font-family:monospace"> "Extract
information before mutating because it won't be available
afterwards."</span><br style="font-family:monospace">
<font face="monospace"> result := history.<br>
self become: object.<br>
^ result.</font><br>
</div>
<div><br>
</div>
<div>Here's an example of using this:</div>
<div><br>
</div>
<div><font face="monospace">date := Date today.<br>
HistoryProxy on: date.<br>
date monthName print.<br>
date dayOfWeekName print.<br>
date year print.<br>
history := date restoreAndProvideHistory.<br>
history print "an OrderedCollection(monthName dayOfWeekName
year)"</font><br>
</div>
<div><br>
</div>
<span class="gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div dir="ltr">
<div><font face="arial, helvetica, sans-serif">R.
Mark Volkmann</font></div>
<div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object
Computing, Inc.</font></span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
<br>
<pre cols="72">--
Juan Vuletich
<a href="http://cuis.st" target="_blank">cuis.st</a>
<a href="http://github.com/jvuletich" target="_blank">github.com/jvuletich</a>
<a href="http://researchgate.net/profile/Juan-Vuletich" target="_blank">researchgate.net/profile/Juan-Vuletich</a>
<a href="http://independent.academia.edu/JuanVuletich" target="_blank">independent.academia.edu/JuanVuletich</a>
<a href="http://patents.justia.com/inventor/juan-manuel-vuletich" target="_blank">patents.justia.com/inventor/juan-manuel-vuletich</a>
<a href="http://linkedin.com/in/juan-vuletich-75611b3" target="_blank">linkedin.com/in/juan-vuletich-75611b3</a>
<a href="http://twitter.com/JuanVuletich" target="_blank">twitter.com/JuanVuletich</a></pre>
</div>
</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><font face="arial, helvetica, sans-serif">R. Mark Volkmann</font></div><div><span style="font-size:12.8px"><font face="arial, helvetica, sans-serif">Object Computing, Inc.</font></span></div></div></div></div></div></div></div></div>