Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between return and returnIO in OO Haskell

Tags:

haskell

returnIO is used in OOHaskell to return the object as a record of methods. But even on use of return instead of returnIO there is no difference in the output or object behaviour. The OOHaskell code is:

{-# LANGUAGE EmptyDataDecls, DeriveDataTypeable, TemplateHaskell #-}
{-# OPTIONS_GHC -fcontext-stack=100 #-}

module Rectangle where

import OOHaskell

$(label "getLength")
$(label "getWidth")
$(label "incr")
$(label "lengthenBy")
$(label "setLength")
$(label "setWidth")
$(label "show'")

rectangle length width self
 = do
     lengthRef <- newIORef length :: IO (IORef Int)
     widthRef <- newIORef width :: IO (IORef Int)
     return $
          getLength      .=. readIORef lengthRef
      .*. getWidth      .=. readIORef widthRef
      .*. setLength      .=. writeIORef lengthRef
      .*. setWidth      .=. writeIORef widthRef
      .*. lengthenBy    .=. (\dl ->
              do
             length <- self # getLength
             (self # setLength) (length + dl))
      .*. incr          .=. (self # lengthenBy) (1)
      .*. show'         .=. printLn ("Length : "<< self # getLength<<" Width : "<< self # getWidth)
      .*. emptyRecord

Can anyone please explain why is this happening? What makes the object behave in same way in both cases?

like image 838
Rog Matthews Avatar asked Dec 16 '25 15:12

Rog Matthews


1 Answers

returnIO is return, but specialised to IO types.

That is:

return   :: Monad m => a ->  m a
returnIO ::            a -> IO a

See lines 75-76 of OOHaskell.hs:

returnIO :: a -> IO a
returnIO = return
like image 191
dave4420 Avatar answered Dec 19 '25 06:12

dave4420



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!