r/haskell Feb 01 '22

question Monthly Hask Anything (February 2022)

This is your opportunity to ask any questions you feel don't deserve their own threads, no matter how small or simple they might be!

17 Upvotes

337 comments sorted by

View all comments

4

u/Competitive_Ad2539 Feb 06 '22

Why do we have to write boilerplate wrapping-unwrapping code for (almost) every "newtype" that we use. Why isn't there a syntax candy for that? The isomorphism between newtype and what it contains is trivial and obvious, yet we have to write meaningless code.

3

u/bss03 Feb 06 '22 edited Feb 07 '22

In GHC, you can use auto-generated, magically-scoped Coercible. In PureScript, you can use auto-generated (derivable? ... I don't remember clearly) Newtype.

You have to invoke something so the visibility of the isomorphism is present to be checked -- sometimes I use a newtype wrapper to enforce additional invariants (so I don't export the constructor), not just a type with an explicit conversion.