Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow explicit type signatures for active pattern parameters #758

Open
Happypig375 opened this issue Jun 20, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@Happypig375
Copy link
Contributor

commented Jun 20, 2019

Allow explicit type signatures for active pattern parameters

Consider

let (|Eq|_|) v x = if v = x then Some Eq else None
let target = typeof<int>
let target2 = 3
let f = function Eq target -> "An int!" | _ -> "Not int..."
let g = function Eq target2 -> "Three!" | _ -> "Not three..."

Now we inline target2.

let (|Eq|_|) v x = if v = x then Some Eq else None
let target = typeof<int>
let f = function Eq target -> "An int!" | _ -> "Not int..."
let g = function Eq 3 -> "Three!" | _ -> "Not three..." // ✓

Now we try to inline target.

let (|Eq|_|) v x = if v = x then Some Eq else None
let f = function Eq typeof<int> -> "An int!" | _ -> "Not int..." // error FS0010: Unexpected type application  in pattern matching. Expected '->' or other token.
let g = function Eq 3 -> "Three!" | _ -> "Not three..."

The error is unexpected and this should be allowed.

This has been an area of unexpectedness, as shown in https://stackoverflow.com/questions/6168851/how-can-i-pass-complex-expression-to-parametrized-active-pattern and https://stackoverflow.com/questions/6779602/use-of-typeof-in-active-pattern.
From an answer to the former question:

The langauge of expressions that can be used as arguments for parameterized active patterns is limited in some ways. As far as I can tell, the F# specification doesn't say that explicitly, but the grammar suggests that it must be possible to parse the argument expression as pat-param (page 90):

pat-param :=
| const
| long-ident
| [ pat-param ; ... ; pat-param ]
| ( pat-param, ..., pat-param )
| long-ident pat-param
| pat-param : type
| <@ expr @>
| <@@ expr @@>
| null

Although the above definition may be incomplete, e.g. let k = function Eq [|3; 4; 5|] -> 3 | _ -> 4 compiles, this shows that this should be an oversight and not a deliberate design decision.

The existing way of approaching this problem in F# is via a let binding, as shown above.

Pros and Cons

The advantages of making this adjustment to F# are

  1. Consistency
  2. Convenience
  3. Conciseness

Disadvantages: None known yet.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: Nope

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • 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:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

P.S. I hit https://twitter.com/githubstatus/status/1141629836202532864 while submitting... Resulted in me re-typing this. Ouch. Good thing this is fixed.

@yatli

This comment has been minimized.

Copy link

commented Jun 20, 2019

Slightly off-topic, but more of a general comment to your proposals:

The disadvantage of making this adjustment to F# is the implementation effort required.

This is not really a disadvantage, and the information is redundant (quantified by 'S' already).
So you'd probably say:
Disadvantages: None known yet.
:)

@Happypig375

This comment has been minimized.

Copy link
Contributor Author

commented Jun 20, 2019

Thanks, updated the proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.