I am attempting to implement the Visitor Design Pattern using OCaml's OO constructs and type system and am running into problems upon instantiation of an Element.
class virtual ['hrRep] employee = object
method virtual receiveEvaluation : 'hrRep -> unit
method virtual getName : string
end;;
class ['hrRep] accountant myName = object (self : 'a)
inherit ['hrRep]employee
val name = myName
method receiveEvaluation rep = rep#visitAccountant self
method getName = name
end;;
class ['hrRep] salesman myName = object (self : 'a)
inherit ['hrRep]employee
val name = myName
method receiveEvaluation rep = rep#visitSalesman self
method getName = name
end;;
class virtual ['accountant, 'salesman] hrRep = object (self)
method virtual visitSalesman : 'salesman -> unit
method virtual visitAccountant : 'accountant -> unit
end;;
class ['employee, 'salesman] lowerLevelHRRep = object (self) inherit ['employee, 'salesman]hrRep
method visitSalesman s = print_endline ("Visiting salesman "^s#getName)
method visitAccountant a = print_endline ("Visiting accountant "^a#getName)
end;;
let s1 : (< visitSalesman : 'a -> unit>) salesman = new salesman "Bob";;
let a1 : (< visitAccountant : 'a -> unit>) accountant = new accountant "Mary";;
let s2 : (< visitSalesman : 'a -> unit>) salesman = new salesman "Sue";;
let h1 : (< getName : string>, < getName : string>) lowerLevelHRRep = new lowerLevelHRRep;;
s1#receiveEvaluation h1;;
The error I get upon compilation is,
"The type of this expression, unit; _.. > salesman as 'a, contains type variables that cannot be generalized"
, however, the code compiles minus the line instantiating the salesman. How do I go about instantiating the salesman while maintaining the classes' functionality?
Edit: Error received with call to receiveEvaluation: This expression has type (, < getName : string>) lowerLevelHRRep but is here used with type unit > as 'a. The second object type has no method visitAccountant