{-# OPTIONS -fglasgow-exts #-}
module Typecase2 (tests) where
import Test.HUnit
import Data.Generics
import Data.Maybe
data MyData = MyCons String deriving (Typeable, Data)
f :: Data 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 :: MyData -> String
mytys a = "got my data: " ++ gshow a
others = "got something else: " ++ gshow 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 my data: (MyCons \"42\")"
, "got something else: (True)")