Data.IDynamic is a indexable and serializable version Data.Dynamic .
IDinamic provices methods for safe casting, serializaton, deserialization,
registration output and input
the data definition of IDymanic is as such:
data IDynamic= forall a. (Typeable a, IResource a) => IDynamic a deriving Typeable
The registration trough registerType is necessary before deserialization:
registerType :: IO Type
example:
module Main where
import Data.IResource
import Data.IDynamic
import Data.Typeable
instance IResource Int where
keyResource x= "I"
serialize = show
deserialize = read
defPath _= "data/ "
instance IResource String where
keyResource x= take 5 x
serialize = show
deserialize = read
defPath _= "data/"
main= do
putStrLn "see the code to know the meaning of he results"
registerType :: IO Int -- register both datatypes (Int, and String)
registerType :: IO String
let x= 1 :: Int
let list= [IDynamic x, IDynamic "hello, how are you"]
let assoc= zip (map keyResource list) list
print $ lookup (keyResource (5 ::Int)) assoc
mapM writeResource list
mds <- readResource $ IDynamic "hello"
case mds of
Nothing -> error "must have been Just!"
Just ds -> do
putStrLn $ serialize ds
let str= fromIDyn ds :: String
putStrLn str
let y= fromIDyn ds :: Int -- casting error
print y |