Skip to content

Arguments in active recognizers cannot be with parens, throws wrongful type mismatch FS0001 #4828

Closed

Description

@abelbraaksma

When you have an active pattern and you want to match against it, an argument cannot be placed within parens, making it impossible to use anything else than literals or variable references.

Repro steps

Consider an active pattern like this:

module Str =
    let (|ContainsAt|_|) position search (original: string) =
        if String.IsNullOrEmpty search then Some ContainsAt
        elif original.LastIndexOf search = position then Some ContainsAt
        else None

And now you apply the pattern:

module testPatterns =
    /// Fails with syntax error
    let testPattern value =
        match value with
        | Str.ContainsAt (Str.length value - 3) ":" -> ()  // FS0001, active recognizer type error

    /// Works
    let testPattern value =
        let pos = (Str.length value - 3)
        match value with
        | Str.ContainsAt pos ":" -> ()

Expected behavior

Parens are meant to group arguments, among others, so I'd expect this to work.

Actual behavior

Instead, I receive an FS0001:

image

In text:

Error FS0001 Type mismatch. Expecting a
'string -> 'a option'
but given a
'int -> string -> string -> unit option'
The type 'string' does not match the type 'int'

Known workarounds

Create variable bindings instead. I don't know of any workaround with syntax alone.

Related information

Recent F# versions.

Activity

  1. dsyme commented on May 13, 2018

    @dsyme
    Contributor

    This is by spec. Arguments to active patterns are limited in the syntactic forms - in this case they can't be operator expressions. See the F# language spec and add a language suggestion if you like. Your workaround is correct.

  2. abelbraaksma commented on May 18, 2018

    @abelbraaksma
    ContributorAuthor

    Ok. I tried to read that part on the syntax in the spec, but didn't come to the same conclusion, but then again, parsing a language BNF-style that I'm not too acquainted with... ah well. I think there's enough new ideas for now that are waiting implementation, I'm sure this one is a nice-to-have in a further future ;).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions