[Haskell-cafe] Is it usual to read a Maybe (IORef a) ?

minh thu noteed at gmail.com
Wed Sep 3 06:09:38 EDT 2008


I'd like to write a data structure to be used inside the IO monad.
The structure has some handles of type Maybe (IORef a),
i.e. IORef are pointers and the Maybe is like null pointers.

So I came up with the following functions :

readHandle :: Maybe (IORef a) -> IO (Maybe a)
readField :: (a -> b) -> Maybe (IORef a) -> IO (Maybe b)

readHandle Nothing  = do
  return Nothing
readHandle (Just r) = do
  v <- readIORef r
  return $ Just v

readField f h = do
  m <- readHandle h
  return $ fmap f m

Is it something usual ?
Are there any related functions in the standard libraries ?


More information about the Haskell-Cafe mailing list