adding a method via Parser/Compiler
Jakob Praher
jp at hapra.at
Fri May 7 08:47:25 UTC 2004
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 TestClass'.
I get the results printed on the Transcript.
The funny thing is though, that the method is not shown in the
ObjectBrowser/ClassBrowser.
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
mailing list
|