Visitar URL original
Type inference limitation with records and SRTP · Issue #685 · fsprojects/FSharpPlus · GitHub
Skip to content

Type inference limitation with records and SRTP #685

Open

Description

@gusty

Description

Type inference for records is a bit special in F# and it doesn't play well with SRTPs
I've seen many cases, but here's a cool one:

#r "nuget: FSharpPlus"

open FSharpPlus
type Station = { Icao: string; Lat: float; Lon: float; Raw: string }

let private parseStations (json: string) =
    ["1"; "2"]
    |> List.choose<_, Station> (fun e ->
        let str (n: string) : string option = tryParse<int> n |> Option.map string
        let num (n: string) : float option  = tryParse<int> n |> Option.map float

        applicative {
            let! icao = str "icaoId"
            and! raw  = str "rawOb"
            and! lat  = num "lat"
            and! lon  = num "lon"
            return { Station.Icao = icao; Station.Lat = lat; Station.Lon = lon; Station.Raw = raw }
        })

Note that this fails when it has more that 3 parameters in the final binding (I have 4 in the code)

Known Workarounds

Add type parameter annotation in the choose about the record type:

#r "nuget: FSharpPlus"

open FSharpPlus
type Station = { Icao: string; Lat: float; Lon: float; Raw: string }

let private parseStations (json: string) =
    ["1"; "2"]
    |> List.choose<_, Station> (fun e ->
        let str (n: string) : string option = tryParse<int> n |> Option.map string
        let num (n: string) : float option  = tryParse<int> n |> Option.map float

        applicative {
            let! icao = str "icaoId"
            and! raw  = str "rawOb"
            and! lat  = num "lat"
            and! lon  = num "lon"
            return { Station.Icao = icao; Station.Lat = lat; Station.Lon = lon; Station.Raw = raw }
        })

Expected behavior

Workaround not needed.
This is an F# type inference limitation and should be solved in the F# compiler.

Actual behavior

No response

Configuration

No response

Reproduction Steps

No response

Are you willing to help with a pull-request?

No

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions