If you want proper type safety, you should use newtype
. This is designed to prevent you from implicitly converting back and forth to prevent accidental mistakes.
If you simply want a type alias that doesn’t get in your way, you should use the type keyword:
type Username = Text
This means a function that takes a Username
can take Text
without needing to call a value constructor, etc. In fact, String
is defined as type String = [Char]
so you can supply a [Char]
wherever you need a string; they’re equivalent. (Coincidentally, the inefficiency of using linked lists for strings rather than vectors is why Text
and ByteString
exist.)
You will still need conversions to make it a String
, but with this approach there is no distinction between the Username
and Text
types other than the semantic hint for people reading your code.
If you want conversions to and from String
, I’m not too familiar with those language extensions but they look like they’ll do the trick, as long as you also use a type
alias rather than a newtype
definition.
CLICK HERE to find out more related problems solutions.