programatically generating methods

Niko Matsakis niko at alum.mit.edu
Fri Dec 7 10:28:13 UTC 2007


What is the best way to programatically generate a CompiledMethod from  
a string?

This is what I currently do:

	mthd := Compiler new
		compile:(String streamContents: [:stream |
			stream
				nextPutAll: 'bindingsString'; cr;
				tab; nextPutAll: 'Dynamically generated by bindingsString:'  
asSmalltalkComment; cr;
				tab; nextPutAll: '^ '; nextPutAll: aString  
surroundedBySingleQuotes; nextPut: $.; cr.
			 ])
		in:self
		classified:'bindings'
		notifying:nil
		ifFail:[:x | ^ x].
	self class addSelector:#bindingsString withMethod: mthd generate.
	self class organization classify: #bindingsString under:#bindings  
suppressIfDefault:false.

This works, actually, though I'm sure the "ifFail" block is totally  
wrong.  However, it doesn't "feel" like the right way.  For one thing,  
I am not sure that method of Compiler is a public API.  For another, I  
have to repeat a lot of information such as the selector and  
classification.

This is in relation to my last message: I am trying to generate  
methods that will return constant data so that it survives  
Monticello.  (I also tried hacking around with MCClassDefinition, but  
that seemed like a lot of work...).  The constant data is only a  
string, so if there is an easier way I'm all ears.


thanks!

Niko 



More information about the Squeak-dev mailing list