Reverse data constructor

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

newtype MyInt = MakeInt Int

And I want a function:

unMyInt :: MakeInt -> Int
+3
source share
2 answers

By figurative comparison of the constructor:

unMyInt (MakeInt i) = i
+6
source

Another (and more convenient way sometimes) is to write syntax:

newtype myInt a = MyInt { unMyInt :: Int }

This automatically determines the function

unMyInt :: MyInt -> Int
+15
source

Source: https://habr.com/ru/post/1766873/


All Articles