你如何在F#报价中构建一个通用的联合案例?

时间:2015-05-20 14:38:41

标签: f# quotations

以下代码失败:

open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations
let (empty,cons) =
    FSharpType.GetUnionCases(typeof<List<_>>)
    |> (fun cases ->
        cases |> Array.find (fun c -> c.Name = "Empty"),
        cases |> Array.find (fun c -> c.Name = "Cons"))

let valuesToList values =
    values
    |> List.map (fun v -> Expr.Value(v))
    |> List.fold
        (fun l v -> Expr.NewUnionCase(cons, [v;l]))
        <@@ List.empty<int> @@>

[1;2;3]
|> valuesToList

除外:

System.ArgumentException: Type mismatch when building 'sum': incorrect argument type for an F# union. Expected 'System.Object', but received type 'System.Int32'.

如何指定Cons union case的泛型参数类型?

1 个答案:

答案 0 :(得分:3)

问题在于.wrapper { width: 80%; margin: 1em auto; overflow: hidden; } .wrapper div { float: left; width: 50%; min-height: 100px; } .wrapper div:nth-child(1) { background: #f66; width: 100%; min-height: 100px; } .wrapper div:nth-child(2) { background: #6f6; } .wrapper div:nth-child(3) { background: #66f; } .wrapper div:nth-child(4) { background: #ff6; } .wrapper div:only-child { width: 100%; min-height: 200px; } .wrapper div:nth-child(2):nth-last-child(1) { width: 100%; min-height: 100px; } .wrapper div:nth-child(3):nth-last-child(1) { width: 50%; min-height: 100px; } .wrapper div:nth-child(2):nth-last-child(2) { width: 50%; min-height: 100px; } .wrapper div:nth-child(1):nth-last-child(4) { width: 50%; min-height: 100px; } .wrapper div:nth-child(2):nth-last-child(3) { width: 50%; min-height: 100px; } .wrapper div:nth-child(3):nth-last-child(1) { width: 50%; min-height: 100px; } 。通配符被推断为GetUnionCases(typeof<List<_>>)。因此错误

  

预期&#39; System.Object&#39;,但收到类型&#39; System.Int32&#39;。

这是一个有效的版本。

obj
相关问题