dotnet / dotnet/fsharp

Unclear error message for CE custom operations

Open
#10,662 0 comments 0 reactions 1 assignee Claimed by @edgarfgp View on GitHub
Area-Diagnostics Feature Request
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

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

**Repro steps**

Consider these two versions of a file:
```fsharp
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, b:string -> Result) =
match a with
| Error x -> Error x
| Ok foo -> b foo
[]
member __.Nothing<'T>(before:Result<'T,unit>) = before

let result = ResultBuilder()
let stuff =
result {
do! (Ok "hello")
nothing
return ()
}
```
and
```fsharp
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, b:string -> Result) =
match a with
| Error x -> Error x
| Ok foo -> b foo
[]
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.