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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:25:22+00:00 2026-06-03T22:25:22+00:00

I’m still trying to wrap my head around how F# generalizes (or not) functions

  • 0

I’m still trying to wrap my head around how F# generalizes (or not) functions and types, and there’s a case that’s bugging me:

let min(a, b) = if a < b then a else b

let add(a, b) = a + b

let minInt = min(3, 4)
let minFloat = min(3.0, 4.0) // works!

let addInt = add(3, 5)
let addFloat = add(3.0, 5.0) // error: This expression was expected to have type
                             // int but here has type float

Here min has the generic type 'a * 'a -> 'a (requires comparison) while add has a concrete type int * int -> int, apparently inferred from its first use in the program. Both are declared and used in the same way, so why the difference in generalization?

I understand that in the case of add, the problem can be side-stepped by declaring the function inline, which causes it to get a generic type definition, i.e. 'a * 'b -> 'c (requires member (+)), but that doesn’t explain why this is needed in this case and not the other.

  • 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-03T22:25:23+00:00Added an answer on June 3, 2026 at 10:25 pm

    There is an excellent write up on this very issue by @TomasP here: http://tomasp.net/blog/fsharp-generic-numeric.aspx

    When writing simple generic code that has some type parameter ‘T, we
    don’t know anything about the type parameter and there is no way to
    restrict it to a numeric type that provides all the operators that we
    may need to use in our code. This is a limitation of the .NET runtime
    and F# provides two ways for overcoming it.

    But why are < and > (and by extension, =, <= and >=) OK?

    The F# compiler treats equality and comparison differently (see section 5.2.10 Equality and Comparison Constraints in the specs, thanks @Daniel). You get the special comparison constraint, which is allowed when (simply, see the spec for more detail):

    If the type is a named type, then the type definition does not have,
    and is not inferred to have, the NoComparison attribute, and the type
    definition implements System.IComparable or is an array type or is
    System.IntPtr or is System.UIntPtr.

    There is no such special handling for the + operator. Why couldn’t there be a constraint such as numeric?

    Well isn’t that operator also defined for strings? In some languages for lists and collections? Surely it would be an addable constraint and not numeric. Then many such overloaded operators can be found in a program with different semantic meaning. So F# provides a ‘catch-all’ method with static member constraints and the inline keyword. Only equality and comparison are special.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.