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 7948485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:44:07+00:00 2026-06-04T01:44:07+00:00

I am learning F# and I don’t understand how type inference and generics work

  • 0

I am learning F# and I don’t understand how type inference and generics work in this language. For example, I can declare a generic min function and use it with parameters of different types:

let min a b = if a < b then a else b

let smallestInt = min 3 5
let smallestFloat = min 3.0 5.0

But if I try the same thing with a type, it doesn’t work:

type Point2D(x, y) = 
    member this.X = x
    member this.Y = y

let float32Point = new Point2D(0.0f, 1.0f)
let intPoint = new Point2D(0, 1) // This expression was expected to have type 
                                 // float32 but here has type int

So, I have a few questions:

  • Why can I re-use the generic function definition for different types but not the type definition?
  • Is the function specialized for every type at run-time like with C# generics? Or at compile-time like C++ templates? Or is boxing performed to treat every argument as IComparable?

Thanks.

  • 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-06-04T01:44:09+00:00Added an answer on June 4, 2026 at 1:44 am

    Classes require explicit type parameters. This works:

    type Point2D<'T>(x:'T, y:'T) = 
        member this.X = x
        member this.Y = y
    
    let float32Point = Point2D(0.0f, 1.0f)
    let intPoint = Point2D(0, 1)
    

    To answer your second question, your definition of min has the signature 'a -> 'a -> 'a (requires comparison). The comparison constraint exists only at compile-time (the run-time signature is the same, minus the constraint).

    < is replaced with a call to GenericLessThanIntrinsic, which has the constraint. The constraint is merely propagated to callers.

    Also, from section 14.6.7 of the spec:

    Generalization is the process of inferring a generic type for a definition where possible, thereby making the construct reusable with multiple different types. Generalization is applied by default at all function, value, and member definitions, except where listed later in this section. Generalization also applies to member definitions that implement generic virtual methods in object expressions.

    (emphasis added)

    Notice, classes are missing from the list. I suppose it doesn’t give the rationale, but it is by design.

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

Sidebar

Related Questions

I am learning Haskell and I don't understand why I can do this: f
I just started learning C but I don't understand this part of the code.
I'm learning how to dynamically load DLL's but what I don't understand is this
I'm learning PHP and I don't know how to get this to even work.
I'm actually learning Symfony and I don't understand the fact of including classes. Can
I am learning Obj-C and don't quite understand the purpose of the code on
I'm learning processing (for those who don't know its a java-based language geared towards
Something I don't enjoy about programming is learning a new API. For example, right
i have learning reports in access and i don't understand the difference between these
im learning how to use the SELECT CASE but i don't understand very well,

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.