← Back to context

Comment by nh2

10 hours ago

In Haskell they are a little less annoying. It is just easier to reason about (including proving) pure functions.

I meant the constrained types by hiding the constructors. Super annoying, not automatically convertible, in Haskell you have to remember what the fake constructor is called, and write it every time you use it, but at least it's efficiently implemented with newtype, unlike the Java OOP version. Think about writing a value with several nested constrained types, like NonEmptyListOne (makeNonZeroNumber 42, 'h' `NonEmptyString` "ello world"). It's just really annoying.

  • The blog link I mentioned avoids this cost with literals, by providing using a required type argument to check the string length at compile time without TH. It requires a relatively recent GHC:

        make :: forall symbol -> (IsNonEmptySymbol symbol) => NonEmptyText
    
        type family IsNonEmptySymbol symbol :: Constraint where
          IsNonEmptySymbol "" = Unsatisfiable (Text "Expected a non-empty string")
          IsNonEmptySymbol _ = (()::Constraint) -- empty constraint is always satisfied