I'm parsing code quotations in FSharp and am building up pattern helpers. All was going well till I tried
let (|BinaryFn|_|) fn (input:Expr) =
function
| SpecificCall fn (_,_,l::r::[]) -> Some(l,r)
| _ -> None
let (|Multiply|_|) x =
function
| BinaryFn <@ (*) @> (l,r) -> Some(l,r)
| _ -> None
The intention is to have a generic binary function matcher that returns the ''left'' and the ''right'' and then create specialised binary matchers such as Multiple, Divide and Add and Subtract.
However I get an error on the second pattern that
Error FS0001: Type mismatch. Expecting a
'a -> 'b option
but given a
'a -> 'c -> (Expr * Expr) option
The type ''a option' does not match the type
''b -> (Expr * Expr) option' (FS0001) (Shambolics)
Can somebody please enlighten me on what I should be doing here?
The issue here is that function
doesn't only pattern-match the last argument, but also adds an additional argument (function
is a combination between of fun
and match
). Remove the function argument input
from the first pattern, and your problem will be solved.