I have tried to implement the rectangle problem using OOHaskell.
{-# 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")
rectangle length width self
= do
lengthRef <- newIORef value
widthRef <- newIORef width
return $
getLength .=. readIORef lengthRef
.*. getWidth .=. readIORef widthRef
.*. setLength .=. writeIORef lengthRef
.*. setWidth .=. writeIORef widthRef
.*. lengthenBy .=. (\dv ->
do
value <- self # getValue
(self # setValue) (value + dv))
.*. incr .=. (self # (lengthenBy 1))
.*. emptyRecord
But i am getting the scope error. The error meassage is
Rectangle.hs:21:38: Not in scope: `widthRef'
Rectangle.hs:22:39: Not in scope: `lengthRef'
Rectangle.hs:23:39: Not in scope: `widthRef'
How can i resolve the error?
Thanks Daniel done that. But now the error i am getting is:
The function `lengthenBy' is applied to one argument,
but its type `Proxy LengthenBy' has none
In the second argument of `(#)', namely `(lengthenBy 1)'
In the second argument of `(.=.)', namely `(self # (lengthenBy 1))'
In the first argument of `(.*.)', namely
`incr .=. (self # (lengthenBy 1))'
Fix your indentation:
the ‘l’ of
lengthRefand the ‘w’ ofwidthRefmust be in the same column.As it stands, it is parsed as
However, I think that should produce a parse error and not reach the ‘not in scope’ phase. So I guess that’s not in your actual code but a paste-glitch here.
Then:
breaks it, the
.*.is indented less than the do-block indentation, so it’s parsed asIndent the stuff you want to go inside the
returnfarther than the enclosing do-block.