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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:00:32+00:00 2026-05-31T23:00:32+00:00

Consider this: module Module1 = type A() = class end type B() = inherit

  • 0

Consider this:

module Module1 =
    type A() = class end
    type B() = inherit A()
    type C() = inherit A()

    let f x = if x > 0 then new B() else new C()

The last line yields an error about type B being expected, but type C being found instead.
Ok, I can pretend to understand that: the compiler doesn’t know which common base to infer in case there are many.

But guess what? Even when I specify the function type, it still doesn’t work:

    let f x : A = if x > 0 then new B() else new C()

Now this gives me two errors: “A expected, B found” and “A expected, C found“.
WTF? Why can’t it see that both B and C are implicitly convertible to A?

Yes, I do know that I could use upcast, like so:

    let f x : A = if x > 0 then upcast new B() else upcast new C()

But guess what (again)? upcast only works in the presence of the explicit function type declaration!
In other words, this:

    let f x = if x > 0 then upcast new B() else upcast new C()

still gives an error.

WTF?! Do I really have to add 50% of noise to my program just to help the compiler out?
What’s with all that hype about F# code being clean and noiseless?

Somehow it feels like this cannot be true.
So the question is: am I missing something? How do I make this both compact and working?

  • 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-31T23:00:34+00:00Added an answer on May 31, 2026 at 11:00 pm

    Type inference and subtyping do not play well together, as Carsten’s links discuss to some extent. It sounds like you are unhappy with F#’s approach and would prefer it if

    if b then 
        e1 
    else 
        e2
    

    were implicitly treated more like

    if b then (e1 :> 'a) else (e2 :> 'a)
    

    with the compiler additionally inferring 'a to be the least upper bound in the type hierarchy based on the types that would otherwise be inferred for e1 and e2.

    It might be technically possible to do this, and I can’t definitively speak to why F# doesn’t work this way, but here’s a guess: if if statements behaved this way then it would never be an error to have different types in the if and else branches, since they could always be unified by implicitly upcasting them to obj. However, in practice this is almost always a programmer error – you almost always want the types to be the same (e.g. if I return a character from one branch and a string from the other, I probably meant to return strings from both, not obj). By implicitly upcasting, you would merely make the presence of these errors harder to find.

    Furthermore, it’s relatively rare in F# to deal with complicated inheritance hierarchies, except perhaps when interoperating with other .NET code. As a result, this is a very minor limitation in practice. If you’re looking for a syntactically shorter solution than upcast, you might try :> _, which will work as long as there is something to constrain the type (either an annotation on the overall result, or a specific cast on one of the branches).

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

Sidebar

Related Questions

I'm writing a module that has several methods. Let's consider this : package MyPackage;
Consider this code: new Ajax.Request('?service=example', { parameters : {tags : 'exceptions'}, onSuccess : this.dataReceived.bind(this)
Consider this - a base class A, class B inheriting from A, class C
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Consider this python program: import sys lc = 0 for line in open(sys.argv[1]): lc
Consider this scenario: I have a project with two modules and one common module
I have created a new library file sampler.rb inside the lib folder. Consider this
Consider this line of code: List<SIDB_TransactionInformation> transaction = SIDB.SIDB_TransactionInformations .Where(k => k.iscurrent == true
Lets consider I have a class inside a really long module path: sux =
Yet another argument with a friend of mine. Consider this code: class User <

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.