I\'m building a Smalltalk API to an XML-based web service. The XML service is so regular that, rather than write the methods by hand, I figured I\'d just override #doe
If you just want the source string to more clearly reflect the method:
SomeObject>>addHello: name
| methodTemplate methodSource |
methodTemplate := 'sayHello{1}Times: count
count timesRepeat: [ Transcript show: ''Hi, {1}!'' ].'.
methodSource := methodTemplate format: { name }.
self class compile: methodSource.
If you want the source to be syntax-checked, you could start with a template method like this:
sayHelloTemplate: count
count timesRepeat: [ Transcript show: 'Hi, NAME' ].
And then fill the template accordingly, like:
addHello2: name
| methodTemplate methodSource |
methodTemplate := (self class compiledMethodAt: #sayHelloTemplate:) decompileWithTemps.
methodTemplate selector: ('sayHello', name, 'Times:') asSymbol.
methodSource := methodTemplate sourceText copyReplaceAll: 'NAME' with: name.
self class compile: methodSource.
Of course, all of this would be clearer if some methods were extracted :)