{-# OPTIONS -fglasgow-exts #-}
module Typecase1 (tests) where
import Test.HUnit
import Data.Typeable
import Data.Maybe
data MyTypeable = MyCons String deriving (Show, Typeable)
f :: (Show a, Typeable a) => a -> String
f a = (maybe (maybe (maybe others
mytys (cast a) )
float (cast a) )
int (cast a) )
where
int :: Int -> String
int a = "got an int, incremented: " ++ show (a + 1)
float :: Float -> String
float a = "got a float, multiplied by .42: " ++ show (a * 0.42)
mytys :: MyTypeable -> String
mytys a = "got a term: " ++ show a
others = "got something else: " ++ show a
tests = ( f (41::Int)
, f (88::Float)
, f (MyCons "42")
, f True) ~=? output
output = ( "got an int, incremented: 42"
, "got a float, multiplied by .42: 36.96"
, "got a term: MyCons \"42\""
, "got something else: True")