1
votes

I am following this walkthrough on how to draw to an html canvas in purescript. Unfortunately I ran into a problem getting the code to work. I created the following code by following the tutorial but it doesn't compile:

main = do
  -- Canvas has type Maybe CanvasElement
  canvas <- getCanvasElementById "canvas"

  -- Type mismatch: CanvasElement expected, (Maybe CanvasElement) provided
  context <- getContext2D canvas

  runGraphics context $ do
    setFillStyle "#00FFFF"
    rect { x: 0, y: 0, w: 400, h: 600 }
    fill

I tried to fix the problem by doing the following:

run Nothing = do return Unit -- Doesn't compile
run (Just canvas) = do
  g2d <- GraphicsBuilder.getContext2D canvas
  runGraphics g2d $ do
          setFillStyle "#00FFFF"
          rect { x: 0.0, y: 0.0, w: 400.0, h: 600.0 }
          fill

main = do
  mcanvas <- GraphicsBuilder.getCanvasElementById "canvas"
  run mcanvas

How should I handle the "run Nothing" case? In order for it to match the type signature it needs to return an Eff value, is there an easy way to wrap the computation in an Eff monad?

1

1 Answers

2
votes

pure is the function you're looking for - we no longer have return since it is redundant when Applicative is a superclass of Monad.