Hi everyone,
I believe I have come to the conclusion that what I would like to do is impossible, but I would like to have that confirmed. I would basically like to be able to have a heterogeneous list without boxing everything in a data type. Below is the sample code, with the code I would like to use commented out. I'm I missing something, or does Haskell simply not support what I am trying for?
{-# LANGUAGE ExistentialQuantification #-}
data Foo = Foo String
class IFoo a where
toFoo :: a -> Foo
instance IFoo Foo where
toFoo = id
data A = A String
instance IFoo A where
toFoo (A a) = Foo a
data B = B Int
instance IFoo B where
toFoo (B b) = Foo $ show b
data FooBox = forall t. IFoo t => FooBox t
instance IFoo FooBox where
toFoo (FooBox f) = toFoo f
myPrint :: IFoo t => [(String, t)] -> IO ()
myPrint = mapM_ myPrint'
myPrint' :: IFoo t => (String, t) -> IO ()
myPrint' (key, value) =
let (Foo foo) = toFoo value
in putStrLn $ key ++ ": " ++ foo
{- What I'd like to do:
main = myPrint
[ ("one", Foo "1")
, ("two", A "2")
]
-}
main = myPrint
[ ("one", FooBox $ Foo "1")
, ("two", FooBox $ A "2")
]
----
Thanks