【问题标题】:What to do about 'discard' in Purescript do-notation?如何处理 Purescript do-notation 中的“丢弃”?
【发布时间】:2017-07-06 06:14:17
【问题描述】:

对通过示例学习 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
    }

给出以下错误:

在模块 Example.Rectangle 中 在 src\Example\Rectangle.purs 第 16 行,第 3 列 - 第 16 行,第 29 列

类型的结果

Context2D

在 do 表示法块中被隐式丢弃。 您可以使用 _

在应用函数时丢弃 类型丢弃 t0 => 绑定 t1 => t1 t0 -> (t0 -> t1 t2) -> t1 t2 参数 (setFillStyle "#0000FF") ctx 同时推断丢弃的类型 ((setFillStyle "#0000FF") ctx) 在价值声明中主要

其中 t0 是未知类型 t2 是未知类型 t1 是未知类型

更多信息请参见https://github.com/purescript/documentation/blob/master/errors/NoInstanceFound.md

建议的错误没有帮助,我无法弄清楚“丢弃”究竟是做什么的。我还注意到一个类似的问题,例如,第 8.17 节中的“模拟”功能。如果我尝试使用“_

(这是使用 PSCi 0.11.5)

【问题讨论】:

  • 请分享您的代码,而不是图书链接。这不起作用。

标签: purescript do-notation


【解决方案1】:

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

您可以: - 明确忽略该值:_

【讨论】:

  • 或者,您可以使用void $ setFilleStyle...,其中void 来自前奏。
  • 谢谢,这两种解决方案都有效,但确实有点冗长
猜你喜欢
  • 2021-08-14
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多