记录的 PureScript "Could not match type" 错误
PureScript "Could not match type" error for a Record
我正在遵循 PureScript 书中的 exercises 之一,我必须在其中定义一个函数 renderPath
,该函数采用 Point
s(x 和 y)的数组,并且使用它们绘制路径(使用 purescript-canvas
),其中 Point
定义为
type Point = { x :: Number, y :: Number }
我决定将该功能分为两个步骤。第一步是 moveTo
数组中的第一个点,然后对其余点使用 lineTo
。
这是我到目前为止编写的函数,它只有第一步。但是我遇到了一个我无法弄清楚的错误。
这是函数
import Prelude
import Control.Monad.Eff (Eff)
import Data.Array.Partial (head, tail)
import Data.Maybe (Maybe(..))
import Graphics.Canvas (CANVAS, Context2D, arc, closePath, fillPath, getCanvasElementById, getContext2D, lineTo, moveTo, rect, setStrokeStyle, strokePath)
import Partial.Unsafe (unsafePartial)
renderPath :: forall eff
. Context2D
-> Array Point
-> Eff (canvas :: CANVAS | eff) Unit
renderPath ctx points = fillPath ctx $ do
p0 <- unsafePartial head points
moveTo ctx p0.x p0.y
我在函数的最后一行 p0
处遇到了这个错误 (moveTo ctx p0.x p0.y
)
错误是:
Could not match type
{ x :: Number
| t0
}
with type
( x :: Number
, y :: Number
)
while checking that type ( x :: Number
, y :: Number
)
is at least as general as type { x :: Number
| t0
}
while checking that expression p0
has type { x :: Number
| t0
}
while checking type of property accessor p0.x
in value declaration renderPath
where t0 is an unknown type
我假设 p0
应该是 { x :: Number, y :: Number }
类型
此错误显示一个为 { x :: Number | t0 }
,另一个为 ( x :: Number, y :: Number)
。我完全不明白这一点。为什么一个使用 {}
而另一个使用 ()
。它们之间有什么区别?我哪里错了?
由于我是 PureScript 和整个函数式编程的新手,因此我在尝试理解很多概念时遇到了麻烦。
我不完全确定你为什么会收到那条特定的错误消息,但你的代码有一个问题是这一行:
p0 <- unsafePartial head points
这应该是
let p0 = unsafePartial (head points)
<-
在 do
块中的意思类似于 "get the value out of the context of Eff
"。但是由于 unsafePartial (head points)
不是 Eff
,类型不匹配,您会收到一条错误消息。
此代码编译:
http://try.purescript.org/?backend=flare&gist=6baa84215d33bf6f11c808e24b95c72f
我正在遵循 PureScript 书中的 exercises 之一,我必须在其中定义一个函数 renderPath
,该函数采用 Point
s(x 和 y)的数组,并且使用它们绘制路径(使用 purescript-canvas
),其中 Point
定义为
type Point = { x :: Number, y :: Number }
我决定将该功能分为两个步骤。第一步是 moveTo
数组中的第一个点,然后对其余点使用 lineTo
。
这是我到目前为止编写的函数,它只有第一步。但是我遇到了一个我无法弄清楚的错误。
这是函数
import Prelude
import Control.Monad.Eff (Eff)
import Data.Array.Partial (head, tail)
import Data.Maybe (Maybe(..))
import Graphics.Canvas (CANVAS, Context2D, arc, closePath, fillPath, getCanvasElementById, getContext2D, lineTo, moveTo, rect, setStrokeStyle, strokePath)
import Partial.Unsafe (unsafePartial)
renderPath :: forall eff
. Context2D
-> Array Point
-> Eff (canvas :: CANVAS | eff) Unit
renderPath ctx points = fillPath ctx $ do
p0 <- unsafePartial head points
moveTo ctx p0.x p0.y
我在函数的最后一行 p0
处遇到了这个错误 (moveTo ctx p0.x p0.y
)
错误是:
Could not match type
{ x :: Number
| t0
}
with type
( x :: Number
, y :: Number
)
while checking that type ( x :: Number
, y :: Number
)
is at least as general as type { x :: Number
| t0
}
while checking that expression p0
has type { x :: Number
| t0
}
while checking type of property accessor p0.x
in value declaration renderPath
where t0 is an unknown type
我假设 p0
应该是 { x :: Number, y :: Number }
此错误显示一个为 { x :: Number | t0 }
,另一个为 ( x :: Number, y :: Number)
。我完全不明白这一点。为什么一个使用 {}
而另一个使用 ()
。它们之间有什么区别?我哪里错了?
由于我是 PureScript 和整个函数式编程的新手,因此我在尝试理解很多概念时遇到了麻烦。
我不完全确定你为什么会收到那条特定的错误消息,但你的代码有一个问题是这一行:
p0 <- unsafePartial head points
这应该是
let p0 = unsafePartial (head points)
<-
在 do
块中的意思类似于 "get the value out of the context of Eff
"。但是由于 unsafePartial (head points)
不是 Eff
,类型不匹配,您会收到一条错误消息。
此代码编译:
http://try.purescript.org/?backend=flare&gist=6baa84215d33bf6f11c808e24b95c72f