fsharp / fsharp/fslang-suggestions
Allow further expressions as active pattern arguments
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Allow arbitrary expressions as active pattern arguments
I propose we allow:
- Property access
Already allowed:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
let l = "123".Length
match 3 with
| Eq l -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
match 3 with
| Eq "123".Length -> printfn "A"
(*
error FS0010: Unexpected symbol '.' in pattern matching. Expected '->' or other token.
error FS0010: Unexpected symbol '.' in pattern matching. Expected '->' or other token.
error FS0001: This expression was expected to have type
'int'
but here has type
'string'
*)
| _ -> printfn "B"
```
- `typeof` (https://github.com/fsharp/fslang-suggestions/issues/758)
Already allowed:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
let t = typeof
match typeof with
| Eq t -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
match typeof with
| Eq typeof -> printfn "A" // error FS0010: Unexpected type application in pattern matching. Expected '->' or other token.
// error FS0010: Unexpected type application in pattern matching. Expected '->' or other token.
// error FS0685: The generic function 'typeof' must be given explicit type argument(s)
| _ -> printfn "B"
```
- Lambdas (`fun` and `function`)
Currently allowed:
```fs
let (|Member|) f x = f x
let f (x:System.Type) = x.BaseType
match typeof with
| Member f (Member f null) -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|Member|) f x = f x
match typeof with
| Member (fun x -> x.BaseType) (Member (fun x -> x.BaseType) null) -> printfn "A"
(*
error FS0010: Unexpected keyword 'fun' in pattern. Expected ')' or other token.
error FS0583: Unmatched '('
error FS0010: Unexpected symbol ')' in implementation file
error FS0010: Unexpected keyword 'fun' in pattern. Expected ')' or other token.
error FS0583: Unmatched '('
error FS0010: Unexpected symbol ')' in implementation file
error FS0001: Type mismatch. Expecting a
'System.Type -> 'a'
but given a
'('b -> 'c) -> 'b -> 'c'
The type 'System.Type' does not match the type ''a -> 'b'
*)
| _ -> printfn "B"
```
(Imagine this with #506 :wink:)
```fs
let (|Member|) f x = f x
match typeof with
| Member _.BaseType (Member _.BaseType null) -> printfn "A"
| _ -> printfn "B"
```
(Partially applied operators are already ok too!)
```fs
let (|Apply|) f x = f x
match 1 with
| Apply ((+) 1) (Apply ((+) 1) 3) -> printfn "A"
| _ -> printfn "B"
```
- Computation expressions
Currently allowed:
```fs
let (|SeqEqual|_|) a = Seq.map2 (=) a >> Seq.fold (&&) true >> function true -> Some SeqEqual | false -> None
let otherSeq = seq { 1; 2; 3 }
match seq { 1; 2; 3 } with
| SeqEqual otherSeq -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|SeqEqual|_|) a = Seq.map2 (=) a >> Seq.fold (&&) true >> function true -> Some SeqEqual | false -> None
match seq { 1; 2; 3 } with
| SeqEqual (seq { 1; 2; 3 }) -> printfn "A"
(*
error FS0010: Unexpected integer literal in pattern. Expected identifier, 'global' or other token.
error FS0583: Unmatched '('
error FS0010: Unexpected integer literal in pattern. Expected identifier, 'global' or other token.
error FS0583: Unmatched '('
error FS0723: Invalid argument to parameterized pattern label
*)
| _ -> printfn "B"
```
- Records
Already allowed:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
type X = { x : int }
let x = { x = 3 }
match { x = 3 } with
| Eq x -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
type X = { x : int }
match { x = 3 } with
| Eq { x = 3 } -> printfn "A"
// error FS0723: Invalid argument to parameterized pattern label
| _ -> printfn "B"
```
- Anonymous records
Already allowed:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
let x = {| x = 3 |}
match {| x = 3 |} with
| Eq x -> printfn "A"
| _ -> printfn "B"
```
Should allow:
```fs
let (|Eq|_|) x = (=) x >> function true -> Some Eq | false -> None
match {| x = 3 |} with
| Eq {| x = 3 |} -> printfn "A"
// error FS0010: Unexpected symbol '{|' in pattern
// error FS0010: Unexpected symbol '{|' in pattern
| _ -> printfn "B"
```
The existing way of approaching this problem in F# is to annoyingly put the argument in a `let` binding elsewhere!
## Pros and Cons
The advantages of making this adjustment to F# are
1. Consistency with the rest of F#
2. Convenience when using active patterns
3. Concise matching with active patterns
The disadvantages of making this adjustment to F# are (none).
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions: (put links to related suggestions here)
https://github.com/fsharp/fslang-suggestions/issues/758 - This while only being scoped to `typeof`.
## Affidavit (please submit!)
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reviewing the proposed active-pattern examples and the linked typeof suggestion. Compare the currently allowed arguments with the requested property access, typeof applications, lambdas, computation expressions, records, and anonymous records. Done means the language design accepts these forms consistently, with appropriate tests and diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100