on f104f91 help sign in
hakyll/src/Hakyll/Core/CompiledItem.hs
40 lines 1.2 kB history raw
1-- | A module containing a box datatype representing a compiled item. This
2-- item can be of any type, given that a few restrictions hold (e.g. we want
3-- a 'Typeable' instance to perform type-safe casts).
4--
5{-# LANGUAGE ExistentialQuantification #-}
6module Hakyll.Core.CompiledItem
7 ( CompiledItem (..)
8 , compiledItem
9 , unCompiledItem
10 ) where
12import Data.Binary (Binary)
13import Data.Typeable (Typeable, cast)
14import Data.Maybe (fromMaybe)
16import Hakyll.Core.Writable
18-- | Box type for a compiled item
19--
20data CompiledItem = forall a. (Binary a, Typeable a, Writable a)
21 => CompiledItem a
23instance Writable CompiledItem where
24 write p (CompiledItem x) = write p x
26-- | Box a value into a 'CompiledItem'
27--
28compiledItem :: (Binary a, Typeable a, Writable a)
29 => a
30 -> CompiledItem
31compiledItem = CompiledItem
33-- | Unbox a value from a 'CompiledItem'
34--
35unCompiledItem :: (Binary a, Typeable a, Writable a)
36 => CompiledItem
37 -> a
38unCompiledItem (CompiledItem x) = fromMaybe error' $ cast x
39 where
40 error' = error "Hakyll.Core.CompiledItem.unCompiledItem: Unsupported type"