开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜