tags:

views:

100

answers:

2

How can I define a function that will accept my type and return its primitive "synonym"? For example:

newtype MyInt = MakeInt Int

And i want a function:

unMyInt :: MakeInt -> Int
+5  A: 

By pattern matching on the constructor:

unMyInt (MakeInt i) = i
sepp2k
Thanks, where can I read what this construction means?
Ramesh
@Ramesh: In any haskell tutorial or book (look for the chapter about pattern matching). It means "for any i, the result of applying `unMyInt` to the value `MakeInt i` is `i`".
sepp2k
@Ramesh: For example take a look at the [section about pattern matching](http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#deftypes.pattern) from Real World Haskell.
sepp2k
+12  A: 

The other (and more comfortable way sometimes) is record syntax:

newtype myInt a = MyInt { unMyInt :: Int }

This automatically defines a function

unMyInt :: MyInt -> Int
FUZxxl
Who downvoted me? Can he please explain?
FUZxxl