Hi Friedrich, Am 31.03.2011 um 18:09 schrieb Friedrich Dominicus:
I've spend the better time of the afternoon with trying metaprogramming with Smalltalk I finally have come up with:
asciiNames := Array withAll: #(#nul #soh #stx #etx #eot #enq #ack #bel #bs #tab #lf). i := 1. asciiNames do: [ :each | self class compile: each asString , ' ^ asciiField at: ', i asString. i := i + 1 ].
if you don't like string concatenation you may find this useful #(#nul #soh #stx #etx #eot #enq #ack #bel #bs #tab #lf) withIndexDo: [:each :index | self class compile: ('<1s><n><t>^ asciiField at: <2s>' expandMacrosWith: each with: index printString) classified: #(#accessing)]
which at least does what I want given names to ASCI Codes. Now I have to admit I dislike this stuff with putting a string togehter and run compile: on this string.
I'm sure there is a more elegant way. Would you mind to lend me a hand?
Yes and because I think I will need much more code generation in the near future. Does you have nice Tutorial, Book whatever to do such kind of jobs in Smalltalk I've quite a lot of books about Smalltalk but none has this as theme... So it's really a bit hard to get into it....
Well, I think you have the perfect source for learning right in front of you. The code snippet above didn't come from my mind. I just remembered that if you are on a class you can open the menu, open 'Refactor Class' and select Accessors to have pharo generate getters and setters for you. So I wrote "Accessors" (with the double quotes) in the workspace selected it and did a string seach (shortcurt E). From the choice given 'ORCmdAccessorClassRefactoring' seemed to be the most feasible. Then I investigated the methods found some other reference and so on (I leave this as an exercise). Finally I found a snippet which I copied and altered for your needs. This way I learned something about OmniBrowser, Refactoring-Engine and that there is a method expandMacrosWith: :) I think this is the way to go. Often it is hard if you first have to think about a problem that you can solve. But you have a problem so with the very good searching capabilities of pharo you can find such things easily. Hope this helps, Norbert