The goal is to be able to expose Haskell code as COM component functionality, so how should we represent such an object in Haskell? We've more or less given the solution away for the kind of representation we prefer in Section Implementing COM components in Haskell, but there's a couple of possible choices:
module IntRef where
type State = IORef Int
get :: State -> IO Int
set :: State -> Int -> IO ()
new :: IO State
which they then are responsible for explicitly updating when
invoked. A variation on this is to instead return the updated state:
module IntRef where
type State = Int
get :: State -> IO (Int, State)
set :: State -> Int -> IO State
new :: IO State
but the advantages of this scheme over the first one are few (if any).
module IntRef where
type IntRefMethods =
( Interface IIntRef -> Pointer Int -> IO HRESULT -- getter
, Int -> Interface IIntRef -> IO HRESULT -- setter
)
new :: IO IntRefMethods
new = do
var <- newIORef 0
let
set' v _ = do
writeIORef var v
return s_OK
get' _ ptr = do
v <- readIORef var
if ptr == nullAddr
then return e_POINTER
else do
writeIntOffAddr ptr v
return s_OK
return (set',get')
Discussion:
IntRef.HRESULTs.)The following sections assume that the explicit state representation is used.