开发者

Reverse data constructor

开发者 https://www.devze.com 2023-01-18 07:28 出处:网络
How can I define a function that will accept my type and return its primi开发者_开发百科tive \"synonym\"? For example:

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

精彩评论

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