type, newtype & data
24 Jan 2013Introduction
This post is just a small write up on the differences between these three keywords and their uses within Haskell.
type
Using the type
keyword is the same as just referring to the actual type that you’ve declared. In other words, type
just lets you make a synonym for the original type.
Using these, we’ll be able to refer to a Card
list as a Deck
. They can be used interchangeably as they’ll directly cast. All this really does for us, is gives a pre-existing type a more meaningful name to our code.
newtype
Using the newtype
keyword, we make a thin wrapper around an existing type. Something that will be treated differently at compile time, but will be directly translatable (and not subject to conversion) at runtime.
newtype
only allows you one constructor and one field. It’s important that its used when you’re creating data entries that have these constraints on them. These attributes make newtype
a great candidate for when you just want to add a typeclass instance to an existing type.
data
The data
keyword allows you to build much more complex types. With the data
keyword, you can have as many constructors and fields as you like.
Of course, it would make more sense to use “record syntax” when defining these datatypes above.
Wrapping up
- Use
type
to give your types more meaningful names - Use
newtype
if you just want to take an existing type and add a typeclass instance to it - Use
data
if you want to create your own datatype