[英]Quotations and pattern matching in F#
In a new console application, just pasting the following code leads to the exception "The parameter is not a recognized method name". 在新的控制台应用程序中,仅粘贴以下代码将导致异常“参数不是可识别的方法名称”。
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
let somefunction1 arg = ()
let somefunction2 () = ()
open Quotations.DerivedPatterns
let test() =
let d = <@ somefunction1() @>
let e = <@ somefunction2() @>
match d with
| SpecificCall <@ somefunction1() @> (a,b ,c) -> printfn "somefunction"
| _ -> printfn "something else"
match d with
| SpecificCall <@ somefunction1 @> (a,b ,c) -> printfn "somefunction"
| _ -> printfn "something else"
match e with
| SpecificCall <@ somefunction2() @> (a,b ,c) -> printfn "somefunction"
| _ -> printfn "something else"
//THIS FAILS HERE saying "The parameter is not a recognized method name"
match e with
| SpecificCall <@ somefunction2 @> (a,b ,c) -> printfn "somefunction"
| _ -> printfn "something else"
[<EntryPoint>]
let main argv =
test()
printfn "%A" argv
0 // return an integer exit code
Looking at the definition of active pattern SpecificCall defined in the compiler I find: 查看在编译器中定义的活动模式SpecificCall的定义,我发现:
[<CompiledName("SpecificCallPattern")>]
let (|SpecificCall|_|) templateParameter =
// Note: precomputation
match templateParameter with
| (Lambdas(_,Call(_,minfo1,_)) | Call(_,minfo1,_)) ->
let isg1 = minfo1.IsGenericMethod
let gmd = if isg1 then minfo1.GetGenericMethodDefinition() else null
// end-of-precomputation
(fun tm ->
match tm with
| Call(obj,minfo2,args)
#if FX_NO_REFLECTION_METADATA_TOKENS
when (minfo1.MethodHandle = minfo2.MethodHandle &&
#else
when (minfo1.MetadataToken = minfo2.MetadataToken &&
#endif
if isg1 then
minfo2.IsGenericMethod && gmd = minfo2.GetGenericMethodDefinition()
else
minfo1 = minfo2) ->
Some(obj,(minfo2.GetGenericArguments() |> Array.toList),args)
| _ -> None)
| _ ->
invalidArg "templateParameter" (SR.GetString(SR.QunrecognizedMethodCall))
Offhand, that looks okay to me... Is it possible that you've shadowed the original definition of var
somehow? 副手,对我来说还好...是否可能以某种方式掩盖了
var
的原始定义? For instance, the following self-contained example works fine for me: 例如,以下独立的示例对我来说很好用:
let var<'a>() = Unchecked.defaultof<'a>
match <@ var<int>() @> with
| Quotations.DerivedPatterns.SpecificCall <@ var @> (obj,_,[]) ->
printfn "var"
| _ -> printfn "something else"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.