I am new to Haskell and I want to be able to a newtype so I can tell what is what, but I also have to read it from a string. I have
newtype SpecialId Int
deriving (Eq, Ord, Show)
I want to be able to read "5" :: SpecialId
if I derive Read in the newtype it doesn't work it only works on read "SpecialId 5" :: SpecialId
. I have tried
instance Read SpecialId where
readsPrec _ s = read s
But this gives me
SpecialId *** Exception: Prelude.read: no parse
This is possible since GHC 8.2 using -XDerivingStrategies
:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
newtype SpecialId = SpecialId Int
deriving stock (Eq, Ord, Show)
deriving newtype Read
In ghci:
ghci> read "5" :: SpecialId
SpecialId 5