I tried the following:
let inline (|OpAdd|_|) (aty:Type, x:obj, y:obj) =
if aty.Equals(typeof<'a>) then Some(box ((unbox<'a> x) + (unbox<'a> y)))
else None
//FSI given signature:
//val inline ( |OpAdd|_| ) : Type * obj * obj -> obj option
Which gives no warnings or errors, but I can’t figure out how to pass the explicit type argument at the call site and it seems that 'a is always inferred as int.
When I try to place the explicit parameter in the definition, I get a couple warnings and errors:
let inline (|OpAdd|_|)<'a> (aty:Type, x:obj, y:obj) =
if aty.Equals(typeof<'a>) then Some(box ((unbox<'a> x) + (unbox<'a> y)))
else None
warning FS1189: Type parameters must be placed directly adjacent to the type name, e.g. "type C<'T>", not type "C <'T>"
error FS0001: The declared type parameter 'a' cannot be used here since the type parameter cannot be resolved at compile time
Is it possible for active patterns to have explicit type parameters? If so how do I define and use them?
I’m not sure whether there is a clean way to do this (probably not, but I may be wrong).
As a dirty workaround, you can add a dummy parameter of type
'T(when working with primitive types that have easy-to-create values) or of typeExpr<'T>(when you don’t really want to create an instance). Then you can use the pattern with some dummy parameter that specifies the type: