1

I'm trying to overwrite the #new message in MyObject. The problem is that when the text gets compiled, the local variables, disp and oldNew are changed to t1 and t2 respectively (I'm using Squeak 4.3) and then it can't send oldNew to self.
I could change their names but I'm not sure that's a good idea. Here's a basic outline of what I have:

MyObject class methodDict at: #new put:
    (Object compilerClass new
    compile: 'new
        | disp oldNew |
        oldNew := MyObject class methodDict at: #new.
        disp := Dispatcher new.
        ^disp xxxViewedObject: self oldNew'
    in: MyObject
    notifying: nil
    ifFail: []) generate

I'm not 100% sure if what I'm doing is the right way to do it so other ideas are welcome.

Edit: OK so I realise now it was looking for oldNew as a message in MyObject, but then how do I run the compiled method?

Itamar Bitton
  • 777
  • 2
  • 7
  • 25

3 Answers3

1

Apparently my problem was that MyObject was a subclass of ProtoObject and is now a subclass of Object.
Here's the code that seems to work after this change:

MyObject class methodDict at: #new put:
    (Object compilerClass new
    compile: 'new
        | disp |
        disp := Dispatcher new.
        ^disp xxxViewedObject: self basicNew initialize'
    in: MyObject
    notifying: nil
    ifFail: []) generate
Itamar Bitton
  • 777
  • 2
  • 7
  • 25
0

To evaluate your new generated compiled method you may use:

aCompiledMethod valueWithReceiver: nil arguments: #()

That's a nice way, however if you're experimenting problems I wrote a "code generator" based in a cross-Smalltalk library called Grease and which can be useful for you. It manages auto-comments, RBParser and Parser, authoring, and basic templating. All can be extended by anyone of course.

Hernán
  • 1,749
  • 10
  • 12
0

Generated methods are no different than others. So you simply send the method's selector to invoke it:

var := MyObject new. 
codefrau
  • 4,583
  • 17
  • 17