fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Unclear error message for CE custom operations

Open csgordon opened this issue 5 years ago • 0 comments

The error messages for custom operations in CEs are unclear and sometimes contradictory

Repro steps

Consider these two versions of a file:

type ResultBuilder() =
    member __.Return<'T,'TError>(x:'T) : Result<'T,'TError> = Ok x
    member __.Yield<'T,'TError when 'TError :> obj>(x:'T) : Result<'T,'TError> = Ok x
    member __.Bind<'TError when 'TError :> obj>(a:Result<string,'TError>, b:string -> Result<unit,'TError>) = 
        match a with
        | Error x -> Error x
        | Ok foo -> b foo
    [<CustomOperation("nothing")>]
    member __.Nothing<'T>(before:Result<'T,unit>) = before

let result = ResultBuilder()
let stuff =
    result {
        do! (Ok "hello")
        nothing
        return ()
    }

and

type ResultBuilder() =
    member __.Return<'T,'TError>(x:'T) : Result<'T,'TError> = Ok x
    member __.Yield<'T,'TError when 'TError :> obj>(x:'T) : Result<'T,'TError> = Ok x
    member __.Bind<'TError when 'TError :> obj>(a:Result<string,'TError>, b:string -> Result<unit,'TError>) = 
        match a with
        | Error x -> Error x
        | Ok foo -> b foo
    [<CustomOperation("nothing")>]
    member __.Nothing<'T>(before:Result<'T,unit>) = before

let result = ResultBuilder()
let stuff =
    result {
        nothing
    }

Expected behavior

I would expect both versions of the file to complain about the same thing, or both work.

Actual behavior

The second file compiles just fine. The first file gives an error on the line that invokes the custom operation:

This control construct may only be used if the computation expression builder defines a 'For' methodF# Compiler(708)

This error message makes it seem like using custom operations always requires implementing For. But the second example shows this isn't strictly true. And the error message is not clear enough to fix the error. Arguably this is sort of a documentation request as well, as the docs for computation expressions are very light on details for custom operations.

Known workarounds

None known to me.

Related information

Provide any related information (optional):

  • Operating system: macOS Catalina (10.15)
  • .NET Runtime: .NET Core 5.0.100
  • Editing Tools: Visual Studio Code + Ionide

csgordon avatar Dec 09 '20 20:12 csgordon