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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:13:57+00:00 2026-06-12T11:13:57+00:00

I understand how active patterns can be defined and used in F#, including partial

  • 0

I understand how active patterns can be defined and used in F#, including partial active patterns, and the different sorts of patterns available. E.g.

let (|Big|Small|) animal = if animal.IsBig then Big(animal) else Small(animal)
let f = function | Big(_) -> "big" |Small(_) -> "small

However, I’m confused regarding using active patterns and discriminated unions in let bindings, arguments, and other places. For example, the MSDN has the following code:

let GetSubstring1 (Slice(p0, p1, text)) = 
    printfn "Data begins at %d and ends at %d in string %s" p0 p1 text
    text.[p0..p1]

Which confuses me a bit.

A specific issue. Let’s say I have the discriminated union,

type Union = A of int * int | B of int

Can I somehow make a function that accepts only Union.A, e.g.

let f (A(a, b)) = a + b

In this situation, it tells me there are unmatched patterns. Is there a way to satisfy it?

  • 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-12T11:13:59+00:00Added an answer on June 12, 2026 at 11:13 am

    As noted by @ildjarn, this applies to all patterns. They can appear in cases of the match clause (and in function which is similar), but also in the parameter declaration of let bound function and even in the let value binding.

    The key difference is that with let, you only want to use complete patterns that will always succeed. This is not needed for match or function, because there are multiple clauses, so if the first one fails, the matching can continue and try the next one.

    For example, the following complete pattern takes int and returns it as a string:

    let (|AsString|) (n:int) = n.ToString()
    

    Here are some ways how you can use it:

    let (AsString s) = 42         // Defines a value 's' of type string
    match 3 with AsString s -> s  // Returns a value "3" of type string
    let convert (AsString s) = s  // Defines a function 'int -> string'
    let convert = function AsString s -> s  // Same as the previous line
    

    EDIT: To answer your second question, if you use let with an incomplete pattern (i.e. that accepts only a single case of a discriminated union) then you get a compiler warning and the code may fail at runtime (if you call it with the other discriminated union case):

    let f (A(a, b)) = a + b      // This gives compile-time warning
    f (B 42)                     // This will fail at runtime 
    

    If you need to define a function that works only one of the cases, then you need to define a separate type.

    type AInfo = int * int 
    type Union = A of AInfo | B of int  
    

    Then you can write a function that will only take AInfo (but you can still work with Union in places where both options represent a valid input).

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

Sidebar

Related Questions

I'm trying to understand Active Patterns, so I'm playing around with FizzBuzz: let (|Fizz|_|)
I can't understand the reasoning behind Most-Recent-Order (how Windows sorts windows when switching via
So I understand that interior_ptr<typename> can be used to operate with arrays of structs
I'm trying to understand Active Directory as quickly as I can, but so far
I can't understand how should I get list of active listings (products) by keyword.
I understand that Flurry gives analytics including: user retention, daily users, average active session
I understand I can create an enum like this: public enum MyEnum { ONE(1),
Can someone give me an easy to understand definition of kernel thread dispatching or
How can I open a child window and keep the parent window active. I
I am trying to design an active object of sorts that once created, essentially

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.