使用 typeof<_>;处于活跃模式
给定以下人为的活动模式:
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
)?
Given the following contrived active pattern:
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
The following:
let dict = System.Collections.Generic.Dictionary<string,obj>()
match dict with
| TypeDef typedefof<Dictionary<_,_>> typeArgs -> printfn "%A" typeArgs
| _ -> ()
gives the error:
Unexpected type application in pattern matching. Expected '->' or other token.
But this works:
let typ = typedefof<Dictionary<_,_>>
match dict with
| TypeDef typ typeArgs -> printfn "%A" typeArgs
| _ -> ()
Why is typedefof
(or typeof
) not allowed here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您使用参数化活动模式(其中参数是某个表达式),编译器也会将参数解析为模式(而不是表达式),因此语法受到更多限制。
我认为这本质上与此处讨论的问题相同:如何将复杂表达式传递给参数化活动模式?(我不确定实际的编译器实现,但 F# 规范表示它应该解析为模式)。
作为解决方法,您可以在引用中编写任何表达式,因此您可以这样做:
Even if you're using a parameterized active pattern (where the argument is some expression), the compiler parses the argument as a pattern (as opposed to an expression), so the syntax is more restricted.
I think this is essentially the same problem as the one discussed here: How can I pass complex expression to parametrized active pattern? (I'm not sure about the actual compiler implementation, but the F# specification says that it should parse as a pattern).
As a workaround, you can write any expression inside a quotation, so you could do this:
添加到托马斯的答案中,这种情况下麻烦的语法似乎与显式类型参数有关。另一种解决方法是使用虚拟参数来传输类型信息
Adding to Tomas' answer, the troublesome syntax in this case appears to be with the explicit type arguments. Another workaround is to use a dummy parameter to transmit the type information