|
From: Andre P. <oz...@al...> - 2004-05-14 16:05:28
|
(Hooray, first post to hoc-devel! :)
One idea which I think would be really nice: use implicit parameters to
pass instance variables to methods. So, normally you'd write something
like this (taken from the ExpressionParser sample I just checked into
CVS):
obj #. var = obj # getIVar var
-- ep_evaluate :: NSButton a
-- -> EPController a
-- -> IO ()
ep_evaluate sender self = do
expressionTextField <- self #. _expressionTextField
expression <- expressionTextField # stringValue >>= haskellString
...
This is quite cumbersome, because you need to write extra statements
just to get the instance variable from the object. Using implicit
variables, you could instead write:
-- ep_evaluate :: ( ?expressionTextField :: NSTextField a )
-- => NSButton a
-- -> EPController a
-- -> IO ()
ep_evaluate sender self = do
expression <- expressionTextField # stringValue >>= haskellString
This saves writing the #. helper function, and also saves having to
write a statement that uses #. for each instance variable you want to
pull out in every method. The only problem I can see with this is that
the Template Haskell syntax has to be extended to include implicit
variables -- do you know how to do this? If not, I'll have to do some
hacking myself or ask Ian Lynagh about it.
Note that if you don't _use_ the implicit variables in the function,
that's perfectly OK too, which is the behaviour we'd want.
{-# OPTIONS -fglasgow-exts #-}
module ImplicitParameterTest where
aMethod :: ( ?instanceVariable1 :: String
, ?instanceVariable2 :: Int )
=> IO ()
aMethod = do
putStrLn "hello!"
^ The above compiles perfectly.
(In Mocha, I was using the "unsafePerformIO $ newIORef ..." hack to do
this because it's just so much more convenient than an explicit,
monadic getIVar function, but implicit variables achieves the same goal
and would be safer.)
--
% Andre Pang : trust.in.love.to.save
|