5 ms·
Great article. Rust struct and Haskell records work pretty much the same way, too: // Rust Item { name, price } Item { name: name, price: price } matc
by sshine 4y 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/record_wildcards.html https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/reco...
- masklinn 4y agoFWIW 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)
- naasking 4y agoThere's one important difference: aren't Rust structs unboxed by default, where in Haskell they are boxed by default?
- ghkbrew 4y agoThis 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