[Haskell-cafe] event handler
corentin.dupont at gmail.com
Thu Jun 14 21:15:21 CEST 2012
I'm trying to make a simple event driven engine. It simply consists of two
- "addEvent", where you pass the event name with a callback,
- "triggerEvent" where you pass the event name with the data.
the data shall be passed to the callback of the corresponding event.
I have trouble making it correctly typed.
Here is my try:
type Player = Int --dummy types for the example
type Rule = Int
data EventEnum = NewPlayer | NewRule deriving Eq
data Data = P Player | R Rule
data Handler = H (Data -> IO ())
addEvent :: EventEnum -> Handler -> [(EventEnum, Handler)] -> [(EventEnum,
addEvent e h es = (e,h):es
triggerEvent :: EventEnum -> Data -> [(EventEnum, Handler)] -> IO ()
triggerEvent e d es = do
let r = lookup e es
case r of
Nothing -> return ()
Just (H h) -> h d*
The trouble is that I want the user to be only able to add an event that is
compatible with its handler:
For example the event NewPlayer should have a handler of type Player -> IO
(). The data passed when triggering this event should be only of type
How can I do that? It sound like dependant typing...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe