【发布时间】:2015-01-05 10:19:30
【问题描述】:
在the chapter 14 of Real World Haskell (Monads)中,注入函数return的类型签名是return :: a -> m a,其中m a是一个类型构造函数,所以在ghci下我可以为return arg1指定一个类型签名,比如:
*Main> return 1 :: Maybe Integer
Just 1
*Main> return "ok" :: Maybe String
Just "ok"
因为Nothing是Maybe a类型的值,Nothing的类型是Maybe Integer或者Maybe String,所以我觉得可以指定类型如下:
*Main> return Nothing :: Maybe String
但我得到了一个错误:
Couldn't match type `Maybe a0' with `[Char]'
Expected type: String
Actual type: Maybe a0
In the first argument of `return', namely `Nothing'
In the expression: return Nothing :: Maybe String
In an equation for `it': it = return Nothing :: Maybe String
我对它的类型签名感到困惑。
【问题讨论】:
-
通常你不需要return,在一个do块的末尾
Nothing就足够了。我的意思是do Nothing是有效的。 -
您可能正在寻找更多类似于
fail "To err is human" :: Maybe t的内容,这将导致Nothing。