Reverse data constructor
How can I define a function that will accept my type and return its primi开发者_开发百科tive "synonym"? For example:
newtype MyInt = MakeInt Int
And i want a function:
unMyInt :: MakeInt -> Int
The other (and more comfortable way sometimes) is record syntax:
newtype myInt a = MyInt { unMyInt :: Int }
This automatically defines a function
unMyInt :: MyInt -> Int
By pattern matching on the constructor:
unMyInt (MakeInt i) = i
精彩评论