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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:14:44+00:00 2026-05-29T23:14:44+00:00

How do I match union cases dynamically in F# when there are value declarations?

  • 0

How do I match union cases dynamically in F# when there are value declarations?

Non working code:

let myShape = Shape.Square
expect myShape Shape.Circle 

type Shape =
   | Circle of int
   | Square of int
   | Rectangle of ( int * int )

let expect someShape someUnionCase =
    if not ( someShape = someUnionCase )
    then failwith ( sprintf "Expected shape %A. Found shape %A" someShape someUnionCase )

let myShape = Shape.Square
expect myShape Shape.Circle // Here I want to compare the value types, not the values

If my union cases did not declare values, this works using instantiation samples (which is not what I want):

let myShape = Shape.Square
expect myShape Shape.Circle 

type Shape =
   | Circle
   | Square
   | Rectangle

let expect someShape someUnionCase =
    if not ( someShape = someUnionCase )
    then failwith ( sprintf "Expected shape %A. Found shape %A" someShape someUnionCase )

let myShape = Shape.Square
expect myShape Shape.Circle // Comparing values instead of types
  • 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-29T23:14:47+00:00Added an answer on May 29, 2026 at 11:14 pm

    When you call the expect function in your example with e.g. Shape.Square as an argument, you’re actually passing it a function that takes the arguments of the union case and builds a value.

    Analyzing functions dynamically is quite difficult, but you could instead pass it concrete values (like Shape.Square(0)) and check that their shape is the same (ignore the numeric arguments). This can be done using F# reflection. The FSharpValue.GetUnionFields function returns the name of the case of an object, together with obj[] of all the arguments (which you can ignore):

    open Microsoft.FSharp.Reflection
    
    let expect (someShape:'T) (someUnionCase:'T) = 
      if not (FSharpType.IsUnion(typeof<'T>)) then
        failwith "Not a union!"
      else
        let info1, _ = FSharpValue.GetUnionFields(someShape, typeof<'T>)
        let info2, _ = FSharpValue.GetUnionFields(someUnionCase, typeof<'T>)
        if not (info1.Name = info2.Name) then
          failwithf "Expected shape %A. Found shape %A" info1.Name info2.Name
    

    If you now compare Square with Circle, the function throws, but if you compare two Squares, it works (even if the values are different):

    let myShape = Shape.Square(10)
    expect myShape (Shape.Circle(0)) // Throws
    expect myShape (Shape.Square(0)) // Fine
    

    If you wanted to avoid creating concrete values, you could also use F# quotations and write something like expect <@ Shape.Square @> myValue. That’s a bit more complex, but maybe nicer. Some examples of quotation processing can be found here.

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

Sidebar

Related Questions

Is there any way to pattern match on discriminated union functions, e.g.:- type Test
I am trying to match floating-point decimal numbers with a regular expression. There may
When I execute the following query, even though there are 11 records that match,
Given the following XML: <LiveScores> <Competition id=4915 name=Comp1 type=Union group=> <Match id=1678 round=No Round
match.matches() returns false. This is odd, because if I take this regex and test
preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can
pathTokens match { case List(post) => (post, index) case List(search) => (search, index) case
String match = hello; String text = 0123456789hello0123456789; int position = getPosition(match, text); //
I need to match and remove all tags using a regular expression in Perl.
How to match a single quote in sed if the expression is enclosed in

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.