Mutable objects in rex

set(Var, Value)     Var := Value

sets Var to Value

(Var must be defined first)

Example

Object contains one number

Construct sets initial value

Message  "up"    raises value

       "down"  lowers value

       "max"   returns maximum

counter(initial)=

  value = initial,

  mx = initial,

  (msg) Þ msg=="down"?

             set(value,value-1):

           msg=="up"?

sequencing operator  Þ  seq(set(value,value+1),

                set(mx,max(mx,value))):

           msg=="max"?

             mx :

  concat("message", msg, " not understood");

Instantiations:

a = counter(0); b = counter(0);

a("up"); a("down"); a("max");

Link to source file objects.rex.

To Next Slide To Previous Slide To Contents