【发布时间】:2011-07-21 16:37:50
【问题描述】:
给定以下人为的主动模式:
let (|TypeDef|_|) (typeDef:Type) (value:obj) =
if obj.ReferenceEquals(value, null) then None
else
let typ = value.GetType()
if typ.IsGenericType && typ.GetGenericTypeDefinition() = typeDef then Some(typ.GetGenericArguments())
else None
以下内容:
let dict = System.Collections.Generic.Dictionary<string,obj>()
match dict with
| TypeDef typedefof<Dictionary<_,_>> typeArgs -> printfn "%A" typeArgs
| _ -> ()
给出错误:
模式匹配中的意外类型应用。应为“->”或其他标记。
但这有效:
let typ = typedefof<Dictionary<_,_>>
match dict with
| TypeDef typ typeArgs -> printfn "%A" typeArgs
| _ -> ()
为什么这里不允许typedefof(或typeof)?
【问题讨论】:
-
可能只是解析器中的一个错误;在
>>之间加一个空格有帮助吗? -
只是为了清楚并避免“此规则永远不会匹配”的混淆,
TypeDef是否应该是部分活动模式?即(|TypeDef|_|)而不是(|TypeDef|)
标签: f# active-pattern