<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
    <title></title>
  </head>
  <body bgcolor="#ffffff" text="#000000">
    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
cite="mid:CAFfRWnV3+3A0Jz1uzMDiZm2ACHEDjZMdhxem4z+HfzPohGKeKg@mail.gmail.com"
      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"
          data-smartmail="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 class="moz-signature" cols="72">-- 
Juan Vuletich
cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
linkedin.com/in/juan-vuletich-75611b3
twitter.com/JuanVuletich</pre>
  </body>
</html>