[Haskell-cafe] I really donot know how to use `newtype` ?

Martijn van Steenbergen martijn at van.steenbergen.nl
Fri Nov 27 08:09:33 EST 2009


zaxis wrote:
> then how to use `X` ?  Would you mind explaining the newtype X in detail ?

You can just think of a newtype as a normal datatype:

> data X a = X (ReaderT XConf (StateT XState IO) a)

I.e. construction and pattern matching work indentically.

Every newtype you will find will have exactly one constructor with 
exactly one field. Only datatypes that have one constructor with one 
field can be made newtypes.

You can only use generalized deriving on newtypes:
http://www.haskell.org/ghc/docs/6.10-latest/html/users_guide/deriving.html

There are some other differences between datatypes and newtypes which 
are only sometimes important. They are to do with efficiency and bottom. 
You can read about them here:

http://www.haskell.org/tutorial/moretypes.html

HTH,

Martijn.



More information about the Haskell-Cafe mailing list