HSpec没什么不可以编译的

问Zamarreño

我正在学习Haskell,并且已经编写了以下函数:

safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

我现在正在尝试使用HSpec对其进行测试:

import Test.Hspec

main :: IO ()
main = hspec spec

spec :: Spec
spec =

  describe "safeHead" $
    it "should return Nothing for empty list" $
      safeHead [] `shouldBe` Nothing

但这无法编译:

Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Base’
      instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
      instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
      ...plus 31 others
    In the second argument of ‘($)’, namely
      ‘safeHead [] `shouldBe` Nothing’
    In the second argument of ‘($)’, namely
      ‘it "should return Nothing for empty list"
       $ safeHead [] `shouldBe` Nothing’
    In the expression:
      describe "safeHead"
      $ it "should return Nothing for empty list"
        $ safeHead [] `shouldBe` Nothing

我也尝试过这个:

safeHead :: (Eq a) => [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:xs) = Just x

但这仍然失败:

Error:(14, 19) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance (Eq a, Eq b) => Eq (Either a b)
        -- Defined in ‘Data.Either’
      instance Eq Data.Monoid.All -- Defined in ‘Data.Monoid’
      instance forall (k :: BOX) (f :: k -> *) (a :: k).
               Eq (f a) =>
               Eq (Data.Monoid.Alt f a)
        -- Defined in ‘Data.Monoid’
      ...plus 43 others
    In the second argument of ‘($)’, namely
      ‘safeHead [] `shouldBe` Nothing’
    In the second argument of ‘($)’, namely
      ‘it "should return Nothing for empty list"
       $ safeHead [] `shouldBe` Nothing’
    In the expression:
      describe "safeHead"
      $ it "should return Nothing for empty list"
        $ safeHead [] `shouldBe` Nothing

我不知道这是什么问题。如果我尝试其他类似的测试,则可以正常编译:

    it "should return the head" $ do
      safeHead [1] `shouldBe` Just 1
      safeHead [2,3,4,5,6,1] `shouldBe` Just 2

那么,这是关于Nothing自身的事情,它不能被平等地比较吗?您如何断言那会返回Nothing什么?还是我的功能太通用?

旁注:我已经看到与此功能类似的错误:

palindrome :: (Eq a) => [a] -> [a]
palindrome xs = xs ++ reverse xs

尝试测试空列表时:

palindrome [] `shouldBe` []

失败的原因是:

Error:(26, 21) ghc: No instance for (Eq a0) arising from a use of ‘shouldBe’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance Eq a => Eq (Maybe a) -- Defined in ‘GHC.Base’
      instance Eq a => Eq (GHC.Real.Ratio a) -- Defined in ‘GHC.Real’
      instance Eq Ordering -- Defined in ‘ghc-prim-0.4.0.0:GHC.Classes’
      ...plus 32 others
    In a stmt of a 'do' block: palindrome [] `shouldBe` []
    In the second argument of ‘($)’, namely
      ‘do { palindrome [] `shouldBe` [] }’
    In the second argument of ‘($)’, namely
      ‘it
         "should turn a list into a palindrome, so it reads same both forwards and backwards"
       $ do { palindrome [] `shouldBe` [] }’
泽塔

所以,这与Nothing本身有关,无法通过均等进行比较吗?

什么是NothingNothing :: Maybe a而GHCa在这种情况下并不喜欢:“类型变量'a0'是不明确的”。毕竟,shouldBe采取一切可以与之相比(==)并显示出来的东西。Maybe aEqifa的实例,是的实例EqGHC可能不知道您要使用哪个 a,因此您需要手动指定它:

  describe "safeHead" $
    it "should return Nothing for empty list" $
      safeHead [] `shouldBe` (Nothing :: Maybe Int)

这不是强制性的,您只是在说明要使用的所有可能类型中的哪种其他例子:

  describe "safeHead" $
    it "should return Nothing for empty list" $ do
      safeHead [] `shouldBe` (Nothing :: Maybe Int)
      safeHead [] `shouldBe` (Nothing :: Maybe ())
      safeHead [] `shouldBe` (Nothing :: Maybe Integer)
      safeHead [] `shouldBe` (Nothing :: Maybe Char)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

会话中的OOP对象,什么时候不可以?

来自分类Dev

开关可能掉线(不可以)

来自分类Dev

委托人和Func / Action什么时候不可以互换?

来自分类Dev

为什么不可以在“ for”循环中为多个按钮执行“ .startAnimation”?

来自分类Dev

在Ruby中,为什么变量在代码块中不可以互换?

来自分类Dev

在Designtime 中使用PictureBox 作为Parent,为什么不可以?

来自分类Dev

Python-什么时候可以按名称传递位置参数,什么时候不可以?

来自分类Dev

变异触发器,什么时候可以从触发器表中读取,什么时候不可以?

来自分类Dev

如果还可以,否则不可以

来自分类Dev

Eclipse没什么可以在C项目中建立消息

来自分类Dev

为什么from from from itertools import chain可以,但是不可以使用import itertools.chain as chain呢?

来自分类Dev

django提出的“功能”对象不可下标,但是本地python不可以

来自分类Dev

onHandleIntent有时可以工作,有时不可以

来自分类Dev

Google图表,有时可以正常运行,有时却不可以?

来自分类Dev

整数既可以是非负数也不可以是非正数?

来自分类Dev

CSS中的多色条-在Firefox中可以,但其他不可以

来自分类Dev

在PHP中str_replace可以是ORing还是不可以?

来自分类Dev

既不能从Activity也不可以从BroadcastReceiver停止服务

来自分类Dev

更新已准备就绪,不可以在App Store中的“更新”下显示(6天)

来自分类Dev

匹配两个组,但都不可以为空

来自分类Dev

Laravel没什么可迁移的

来自分类Dev

Passport.js-浏览器的GET请求可以,但是来自JS代码的AJAX请求却不可以?

来自分类Dev

iOS使用核心数据和排序为索引的tableview-索引可以,但数据不可以

来自分类Dev

当列表元素可以或不可以作为列出现时,从具有给定列表的Pandas Dataframe中过滤列

来自分类Dev

我可以和不可以使用哪些DVI端口与其他电缆一起使用

来自分类Dev

一些环境变量可以访问,而其他一些不可以吗?

来自分类Dev

我的班级可以在我的代码开始时调用,但是在我的测试循环时不可以调用

来自分类Dev

仅在PC上可以播放视频,而在平板电脑或手机上不可以播放视频?

来自分类Dev

@ font-face有时有时可以工作,而其他时候不可以?

Related 相关文章

  1. 1

    会话中的OOP对象,什么时候不可以?

  2. 2

    开关可能掉线(不可以)

  3. 3

    委托人和Func / Action什么时候不可以互换?

  4. 4

    为什么不可以在“ for”循环中为多个按钮执行“ .startAnimation”?

  5. 5

    在Ruby中,为什么变量在代码块中不可以互换?

  6. 6

    在Designtime 中使用PictureBox 作为Parent,为什么不可以?

  7. 7

    Python-什么时候可以按名称传递位置参数,什么时候不可以?

  8. 8

    变异触发器,什么时候可以从触发器表中读取,什么时候不可以?

  9. 9

    如果还可以,否则不可以

  10. 10

    Eclipse没什么可以在C项目中建立消息

  11. 11

    为什么from from from itertools import chain可以,但是不可以使用import itertools.chain as chain呢?

  12. 12

    django提出的“功能”对象不可下标,但是本地python不可以

  13. 13

    onHandleIntent有时可以工作,有时不可以

  14. 14

    Google图表,有时可以正常运行,有时却不可以?

  15. 15

    整数既可以是非负数也不可以是非正数?

  16. 16

    CSS中的多色条-在Firefox中可以,但其他不可以

  17. 17

    在PHP中str_replace可以是ORing还是不可以?

  18. 18

    既不能从Activity也不可以从BroadcastReceiver停止服务

  19. 19

    更新已准备就绪,不可以在App Store中的“更新”下显示(6天)

  20. 20

    匹配两个组,但都不可以为空

  21. 21

    Laravel没什么可迁移的

  22. 22

    Passport.js-浏览器的GET请求可以,但是来自JS代码的AJAX请求却不可以?

  23. 23

    iOS使用核心数据和排序为索引的tableview-索引可以,但数据不可以

  24. 24

    当列表元素可以或不可以作为列出现时,从具有给定列表的Pandas Dataframe中过滤列

  25. 25

    我可以和不可以使用哪些DVI端口与其他电缆一起使用

  26. 26

    一些环境变量可以访问,而其他一些不可以吗?

  27. 27

    我的班级可以在我的代码开始时调用,但是在我的测试循环时不可以调用

  28. 28

    仅在PC上可以播放视频,而在平板电脑或手机上不可以播放视频?

  29. 29

    @ font-face有时有时可以工作,而其他时候不可以?

热门标签

归档