如何处理 Purescript do-notation 中的 'discard'?

What to do about 'discard' in Purescript do-notation?

通过示例学习 Purescript 书中的一些示例有问题。特别是第 9.3 节中的这段代码:

main :: Eff (canvas :: CANVAS) Unit
main = void $ unsafePartial do
  Just canvas <- getCanvasElementById "canvas"
  ctx <- getContext2D canvas

  setFillStyle "#0000FF" ctx -- this's line 16 referred to in the error message

  fillPath ctx $ rect ctx
    { x: 250.0
    , y: 250.0
    , w: 100.0
    , h: 100.0
    }

出现以下错误:

in module Example.Rectangle at src\Example\Rectangle.purs line 16, column 3 - line 16, column 29

A result of type

Context2D

was implicitly discarded in a do notation block. You can use _ <- ... to explicitly discard the result.

while applying a function discard of type Discard t0 => Bind t1 => t1 t0 -> (t0 -> t1 t2) -> t1 t2 to argument (setFillStyle "#0000FF") ctx while inferring the type of discard ((setFillStyle "#0000FF") ctx) in value declaration main

where t0 is an unknown type t2 is an unknown type t1 is an unknown type

See https://github.com/purescript/documentation/blob/master/errors/NoInstanceFound.md for more information,

建议的错误没有帮助,我无法弄清楚 "discard" 究竟做了什么。我还注意到一个类似的问题,例如,第 8.17 节中的 "simulate" 函数。如果我尝试使用“_ <-”进行分配的建议,则会出现更多看似随机的错误。

(这是使用 PSCi 0.11.5)

不再允许隐式丢弃 do 块中的值。

您可以: - 明确忽略该值:_ <- setFillStyle.... - 或者如果 return 值为单位(例如 Eff fx 单位),您可以简单地从 "Prelude"

导入 "discard"