我正在关注如何在purescript中绘制到html画布的this walkthrough。不幸的是,我遇到了使代码生效的问题。我按照教程创建了以下代码,但它没有编译:
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
我尝试通过执行以下操作来解决问题:
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
我该怎样处理&#34;什么都不运行&#34;案件?为了使它匹配需要返回Eff值的类型签名,是否有一种简单的方法将计算包装在Eff monad中?
答案 0 :(得分:2)
pure
是您正在寻找的功能 - 我们不再拥有return
,因为当Applicative
是Monad
的超类时它是多余的。