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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:53:24+00:00 2026-06-16T00:53:24+00:00

Consider the following code… type TypeOne () = member val Name = with get,

  • 0

Consider the following code…

type TypeOne () =
    member val Name = "" with get, set

type TypeTwo () =
    member val Name = "" with get, set
    member val Property = 0 with get, set

[<RequireQualifiedAccess>]
type UnionType =
    | TypeOne of TypeOne
    | TypeTwo of TypeTwo

// this only succeeds because we have defined the union type a
// requiring qualified access. otherwise TypeOne would be inferred
// as the union case, not the type (understandably)...

let t1 = TypeOne ()

// if we want t1 "as" the union type, we have to do the following...

let ut1 = UnionType.TypeOne t1

// the following function returns a result of type UnionType
// but we have to explicitly do this for each type

let makeUnion switch =
    match switch with
        | true -> UnionType.TypeOne (TypeOne ())
        | _ -> UnionType.TypeTwo (TypeTwo ())

As in the comments, it seems like there’s no way of inferring that return results should be of a union type, and if we have to require qualified access on the union type this is extremely verbose (which seems worryingly wrong).

There’s also no way of creating a function that takes only the union types and returns them as the union. (In this case a function which takes a TypeOne or TypeTwo and returns UnionType). Or is there? Are there better ways of working with discriminated unions?

  • 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-16T00:53:25+00:00Added an answer on June 16, 2026 at 12:53 am

    As pad has pointed out – you don’t need to qualify a union case with its parent type – so you can write

    let makeUnion switch =
        match switch with
        | true -> TypeOne (new TypeOne ())
        | false -> TypeTwo (new TypeTwo ())
    

    However you will always have to specify the TypeOne. This is to avoid the ambiguity present if you have multiple cases which take the same arguments – like

    type foo=
    |Bar of int
    |Baz of int
    

    Then even knowing the return type, the compiler can’t work out what to return. A more common example is actually with cases which take no arguments – say you decide to redifine true and false:

    type bool = |True |False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following code public class City { public string Name { get { return
Consider the following code: var x = 0; do { $.ajax({ type: POST, url:
Consider the following code: <div data-role=fieldcontain> <label for=name>Car</label> <select data-inline=true data-theme=b> <option value=volvo>Volvo</option> <option
Consider the following code, which simply calls a method on each member of a
Consider the following code:- class Name { {System.out.println(hi);} public static void main(String[] args) {
Consider following code: @interface TestClass () @property NSString *privateProperty; @end Now, as I learned,
Consider the following Code snippet, Assembly asm = Assembly.LoadFile(DLL_Path); Type t = asm.GetType(DLL_NameSpace.MyClass, false,
Consider the following code: public abstract class Test1 { public object Data { get;
Consider the following code example: <!DOCTYPE html> <html> <head> <title>HTML5-Template</title> <meta http-equiv=content-type content=text/html; charset=utf-8>
Consider the following code: enum E { A { public int get() { return

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.