adding a method via Parser/Compiler
alienhard at netstyle.ch
Fri May 7 10:17:12 UTC 2004
The problem that the newly compiled method does not show up in the
class browser is that it is not classified.
Try this to add a new method, which is even simpler than directly
calling the compiler:
compile: 'showOnTranscript: anObject
Transcript show: anObject.'
On May 7, 2004, at 10:47 AM, Jakob Praher wrote:
> hi squeakers,
> finally I got some time to play with the squeak environment - kudos to
> you all.
> Please apologize, but I am a smalltalk beginnner.
> I wanted to know how to define a new Method on the fly and, thanks to
> the terrific inspectors and browsers I found out that the Compiler /
> Parser can be used to compile text to parse trees and optionally hang
> in methods in classes.
> "create a new class"
> Object subclass: #TestClass .....
> "create a new method node and add it to TestClass's MethodDictionary"
> methodNode := Compiler new
> compile: (ReadStream on:
> 'showOnTranscript anObject
> Transcript show: anObject.'
> in: TestClass
> notifying: nil
> ifFail: [Smalltalk beep]
> This works very well, if I say for instance:
> TestClass new showOnTranscript: 'hello from showOnTranscript in
> I get the results printed on the Transcript.
> The funny thing is though, that the method is not shown in the
> But the #subclass method creates a new Entry in the Browser.
> I have also tried to create a CompiledMethod with the generate method
> and hang it in the TestClass manually via Behavior>>addSelector, but
> had no luck with that too.
> so I am just curious, and would like to know, how to do it right :-)
> -- Jakob
More information about the Squeak-dev