fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Unexpected FS0812 when property has same name as DU case

Open cmeeren opened this issue 1 year ago • 4 comments

Consider this code:

type MyId =
    | IdA of int
    | IdB of string

    member this.IdA =
        match this with
        | IdA x -> Some x
        | _ -> None


let onlyIdA (ids: MyId list) = ids |> List.choose _.IdA

The last line fails to compile with an error on _.IdA:

Error FS0812 : The syntax 'expr.id' may only be used with record labels, properties and fields

I would expect this to compile, since IdA is indeed a property.

cmeeren avatar Feb 02 '24 08:02 cmeeren

This is not related to shorthand lambda, it also fails for normal lambdas:

let onlyIdA (ids: MyId list) = ids |> List.choose (fun x -> x.IdA)
                                                            ^^^^^

cmeeren avatar Feb 02 '24 12:02 cmeeren

We should probably add a warning when defining a member with the same name as a DU case.

We might also want to fix the current error message.

0101 avatar Feb 12 '24 17:02 0101

It for sure has been like that for some time, definitely not a bug.

vzarytovskii avatar Feb 20 '24 09:02 vzarytovskii

This is likely to be an error recovery issue FS0812 is reported using error(Error(.....)) so it will hide the previous raised error.

I think FS0812 should use errorR(Error(....)) to allow error cascading as expected

edgarfgp avatar Feb 23 '24 11:02 edgarfgp