[Haskell-cafe] I'm trying to design a GUI library -- a design question

Daniel Trstenjak daniel.trstenjak at gmail.com
Sun Mar 2 21:38:02 UTC 2014


Hi Ömer,

> In a sense I'm like trying to emulate something like closures with
> mutable closed-over data in impure languages.

One way is to keep the specific data completely out of the Widget and
use a closure to hold the data:

    data Widget = Widget
       { handleKey :: Key -> Widget
       , draw      :: Int -> Int -> Int -> Int -> IO ()
       , getFocus  :: Widget
       , loseFocus :: Widget
       }

    someWidget :: SomeWidgetData -> Widget
    someWidget dat = Widget
       { handleKey = \key -> case key of 'A' -> someWidget $ dat { ... } ; ...
       , draw      = \x y w h -> ... 
       , getFocus  = someWidget $ dat { focus = True }
       , loseFocus = someWidget $ dat { focus = False }
       }


Greetings,
Daniel


More information about the Haskell-Cafe mailing list