How can I add methods to a class at runtime in Smalltalk?

后端 未结 4 481
长情又很酷
长情又很酷 2020-12-29 14:44

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

4条回答
  •  渐次进展
    2020-12-29 15:10

    I would use block:

    himaker := [:name | [:n | n timesRepeat: [Transcript show: 'Hi , ', name, '!']]]
    hibob = himaker value: 'bob'.
    hialice = himaker value: 'alice'.
    hialice value: 2
    

    You can still make himaker a method

    himaker: name
        ^[:n | n timesRepeat: [Transcript show: 'Hi, ', name, '!']]
    

提交回复
热议问题