Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 205013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:31:07+00:00 2026-05-11T17:31:07+00:00

I am attempting to implement the Visitor Design Pattern using OCaml’s OO constructs and

  • 0

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, <visitSalesman : 'a -> 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>, < getName:string>) lowerLevelHRRep 
but is here used with type <visitSalesman : 'a salesman -> unit > as 'a. 

The second object type has no method visitAccountant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:31:08+00:00Added an answer on May 11, 2026 at 5:31 pm

    EDIT – Separated the answer in 3 main points: the resolution of the initial compile error, a recursive solution, and a parametrized solution

    Resolution of the compile error

    Note that your code works fine in the top level:

    # let s = new salesman ();;
    val s : < visitSalesman : 'a -> unit; _.. > salesman as 'a = <obj>
    

    This kind of compile error is generally solved by adding a type annotation to help the compiler figuring the type. As the top level kindly told us what it was, we can modify the instantiation:

    let s : (< visitSalesman : 'a -> unit>) salesman = new salesman ();;
    

    And this compiles!

    A recursive solution

    It is possible to reduce complexity by using recursive classes. This totally removes the need for parametrized classes, but means that all objects need to be defined in the same source file.

    class virtual employee =
    object
      method virtual receiveEvaluation:(hrrep -> unit)
    end
    
    and accountant = 
    object(self)
      inherit employee
      method receiveEvaluation:(hrrep -> unit) = fun rep -> rep#visitAccountant (self :> accountant)
    end
    
    and salesman = 
    object (self)
      inherit employee
      method receiveEvaluation:(hrrep -> unit) = fun rep -> rep#visitSalesman (self :> salesman)
    end
    
    and hrrep = 
    object
      method visitSalesman:(salesman -> unit) = fun s -> print_endline ("Visiting salesman")
      method visitAccountant:(accountant -> unit) = fun s -> print_endline ("Visiting accountant")
    end
    
    let s = new salesman;;
    let e = (s :> employee);;
    let v = new hrrep;;
    
    e#receiveEvaluation v;;
    

    This prints “Visiting salesman”. The coercion to employee is just to make this closer to a real world scenario.

    A parametrized solution

    Looking at the problem again, I think it is not necessary to have a parametrized hrRep, because at this moment, all other types are known. By just making the employee class parametrized, I get this:

    class virtual ['a] employee = 
    object
      method virtual receiveEvaluation : 'a -> unit
      method virtual getName : string
    end
    
    class ['a] accountant name =
    object(self)
      inherit ['a] employee
      val name = name
      method receiveEvaluation rep = rep#visitAccountant self
      method getName = "A " ^ name
    end
    
    class ['a] salesman name =
    object(self)
      inherit ['a] employee
      val name = name
      method receiveEvaluation rep = rep#visitSalesman self
      method getName = "S " ^ name
    end
    
    class virtual hrRep = 
    object
      method virtual visitAccountant : hrRep accountant -> unit
      method virtual visitSalesman : hrRep salesman -> unit
    end
    
    class lowerLevelHRRep =
    object
      inherit hrRep
      method visitAccountant a = print_endline ("Visiting accountant " ^ a#getName)
      method visitSalesman s = print_endline ("Visiting salesman " ^ s#getName)
    end;;
    
    let bob = new salesman "Bob";;
    let mary = new accountant "Mary";;
    let sue = new salesman "Sue";;
    let h = new lowerLevelHRRep;;
    bob#receiveEvaluation h;;
    mary#receiveEvaluation h;;
    sue#receiveEvaluation h;;
    

    This returns:

    Visiting salesman S Bob

    Visiting accountant A Mary

    Visiting salesman S Sue

    The advantage of this solution is that employees do not need to know about the visitor, and therefore can be defined in their own compilation units, leading to cleaner code and less recompilation to do when adding new types of employees.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can check the interface but not the attribute: public… May 11, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer That it uses a buffer to read and write 8kB… May 11, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer You need to run it on the iPod touch with… May 11, 2026 at 10:48 pm

Related Questions

I am attempting to implement the Visitor Design Pattern using OCaml's OO constructs and
I am attempting to create a fixed step loop in my program, but for
I'm a complete novice, looking for instructions on implementing javascript. I am attempting to
To quickly summarise my question: Is it feasible to programmatically change the name of
I am attempting to re-implement the Copy behavior for a QTextEdit object. The custom

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.