io
about
blog
community
docs
downloads

    2008 01 09         Constants

    Kevin Greer posted this to LtU recently and I though it was worth repeating due to the recent interest in dynamic features:

    Constant := Object clone do (
      with := method(msg, 
        self msg := msg
        self activate := method(call message setName(msg asString); msg)
      )
    )
    
    // Lets try it out: Make PI a regular number
    PI := 3.1415926
    
    // Make b a method that uses it
    b := method(PI+PI)
    writeln(b) // -> 6.283185
    
    // Inspect bs "source" code
    writeln(getSlot("b") code) // -> block(PI +(PI))
    
    // Make PI a constant
    PI := Constant with(3.1415926)
    writeln(b) // -> 6.283185
    writeln(getSlot("b") code) // -> block(3.141593 +(3.141593))
    
    // Still works but when we look at the code of b we see that
    // the PIs have been replaced with PIs value!!!