Demonstrate how to dynamically add variables to an object (a class instance) at runtime.

This is useful when the methods/variables of an instance are based on a data file that isn't available until runtime. Hal Fulton gives an example of creating an OO CSV parser at An Exercise in Metaprogramming with Ruby. This is referred to as "monkeypatching" by Pythonistas and some others.

declare
  %% Creates a new class derived from BaseClass
  %% with an added feature (==public immutable attribute)
  fun {AddFeature BaseClass FeatureName FeatureValue}
     class DerivedClass from BaseClass
        feat
	   %% "FeatureName" is escaped, otherwise a new variable
	   %% refering to a private feature would be created
           !FeatureName:FeatureValue
     end
  in
     DerivedClass
  end
 
  class Base
     feat
        bar:1
 
     meth init
        skip
     end
  end
 
  Derived = {AddFeature Base foo 2}
 
  Instance = {New Derived init}
in
  {Show Instance.bar} %% inherited feature
  {Show Instance.foo} %% feature of "synthesized" class