[Haskell-cafe] Re: GATD and pattern matching
felipe.lessa at gmail.com
Fri Jun 11 19:46:02 EDT 2010
On Sat, Jun 12, 2010 at 12:13:14AM +0200, Dupont Corentin wrote:
> Thanks all, it works fine (see below).
> I lamentably try to make the same for show:
> > showTypeable :: (Typeable a) => a -> String
> > showTypeable x = case cast x of
> > Just x' -> show x'
> > Nothing -> ""
> Because it really upsets me to add this show constraints to the Equ
> constructor ;)
> what if i want to make an Obs instance with non showable elements, with no
> intention to show it of course?
Ad hoc solution:
class MaybeShow a where
maybeShow :: a -> Maybe String
instance Show a => MaybeShow a where
maybeShow = Just . show
instance MaybeShow a where
maybeShow = Nothing
data MyData where
Something :: MaybeShow a => a -> MyData
instance MaybeShow MyData where
maybeShow (Something x) =
fmap (\s -> "Something (" ++ s ++ ")") (maybeShow x)
Hahahaha :). Try to guess without using GHC/GHCi:
1) Which extensions are required to make the code compile.
2) After compiled, if it works as intended or not.
More information about the Haskell-Cafe