exhaustiveness check

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}

module Lib (gameLoop) where

import Eve
import System.IO

import Control.Lens
import Control.Monad
import Control.Monad.State
import Control.Monad.Trans
import Data.Default

data KeyPress = KeyPress Char

data GameState = GameState
  { _pos' :: Int
  }

makeLenses ''GameState

instance Default GameState where
  def = GameState 0

pos :: Lens' AppState Int
pos = makeStateLens pos'

keypressProvider :: EventDispatcher -> IO ()
keypressProvider dispatcher = forever $ do
  c <- getChar
  dispatcher $ KeyPress c

handleKeypress :: KeyPress -> App ()
handleKeypress (KeyPress c) = do
  case c of
    'a' -> pos -= 1
    'd' -> pos += 1
    -- why does the compiler not complain about non exhaustiveness if this is missing?
    _   -> pure () 
  position <- use pos
  liftIO $ print position

setup :: App ()
setup = do
  liftIO $ do
    hSetBuffering stdin NoBuffering
    hSetBuffering stdout NoBuffering
    hSetEcho stdin False
  asyncEventProvider keypressProvider
  addListener_ handleKeypress

gameLoop :: IO ()
gameLoop = eve_ setup