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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:39:19+00:00 2026-06-09T10:39:19+00:00

So far I’ve been quite impressed with the type inference in F#, however I

  • 0

So far I’ve been quite impressed with the type inference in F#, however I have found something that it didn’t really get:

//First up a simple Vect3 type
type Vect3 = { x:float; y:float; z:float } with 
  static member (/) (v1 : Vect3, s : float) = //divide by scalar, note that float
    {x=v1.x / s; y= v1.y /s; z = v1.z /s}
  static member (-) (v1 : Vect3, v2 : Vect3) = //subtract two Vect3s
    {x=v1.x - v2.x; y= v1.y - v2.y; z=v1.z - v2.z}
  //... other operators...

//this works fine
let floatDiff h (f: float -> float) x = //returns float
  ((f (x + h)) - (f (x - h)))/(h * 2.0)

//as does this
let vectDiff h (f: float -> Vect3) x = //returns Vect3
  ((f (x + h)) - (f (x - h)))/(h * 2.0)


//I'm writing the same code twice so I try and make a generic function:
let genericDiff h (f: float -> 'a) x : 'a = //'a is constrained to a float 
  ((f (x + h)) - (f (x - h)))/(h * 2.0)

When I try and build this last function a blue squiggly appears under the divide sign and the complier says the dreaded warning of “This construct causes code to be less generic than indicated by the type annotations. The type variable ‘a has been constrained to be type ‘float'”. I provide the Vect3 with the suitable / operator for the function. Why is it warning me?

  • 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-09T10:39:20+00:00Added an answer on June 9, 2026 at 10:39 am

    Standard .NET generics are not expressive enough to allow this kind of generic functions. The problem is that your code can work for any 'a that supports the subtraction operator, but .NET generics cannot capture this constraint (they can capture interface constraints, but not member constraints).

    However you can use F# inline functions and statically resolved type parameters that can have additional member constraints. I wrote an article that provides some more details about these.

    Briefly, if you mark the function as inline and let the compiler infer the type, then you get (I removed explicit mention of the type parameter, as that makes the situation trickier):

    > let inline genericDiff h (f: float -> _) x = 
    >   ((f (x + h)) - (f (x - h))) / (h * 2.0);;
    
    val inline genericDiff :
      float -> (float ->  ^a) -> float -> float
        when  ^a : (static member ( - ) :  ^a *  ^a -> float)
    

    The compiler now used ^a instead of 'a to say that the parameter is resolved statically (during inlining) and it added a constraint saying that ^a has to have a member - that takes two things and returns float.

    Sadly, this is not quite what you want, because your - operator returns Vect3 (and not float as the compiler inferred). I think the problem is that the compiler wants / operator with two arguments of the same type (while yours is Vect3 * float). You can use different operator name (for example, /.):

    let inline genericDiff2 h (f: float -> _) x = 
      ((f (x + h)) - (f (x - h))) /. (h * 2.0);;
    

    In this case, it will work on Vect3 (if you rename the scalar division), but it won’t easilly work on float (though there may be hacks that will make that possible – see this answer – though I would not consider that idiomatic F# and I’d probably try to find a way to avoid the need for that). Would it make sense to provide elementwise division and pass h as Vect3 value, perhaps?

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

Sidebar

Related Questions

So far I have only been updated a view from within its controller. I
Far I've found that giving the iframe a background colour will alter the timeline
So far I have not been able to find an answer, but I wanted
So far I have been suggesting new features to http://delphi.uservoice.com Since it is managed
So far all the sites I have made have been in PHP, I'm thinking
So far, I have been storing the array in a vector and then looping
So far I have been using in tutorials, examples and sample code only packages
So far I have this JsFiddle - an arrangement of DIVs that using media
As far as i remember while installing ubuntu,the installer didn't ask me for the
So far I've been setting up redirects for single pages on my ASP.NET site

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.