Fsharp: Parameterized active patterns should support generic functions

Created on 13 Aug 2018  路  2Comments  路  Source: dotnet/fsharp

I see nothing in the F# spec that prevents this, but still let me know if this should be in the language-suggestions repo.

We should allow the following code to compile:

open System

// the real version of this has more logic and cases for matching on F# function types
let (|IsType|_|) expected (actual: Type) = if expected = actual then Some actual else None

let typeToString t =
    // I would expect this to work with and without parenthesis
    match t with
    | IsType (typeof<int>) _ -> "int"
    | IsType typeof<string> _ -> "string"
    | ...

Currently F# gives a parsing error with FS0010: Unexpected identifier in pattern. Expected infix operator, quote symbol or other token. for the int part.

The workaround is the following, but which makes using active patterns in this particular usage not worthwhile:

let typeToString t =
    let tint, tstr = typeof<int>, typeof<string>
    match t with
    | IsType tint _ -> "int"
    | IsType tstr _ -> "string"
    | ...
Area-Compiler Feature Request

Most helpful comment

This is a missing case in let rec convSynPatToSynExpr x =

The F# language spec should really over which syntactic forms are converted.

But more importantly, F# pattern syntax doesn't actually have any syntax for type applications today, which is the core problem here. I think it is easy enough to add one but it would involve adding a case to the parser.

So Id be very happy to see this addressed but we should use a short RFC for it (consider it pre-approved)

All 2 comments

Agreed on filing this here (it doesn't warrant a language suggestion and RFC) - will happily accept a PR for this.

This is a missing case in let rec convSynPatToSynExpr x =

The F# language spec should really over which syntactic forms are converted.

But more importantly, F# pattern syntax doesn't actually have any syntax for type applications today, which is the core problem here. I think it is easy enough to add one but it would involve adding a case to the parser.

So Id be very happy to see this addressed but we should use a short RFC for it (consider it pre-approved)

Was this page helpful?
0 / 5 - 0 ratings