I really donot know how to use `newtype` ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

I really donot know how to use `newtype` ?

z_axis
import Control.Monad.State
import Control.Monad.Reader

data XConf = XConf {}
data XState = XState {}

newtype X a = X (ReaderT XConf (StateT XState IO) a)

> :t StateT
StateT :: (s -> m (a, s)) -> StateT s m a

> :t ReaderT
ReaderT :: (r -> m a) -> ReaderT r m a

then how to use `X` ?  Would you mind explaining the newtype X in detail ?

Sinerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: I really donot know how to use `newtype` ?

Martijn van Steenbergen-2
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.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe