Repository navigation
Description
There are a couple of ways in which it is possible to define "active patterns" that are (from the F# perspective) values, not functions. While the tooling treats them as active patterns, it is impossible to actually use them as patterns in any pattern-matching position.
Expected behavior
The compiler disallows defining a multi-case active pattern that is not a function:
let (|P|Q|) = Choice<unit, unit>.Choice1Of2 ()
// error FS1209: Active pattern '|P|Q|' is not a functionI would expect the same for single-case and partial active patterns.
Actual behavior
(|P|)
A value bound to what looks like a single-case active pattern name is actually compiled as a property with a getter (when a module-bound value):
let (|P|) = 3
// val (|P|) : int = 3(|P|_|)
An option or value option value bound to a partial active pattern name is compiled to the exact same .NET method as it would be if it were defined as a function, but from the F# perspective it is a value (or a type function, even though you cannot give it explicit type arguments), not a function, although the tooling again treats it as an active pattern.
let (|P|_|) = None
// val (|P|_|) : 'a optionlet (|P|_|) = ValueNone
// val (|P|_|) : 'a voptionUnfortunately, updating the compiler to disallow these scenarios would be a breaking change — someone could be passing values so defined around qua values (f (|P|), let r = (|P|) + (|Q|), etc.) — although I doubt anyone actually intentionally binds values to active pattern names like this in normal code.
Metadata
Metadata
Labels
Type
Projects
- StatusShow more project fieldsNew



Activity
edgarfgp commentedon May 20, 2024
A warning behind a lang feature like we have done in other cases might work here ?
brianrourkeboll commentedon May 20, 2024
Yeah, I think that would probably be the best we could do.
brianrourkeboll commentedon May 20, 2024
It turns out that members with active pattern names are also allowed (SharpLab) and are not treated as patterns at all by the tooling:
Anonymous records, too:
(Parens are gone in tooling, though)
And constraint calls:
And lambdas:
Or any other pattern-matching context:
Also unions:
I guess it's a similar phenomenon to the one that allows you to bind operator names to values:
They're all just being lexed as
IDENTs (orparenIdents), and then there's inconsistent treatment later on...abelbraaksma commentedon May 21, 2024
Yes. from F#'s standpoint, these are just
longident, and while they have special meaning in some scenarios, they are not illegal. Likewise, if you do have an active pattern, you can just use it as a HOF function argument, or in a function call.I vaguely remember bringing this up ages ago, but I cannot remember the resolution at the time. We could (should?) issue a warning perhaps, instead of an error?
edgarfgp commentedon Jul 7, 2024
I think we should prevent users from using active patterns that are not functions.
edgarfgp commentedon Aug 13, 2024
@vzarytovskii Would be ok to create a new language preview flag e.g.
ImprovedActivePatternReportingand fix in multiple PR's some of the cases showed in this issue ?vzarytovskii commentedon Aug 13, 2024
I suppose, but I will still need to re-read the whole thread to freshen my memory
edgarfgp commentedon Aug 13, 2024
@vzarytovskii I think we can start with
Why:
let (|P|) = 3is not a function and can not even be used in matchT-Gro commentedon May 18, 2026
🤖 Copilot implementation hints
Fix
One-line addition in
src/Compiler/Checking/PostInferenceChecks.fsatCheckBinding(~line 2093).doesActivePatternHaveFreeTyparsalready raises FS1209 for multi-case APs when the type is not a function. The same check is missing for single-case(|P|)and partial(|P|_|)APs.In the
match TryGetActivePatternInfo vref withblock, add:Important: Use
v.TauType, notv.Type— usingv.Typeproduces false positives on generic partial APs likelet (|EqualTo|_|) y x = x = y.This is technically a breaking change — gate behind
langversion:previewor next if required.Instructions
let (|P|) = 3andlet (|P|_|) = Noneproduce FS1209, whilelet (|P|_|) x = falsestill compiles.dotnet test tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj --filter "ActivePattern" -c Debugexpert-reviewskill to review your work.dotnet fantomas .on changed files.