Search code examples
haskellncursestype-signature

Why is the type signature of getEvent what it is?


The type signature for getEvent from the UI.NCurses library has the type signature

Window -> Maybe Integer -> Curses (Maybe Event)

However having used this function a bunch I am still not quite sure what the Window is used for. The documentation says that it

Get[s] the next Event from a given window.

But that doesn't really enlighten me very much (and reading the source code is similarly unenlightening for me). It would seem to me that if an event like a keypress happens it doesn't happen within a window. And experimentation supports this hypothesis, I seem to pick up the same events regardless of what window I pass. If I have several windows open what difference does it make if I pass one or the other?

And if it does in fact use the window why is the type signature not the more natural

Maybe Integer -> Update (Maybe Event)

Solution

  • The getEvent function needs a window

    The Haskell NCurses library is written on top of the GNU ncurses library, a C library. Since the GNU ncurses library has separate input queues for each window, the getEvent function will need to know which window to get the input from when it makes calls to the appropriate GNU ncurses routines. The need for windows to have separate input queues is probably more apparent when input is not processed as soon as it is received.

    Update monad vs. Window argument? Arbitrary

    The UI.NCurses package contains an unexported function

    withWindow :: (Window -> IO a) -> Update a
    

    that, as its type suggests, can be used to easily convert a function that takes a Window as input to one that returns its result wrapped in the Update monad. It seems like the people who developed the Haskell NCurses library just thought that getEvent would work better in most cases taking a Window as an argument instead of using the Update monad.