I have a computation expression which I want to return a flattened tuple as the first element and an int
as the second. I am trying to use method overloading to accomplish this. Right now the compiler is throwing an error saying it cannot find a unique overload. I do not know how to help the compiler figure it out. It appears deterministic to me.
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>
In this case c
is of type seq<(int * (int * (int * int))) * int>
. I want the IntBuilder
computation to return seq<(int * int * int * int), int>
. How do I make this happen?