f# - Use of typeof<_> in active pattern -
given following contrived active pattern:
let (|typedef|_|) (typedef:type) (value:obj) = if obj.referenceequals(value, null) none else let typ = value.gettype() if typ.isgenerictype && typ.getgenerictypedefinition() = typedef some(typ.getgenericarguments()) else none
the following:
let dict = system.collections.generic.dictionary<string,obj>() match dict | typedef typedefof<dictionary<_,_>> typeargs -> printfn "%a" typeargs | _ -> ()
gives error:
unexpected type application in pattern matching. expected '->' or other token.
but works:
let typ = typedefof<dictionary<_,_>> match dict | typedef typ typeargs -> printfn "%a" typeargs | _ -> ()
why typedefof
(or typeof
) not allowed here?
even if you're using parameterized active pattern (where argument expression), compiler parses argument pattern (as opposed expression), syntax more restricted.
i think same problem 1 discussed here: how can pass complex expression parametrized active pattern? (i'm not sure actual compiler implementation, f# specification says should parse pattern).
as workaround, can write expression inside quotation, this:
let undef<'t> : 't = unchecked.defaultof<_> let (|typedef|) (typeexpr:expr) (value:obj) = let typedef = typeexpr.type.getgenerictypedefinition() // ... let dict = system.collections.generic.dictionary<string,obj>() match dict | typedef <@ undef<dictionary<_,_>> @> typeargs -> printfn "%a" typeargs | _ -> ()
Comments
Post a Comment