{-# OPTIONS -XTypeSynonymInstances #-}
module Main where
import Data.IResource
import Data.IDynamic
import Data.Typeable
instance IResource Int where
keyResource x= "I"
serialize = show
deserialize = read
defPath _= "saved/"
instance IResource String where
keyResource x= take 5 x
serialize = show
deserialize = read
defPath _= "saved/"
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"] --typical Dynamic usage
let assoc= zip (map keyResource list) list
print $ lookup (keyResource (5 ::Int)) assoc -- lookup a IDynamic object from a container and show it
mapM writeResource list
mds ← readResource $ IDynamic "hello" -- save both objects
case mds of
Nothing → error "must have been Just!" --why?
Just ds → do
putStrLn $ serialize ds -- serialize the dynamic object and print it
let str= fromIDyn ds :: String -- casting
putStrLn str -- print the string read
let y= fromIDyn ds :: Int -- casting error
print y