Record type inference for dummies

(haskellforall.com)

80 points | by g0xA52A2A 5 days ago ago

10 comments

  • PashaGo 2 days ago ago

    Anonymous records feel like one of those features that are obviously useful once you work with JSON-heavy systems, but surprisingly uncommon in statically typed languages

    • Timwi 21 hours ago ago

      If a statically typed language has no support for record type (or the support it does have is insufficient for your purposes) then you are forced to find other ways to express your logic, so over time those other ways start feeling more natural due to familiarity, and then you end up feeling that you don't need record types (or more extensive support for them), which in turn leads language designers/implementors to leave it out, and so the cycle repeats.

      I'm not sure I agree that anonymous records are “obviously useful” when working with JSON. JSON is not typed, so if you want your deserialized JSON to be statically typed, you need to declare typed records outside of the JSON, so I'd say it's far more useful to use type declarations that are not anonymous.

      That said, anonymous types are incredibly useful in general. I work with C# a lot and its support for them is rather shallow. In particular, there is no “with” expression equivalent.

  • vatsachak 2 days ago ago

    An anonymous record type in a language whose type system is an enum LangType in Rust is just HashMap<String, Box<LangType>>

    My pet project requires an STLC with anonymous record types and type inference for anonymous records wasn't too bad

    • wavemode 2 days ago ago

      Anonymous records aren't complex in principle. The main challenge Haskell has always faced in this area, is the simple fact that its type system (and syntax) weren't designed for anonymous records from the start, so they have to be shoehorned in without breaking existing semantics.

      PureScript has much better support for row polymorphism for this reason.

    • 2 days ago ago
      [deleted]
  • antonvs 2 days ago ago

    The article missed Go’s anonymous structs:

        p := struct {
            Name string
            Age  int
        }{
            Name: "Alice",
            Age:  30,
        }
    • Timwi 21 hours ago ago

      Technically anonymous I suppose because the struct is not named, but the article is mostly about type inference while your example has every type made explicit.

      • antonvs 12 hours ago ago

        The article mentions Typescript and Purescript under the heading "Static typing" - their definitions correspond exactly to the Go definition, with explicit types for the members.

        I was just saying that for completeness, Go should probably be mentioned in that list since it's become a pretty significant language.

        (For the record, I'm an absolute Go hater, but I'm also a PL enthusiast, so...)

        • Gabriel439 10 hours ago ago

          Author here: those type annotations are optional in the case of TypeScript and PureScript; I only included the type annotations for clarity

          That said, I didn't know about Go's support for inline struct definitions like that!

  • moozechen a day ago ago

    [flagged]