2012-10-15 66 views
4

在新的控制檯應用程序中,只粘貼以下代碼會導致異常「參數不是可識別的方法名稱」。F中的引用和模式匹配#

  • 以下代碼是否適用於您的安裝?
  • 小丑:你知道爲什麼它不起作用嗎?

// 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 

望着主動模式的定義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)) 
+0

當我在Visual Studio 2012中粘貼來自** Edit#2 **的代碼時,它對我來說工作正常。你可以在某處分享編譯好的程序集,也許有一些區別......另外,你使用的是什麼版本的F#(等等)? –

+0

這很奇怪。我們如何追蹤所有這些信息?融合日誌? – nicolas

+0

不確定,但是如果你從你的機器上共享編譯好的'exe',我可以試着運行它來查看我們正在使用的編譯器還是在裝載的'FSharp.Core.dll'中是否有區別。 –

回答

2

隨口說說的,看起來還好我...有沒有可能是你」莫名其妙地影響了var的原始定義?例如,以下自包含的示例適用於我:

let var<'a>() = Unchecked.defaultof<'a> 

match <@ var<int>() @> with 
| Quotations.DerivedPatterns.SpecificCall <@ var @> (obj,_,[]) -> 
    printfn "var" 
| _ -> printfn "something else" 
+0

我應該在郵件中說過,我試圖做類似於你的代碼的東西,並且有同樣的錯誤。從你的例子複製/粘貼也一樣。我會嘗試在不同的上下文(清理項目,fsharp版本等)中找到,這是令人費解的。 – nicolas

+0

對我來說,將編輯中的代碼粘貼到新項目中可以正常工作。我正在運行VS2012。 – kvb

+0

感謝您的檢查。這很奇怪。我會嘗試在我家安裝.... – nicolas