dotnet / dotnet/fsharp

Improve error reporting: Fluent OO code fails if method argument application is not tied to the method name

Open
#12,586 1 comment 0 reactions 0 assignees View on GitHub
Area-Diagnostics Area-LangService-Diagnostics Feature Improvement Theme-Simple-F#
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

```fsharp

type Fluent() =
member x.ASomething(a: int) = x
member x.BNothing(b: int) = x
member x.CEverything(c: int) = x

let f =
Fluent()
.ASomething(1)
.BNothing(2)
.CEverything(3)

let unchainedFluentOk =
Fluent()
.ASomething (1)

let chainedFluentFail =
Fluent()
.ASomething (1) // FS0003 error here
.BNothing (2) // FS0039 error here
.CEverything (3)
```

> error FS0003: This value is not a function and cannot be applied
> error FS0039: The type 'Int32' does not define the field, constructor or member 'BNothing'.

There is a bit of a surprise considering `unchainedFluentOk` type checks to `Fluent`, not `Int32`, and the error message doesn't point at the issue.

**Describe the solution you'd like**

I'm not sure it can be changed to compile because it is indeed ambiguous, `(1)` is same as `1` when it is standing on its own in the code.

Maybe an error message specific to the case of method invocation being chained could improve the situation when facing the error but the user not figuring it is the argument spacing which makes the type checking fail.

**Describe alternatives you've considered**

Finding a way that would make it work, which is backward compatible, and doesn't introduce surprises in other contexts.

Maybe that requires boxing an ambiguity in the AST, which gets resolved during type checking, I'm not sure that is a good idea though.

**Additional context**

related: #1103, #11481

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.