Skip to content

Instantly share code, notes, and snippets.

@phadej
Created August 7, 2020 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/adee6598fad8805f5dcd18c580c667e3 to your computer and use it in GitHub Desktop.
Save phadej/adee6598fad8805f5dcd18c580c667e3 to your computer and use it in GitHub Desktop.
import Data.Typeable (Typeable, typeRep)
import Data.Proxy (Proxy (..))
import Text.Read (readMaybe)
import GHC.Stack (HasCallStack)
import Prelude hiding (read)
read :: forall a. (Typeable a, Read a, HasCallStack) => String -> a
read s = case readMaybe s of
Just x -> x
Nothing -> error
$ showString "read @" . showsPrec 11 (typeRep (Proxy :: Proxy a))
. showChar ' ' . showsPrec 11 s
$ ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment