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

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Sep 3 07:58:45 EDT 2008


Hello minh,

Wednesday, September 3, 2008, 2:09:38 PM, you wrote:

> 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.

i've not used this but overall it seems like a correct way to emulate
NULL. the whole question is that you probably still think C if you
need NULL pointers at all :)

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

> Are there any related functions in the standard libraries ?

readHandle = maybe (return Nothing) (fmap Just . readIORef)

or you can add your own primitive:

liftNULL op = maybe (return Nothing) (fmap Just . op)

readHandle = liftNULL readIORef



-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list