Question

I am trying to accomplish a mutual binding between two classes in OCaml (a la Mediator Pattern's) and am getting an error upon compilation.

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class concreteColleague mIn = object inherit colleague 
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self)
 val mutable myColleague = (None:colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

Error: The class constructor colleague expects 1 type argument(s), but is here applied to 0 type arguments.

I can't say that I'm all that familiar with the ['foo] syntax in the class definition but have resorted to it (with no avail) in trying to allow the mediator to keep a reference to all colleagues and each colleague to its respective mediator while also trying to overcome the importance of a class's definition in source code. How do I go about allowing the colleagues to keep a reference to their mediator?

Was it helpful?

Solution

I fixed all the compilation problems. When you inherited from colleague, you need to provide its type parameter. As a result, concreteColleague I believe needs a type parameter too. Also, you forgot to pass the constructor argument mIn to the inherited class. And I added a type parameter to your type guard for myColleague, using the type of the object.

class virtual ['mediator] colleague mIn = object 
 val m = mIn
 method virtual getmediator : 'mediator
end;;

class ['mediator] concreteColleague mIn =
 object inherit ['mediator] colleague mIn
 method getmediator = m
end;;

(* Some other classes here *)

class mediator = object (self : 'self)
 val mutable myColleague = (None:'self colleague option)
 initializer 
  myColleague <- Some (new concreteColleague self)
end;;

However, I am not familiar with the Mediator pattern, so I am not sure what is the proper way to implement the Mediator pattern. Are you sure you need objects and virtual classes and stuff?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top