import concurrency;

class Reference[a](x: a) with {
  
  module join with {
    def value: a;
    def update(x: a): Unit;
    private def state(x: a): nil;
    value & state(x) = spawn < (reply (x) to value) | state(x) >;
    update(x) & state(y) = spawn < (reply to update) | state(x) >;
  }
  
  state(x);
}


module refTest with {  
  
  def main(args: Array[String]): Unit = {
    val v = new Reference(5);
    System.out.println("value = " + v.value);
    v.update(42);
    System.out.println("value = " + v.value);
    System.out.println("value = " + v.value);
    v.update(23);
    System.out.println("value = " + v.value);
    System.out.println("value = " + v.value);
  }
 
}

