嵌套的 maybe 和 nullable

Nested maybe and nullable

在理清以下 maybe 和 nullable 值时遇到问题:

container <-
  unsafePartial
  (fromJust <<< toMaybe
  <$> DOM.querySelector (DOM.QuerySelector "body")
  (DOM.htmlDocumentToParentNode document))

这给了我以下错误:

Could not match type

Maybe

with type

Nullable

while trying to match type Maybe Element
with type Nullable t1
while checking that expression (querySelector (QuerySelector "body"))
(htmlDocumentToParentNode document)
has type t0 (Nullable t1)
in value declaration main

where t0 is an unknown type
t1 is an unknown type

我已经尝试过了,但我无法通过这里使用的各种类型找到我的方法 (代码原文来自here

我建议稍微分解一下。

开始于

do body <- DOM.querySelector (DOM.QuerySelector "body")
             (DOM.htmlDocumentToParentNode document))
   ?whatNext

这里,?whatNext是打字孔。编译器会告诉你洞的推断类型,这应该可以帮助你找出用什么来替换它。

另请注意,您可以通过将 unsafePartial 直接应用于 fromJust 来简化一些事情,以提供类型为 Maybe a -> a.

的函数