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

  • SEARCH
  • Home
  • 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 6601471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:45:25+00:00 2026-05-25T18:45:25+00:00

This case (simplified to the point that it doesn’t make much sense) is handled

  • 0

This case (simplified to the point that it doesn’t make much sense) is handled correctly by the F#’s type system:

type HumanBeing() = class end
type Animal() = class end

type CreatureController() =
    member this.Register creature = creature

type CreatureFactory() =
    let anAnimal = new Animal()
    let aHuman = new HumanBeing()

    member this.GiveMeAnAnimal  =
        (new CreatureController()).Register anAnimal

    member this.GiveMeAHuman =
        (new CreatureController()).Register aHuman

The type of CreatureController.Register is correctly inferred: ‘a -> ‘a, thus it can be called with the two different arguments.

Now the following version has a slight difference: instead of passing creature as argument to CreatureController.Register, it is passed to its constructor.

type HumanBeing() = class end
type Animal() = class end

type CreatureController(creature) =
    member this.Register = creature

type CreatureFactory() =
    let anAnimal = new Animal()
    let aHuman = new HumanBeing()

    member this.GiveMeAnAnimal =
        (new CreatureController(anAnimal)).Register

    member this.GiveMeAHuman =
        (new CreatureController(aHuman)).Register

This second example does not compile because Register is inferred as Animal, so you cannot call new CreatureController(aHuman).

(Note: in this simplified case the Factory is obviously flawed because it always return the same animal/humanBeing, but this behavior does not change if you replace anAnimal/aHuman with functions.)

Why isn’t CreatureControlled created as generic in the second case? Is this a compiler limitation? Am I missing something very basic (still learning…)?

  • 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-25T18:45:26+00:00Added an answer on May 25, 2026 at 6:45 pm

    In the first case, as you stated, Register is inferred to be generic, so it works. In the second case, you’re passing two different types to the constructor of a non-generic class. A concrete type must be inferred in this case. If you add type args to creature controller, it works:

    type HumanBeing() = class end
    type Animal() = class end
    
    type CreatureController<'T>(creature:'T) =
        member this.Register = creature
    
    type CreatureFactory() =
        let anAnimal = new Animal()
        let aHuman = new HumanBeing()
    
        member this.GiveMeAnAnimal =
            (new CreatureController<_>(anAnimal)).Register
    
        member this.GiveMeAHuman =
            (new CreatureController<_>(aHuman)).Register
    

    The difference is type parameters must be explicit on types, but not on functions. Also, constructors may only deal with type parameters declared by the type itself.

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

Sidebar

Related Questions

Imagine this case where I have an object that I need to check a
I use boost.program_options library. Consider this simplified case. po::options_description desc(Usage); desc.add_options() (uninstall,u, uninstall program)
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
In this case the license is question is Apache License v2, but basically if
In this case, the Visual Studio designer generates a method which takes the parameter
In this case, is it bad to subscribe to the proxy CloseCompleted event? public
Consider this case: public Class1 { public static final String ONE = ABC; public
Imagine this case, but with a lot more component buckets and a lot more
In this case, I've developed a CSS code for this web application ..and sometimes
I think in this case there is no need to declare a public constructor

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.