packages feed

TCache-0.9.0.1: demos/DynamicSample.hs

{-# OPTIONS -XTypeSynonymInstances #-}
-- XTypeSynonymInstances added only to permit IResource instances for Strings
module Main where
import Data.TCache
import Data.TCache.FilePersistence
import Data.Typeable

{------------- tests---------
example of IDynamic usage.

-}

--very simple data:
--two objects with two different datatypes: Int and String

instance Indexable Int where     
   key x=  show x

  
  
 
instance Indexable String where
   key x=  take 2 x
 



main= do
  putStrLn "see the code to know the meaning of he results"

  -- NOTE registerType no longer needed
  
  
  let x= 1:: Int

  -- now *Resources primitives suppont different datatypes
  -- without the need  of Data.Dynamic
  withResources  [] $ const  [x]
  withResources  [] $ const  ["hola"]  --resources creation 
  
  syncCache                           
  
  res <- getResource  x        
  print res 
  
  res <- getResource  "ho"                   
  print res   

  -- to use heterogeneous data in the same transaction,
  -- use DBRef's:
  s <- atomically $ do
        let refInt    = getDBRef $ keyResource x    :: DBRef Int
            refString = getDBRef $ keyResource "ho" :: DBRef String
        i <- readDBRef refInt
        writeDBRef refString $ "hola, the retrieved value of x is " ++ show i
        s <- readDBRef refString
        return s

  print s

  -- however, retrieval of data with the incorrect type will generate an exception:

  syncCache