Hacker News new | ask | show | jobs
by sshine 1221 days ago
Great article.

Rust struct and Haskell records work pretty much the same way, too:

  // Rust
  Item { name, price }
  Item { name: name, price: price }
  match item {
    Item { name, .. } => todo!(),
  }
corresponding to

  -- Haskell
  Item { item, price }
  Item { item = item, price = price }
  case item of
    Item { name, .. } -> undefined
Haskell has some opt-in flexibility wrt. packing and unpacking of field names [1].

[1]: https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/reco...

2 comments

FWIW if you’re just “unpacking” a structure `match` / `case` is unnecessary syntactic overhead, you can just use a regular let:

    // rust
    let Item { name, .. } = item;

    -- haskell
    let Item { name } = item in …
(Note: the haskell version requires NamedFieldPuns, or RecordWildCards for something like Rust’s version)
There's one important difference: aren't Rust structs unboxed by default, where in Haskell they are boxed by default?
This is true. Though I believe GHC is pretty aggressive about unboxing (when posible), but don't quote me on that.

Another possible gotcha is that by default Haskell records introduce a named accessor function into the surrounding scope. So defining two records with a `name` field next to each other is an error