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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:31:52+00:00 2026-05-26T19:31:52+00:00

If ordinary functions could be used as patterns it would save having to write

  • 0

If ordinary functions could be used as patterns it would save having to write trivial active patterns like

let (|NotEmpty|_|) s = Seq.tryPick Some s

and would, hypothetically, allow

let s = seq []
match s with
| Seq.tryPick Some -> ...
| _ -> //empty

This would make functions more reusable, removing the need for “patternizing” functions you want to use with matching:

let f x = if x then Some() else None
let (|F|_|) = f

I know active patterns may be called as functions, so the previous example could be simplified by defining the pattern only. But forgoing the special pattern syntax simplifies this.

What are the reasons for the special syntax?

EDIT

In the following the active pattern shadows the literal.

[<Literal>]
let X = 1
let (|X|_|) x = if x = 0 then Some() else None

match 0 with //returns true
| X -> true
| _ -> false

Why wouldn’t that work for function calls within patterns also?

EDIT 2

I found a scenario that would be ambiguous

let zero n = if n = 0 then Some() else None
match 0 with
| zero -> //function call or capture?

This, in my mind, clarifies why an active pattern must begin with an uppercase letter–it makes the intention clearer and makes shadowing, such as in my previous example, much less likely.

  • 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-26T19:31:53+00:00Added an answer on May 26, 2026 at 7:31 pm

    Patterns and let-bound variables have different namespaces, which makes sense most of the time given how frequently shadowing occurs and how frequently short identifiers are used. For example, you might define x on line one of your program and then 200 lines later have match ... with | (x,y) -> x + y, in which case you almost certainly want x to be a fresh identifier.

    If you want to use arbitrary functions, just use a parameterized active pattern:

    let (|Id|_|) f x = f x
    
    match seq [] with
    | Id (Seq.tryPick Some) _ -> ...
    

    EDIT

    See Name Resolution for Patterns in the spec for details on name resolution. The key is that there is a logical PatItems table which is distinct from the ExprItems table that is used for names in expressions. In the particular case that you added to the edit in your question, the last definition of X wins, so it’s treated as an active pattern in this case (effectively shadowing the literal when X appears in a pattern).

    In addition to issues with name collisions/shadowing, I suspect that there are also ways that allowing a wider range of expressions in patterns would result in ambiguous parses, though I can’t think of any off hand.

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

Sidebar

Related Questions

Let's say I'd like to start a small linux distro before my ordinary operating
Not that I would ever write the code like the following in my professional
An ordinary object, I can use o.__repr__() to see something like '<__main__.A object at
Say I have an ordinary table in my db like so ---------------------------- | id
I'm learning Clojure and as an exercise I wanted to write something like the
I am creating a wizard from the Drupal example file and would like to
I've got a pretty ordinary Java EE application running on JBOSS. It uses the
Can I run my mod_perl aplication as an ordinary user similar to running a
Does anybody know of a method for creating custom Performance Counters using ordinary unmanaged
Suppose I have a weak reference to a car which has an ordinary (strong)

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.