Skip to content

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

Open
@smoothdeveloper

Description

@smoothdeveloper
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Diagnosticsmistakes and possible improvements to diagnosticsArea-LangService-DiagnosticsFCS code analysis, diagnostics, red squiggliesFeature ImprovementTheme-Simple-F#A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.

    Type

    No type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions