You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Arguments in active recognizers cannot be with parens, throws wrongful type mismatch FS0001 #4828
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:
moduleStr =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:
moduletestPatterns =/// Fails with syntax errorlettestPattern value =match value with| Str.ContainsAt (Str.length value -3)":"->()// FS0001, active recognizer type error/// WorkslettestPattern value =letpos=(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:
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.
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.
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 ;).
Activity
dsyme commentedon May 13, 2018
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.
abelbraaksma commentedon May 18, 2018
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 ;).