我有一个计算表达式,想要返回一个扁平化的元组作为第一个元素,以及一个 int
作为第二个元素。 我试图使用方法重载来实现这一点。 现在编译器报错说它找不到唯一的重载。我不知道如何帮助编译器弄清楚。对我来说,这似乎是确定性的。
type IntBuilder () =
member inline this.Yield (i:int) =
i
member inline this.For(source:seq<'a>, body:'a -> seq<'b * int>) =
source
|> Seq.collect (fun x -> body x |> Seq.map (fun (idx, i) -> (x, idx), i))
member inline this.For(source:seq<'a>, body:'a -> int) =
source |> Seq.map (fun x -> x, body x)
member inline this.Run(source:seq<('a * ('b * ('c * 'd))) * 'v>) =
source
|> Seq.map (fun ((x, (y, (z, a))), d) -> (x, y, z, a), d)
member inline this.Run(source:seq<('a * ('b * 'c)) * 'v>) =
source
|> Seq.map (fun ((x, (y, z)), d) -> (x, y, z), d)
member inline this.Run(source:seq<('a * 'b) * 'v>) =
source
|> Seq.map (fun ((x, y), d) -> (x, y), d)
member inline this.Run(source:seq<'a * 'v>) =
source
|> Seq.map (fun (x, d) -> x, d)
let intBuilder = IntBuilder ()
let c =
intBuilder {
for i in 1..2 do
for j in 1..2 do
for k in 1..2 do
for l in 1..2 ->
i + j + k + l
}
// What I get
c : seq<(int * (int * (int * int))) * int>
// What I want
c : seq<(int * int * int * int) * int>
在这种情况下,
c
的类型是 seq<(int * (int * (int * int))) * int>
。我希望 IntBuilder
计算返回 seq<(int * int * int * int), int>
。我该如何做到这一点?