Wednesday, April 13, 2011

Polymorph counter example

I've discovered that I can write this in Pharo:

"This creates a class and one instance on the fly"
counter := Class new 
              superclass: Object; 
              addInstVarNamed: 'counter'; 
              compile: 'initialize 
                           counter := 0';
              compile: 'counterString 
                           ^ counter asString';
              compile: 'increment 
                           counter := counter + 1. 
                           self changed:#counterString';
              compile: 'decrement 
                           counter := counter - 1. 
                           self changed:#counterString';
              new.

(UITheme builder newColumn: {
  UITheme builder newLabelFor: counter getLabel: #counterString getEnabled: nil.
  UITheme builder newRow: {
    UITheme builder newButtonFor: counter action: #increment label: '+' help: nil.
    UITheme builder newButtonFor: counter action: #decrement label: '-' help: nil.
  }
}) openInWindowLabeled: 'Counter example'.

Coooooooooooooool ;)

No comments:

Post a Comment