3

This code:

type Result = Success of string

type Tracer() =
  member x.Bind(p: Result, rest: (string -> Result)) = 
    match p with
    | Success s -> rest s

let tracer = new Tracer()

let t = tracer {
  let! x = Success "yes!"
  let! y = Success "waste of time"
  return! Success x
}

printfn "%A" t

prints Success "yes!"

But gives a warning that implies that it shouldn't work:

File1.fs(19,3): warning FS0708: This control construct may only be used if the computation expression builder defines a 'ReturnFrom' method

Seems like a strange warning: if it's right, then the code shouldn't work. Is it really just saying that the builder had to synthesize ReturnFrom?

(F# Version 1.9.7.4, compiling for .NET Framework Version v4.0.21006)

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
James Moore
  • 8,636
  • 5
  • 71
  • 90

2 Answers2

6

I sent the question to fsbugs@microsoft.com, and it's a bug. They said it'll be an error in the next release.

(They responded almost immediately, on Thanksgiving no less - it just took me a while to put the info here.)

James Moore
  • 8,636
  • 5
  • 71
  • 90
4

I'm surprised that this works. Section 6.10 of the spec doesn't mention anything about synthesizing ReturnFrom when it's not specified. Is there a reason not to just put a member x.ReturnFrom v = v on the builder? Or a member x.Return(v) = Success v, so that you could end your tracer block with return x, which would be more traditional?

kvb
  • 54,864
  • 2
  • 91
  • 133
  • No reason to not do any of those things - this was just some example code I was playing around with, and I couldn't understand what the error was trying to tell me. I mailed this off to fsbugs, we'll see what they have to say. – James Moore Nov 26 '09 at 18:12