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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:05:30+00:00 2026-05-15T16:05:30+00:00

I want to write something like this: type NumExp = Num of float type

  • 0

I want to write something like this:

type NumExp = Num of float

type Exp =
    | Num of float
    | Dot of NumExp * NumExp
    | Op of string * Exp * Exp

 let getValue (Num(n) : NumExp) = n

The compiler complains about a conflict between NumExp and Exp in getValue.
Even the following fails:

let getValue (nn : NumExp) = match nn with | Num(n) -> n

Is there a way to use the same case in both discriminated unions that works with functions? The DU definitions themselves are OK.

I want to use the same case to avoid adding a level of indirection like

type Exp =
    | NumExpExp of NumExp
    | Dot of NumExp * NumExp
    | Op of string * Exp * Exp

in the Exp definition.
I feel I’m missing something very basic here.

The reason I have NumExp is that I want to be able to ‘plug’ 2 Exps into a Dot (rather than 2 floats) because it makes generating expressions easier, but they can’t be any Exp, just numerical.

EDIT: what I really wanted to know is whether the two cases in the two DUs could be treated as the same entity (kind of like Exp “including” NumExp). I realise now Exp.Num and NumExp.Num are completely separate entities. Tomas provides a nice way of discriminating the two cases below.

  • 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-15T16:05:31+00:00Added an answer on May 15, 2026 at 4:05 pm

    If you have two discriminated unions with conflicting names of cases, you can use fully qualified name of the discriminated union case:

     let getValue (NumExp.Num(n)) = n  
    

    A more complete example would look like this:

    let rec eval = function
      | Exp.Num(f) -> f
      | Exp.Dot(NumExp.Num(f1), NumExp.Num(f2)) -> 
          // whatever 'dot' represents
      | Exp.Op(op, e1, e2) ->
          // operator
    

    This always uses fully qualified names, which is probably a good idea if the names are simple enough and there are conflicting cases (which could lead to a confusion).

    EDIT: Regarding sharing of cases – there is no automatic way of doing that, but you could have a case in your Exp that simply includes values of NumExp. For example like this:

    type NumExp =
      | Num of float 
    
    type Exp = 
      // first occurrence of NumExp is just a name, but F# allows us to reuse 
      // the name of the type, so we do that (you could use other name)
      | NumExp of NumExp  
      // other cases
    

    When writing eval function you would then write (note that we no longer have the issue with name clashes, so we don’t need fully qualified names):

    | NumExp(Num f) -> f
    | Op(op, e1, e2) -> // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 512k
  • Answers 512k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Pass SIG_DFL as the func parameter to signal() to reset… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer There are lots of versions of VBA, integrated in various… May 16, 2026 at 5:37 pm
  • Editorial Team
    Editorial Team added an answer Craig Hockenberry from Twitteriffic fame will help you out. Stay… May 16, 2026 at 5:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I often find I want to write code something like this in C#, but
When i write something like this: <Style x:Key=panelS> <Setter Property=Orientation Value=Horizontal /> <Setter Property=DockPanel.Dock
Is there a way to nest calls to active patterns? Something like this: type
In C# I can write something like this: enum MyEnum : byte { Value1,
The corrupt table entry is logged in the error file something like this... /usr/local/mysql/bin/mysqld:
I want to convert a string representations of few dozen enum types to enum
I'd like to create a builder that builds expressions that returns something like a
Is there something like anonymous inner classes (used in Java) in C#? I explain
I want to write a paint program in the style of MS Paint. For
I experimenting with writing Vista/W7 gadgets. In my experiment I want to write the

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.