
the tricky part then is to add support for other types.
another approach to existentially package type classes with the data type!
eg
data HasEq = forall a . HasEq ( Eq a => a)
or its siblinng
data HasEq a = Haseq (Eq a => a )
note this requires more planning in how you structure your program, but is
a much more pleasant approach than using dynamic when you can get it to
suite your application needs.
note its also late, so I've not type checked these examples ;)
-Carter
On Fri, Jul 19, 2013 at 12:54 PM, adam vogt
On Fri, Jul 19, 2013 at 5:19 AM, Jose A. Lopes
wrote: Hello,
How to define equality for Data.Dynamic ?
Hi Jose,
You could try casting the values to different types that do have an (==). You can treat the case where you have the types matching, but didn't list that type beforehand differently.
eqTys a b | Just a' <- fromDynamic a, Just b' <- fromDynamic b = a' == (b' :: Int) | Just a' <- fromDynamic a, Just b' <- fromDynamic b = a' == (b' :: Integer) | show a == show b = error "equal types, but don't know if there's an (==)!" | otherwise = False
{-
eqTys (toDyn 4) (toDyn 5) False
eqTys (toDyn 4) (toDyn 4) True
eqTys (toDyn 4) (toDyn 4.5) False
eqTys (toDyn 4.5) (toDyn 4.5) *** Exception: equal types, but don't know if there's an (==)!
-}
-- Adam
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe