[GHC] #13592: Newtype type class with compiler generated instances

GHC ghc-devs at haskell.org
Wed Apr 19 14:33:05 UTC 2017


#13592: Newtype type class with compiler generated instances
-------------------------------------+-------------------------------------
        Reporter:  Iceland_jack      |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.1
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by RyanGlScott):

 I'm leery about adding more magic classes to GHC, but fortunately, this
 magic already exists as `Data.Coerce.Coercible`:

 {{{#!hs
 import Data.Coerce

 pack :: Coercible on n => on -> n
 pack = coerce

 unpack :: Coercible n on => n -> on
 unpack = coerce

 newtype Example = Example String deriving Show

 main :: IO ()
 main = do
   print (pack "hello" :: Example)
   print (unpack (Example "world") :: String)
 }}}

 Does this suit your needs?

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13592#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list