简体   繁体   English

Haskell IO:无法将预期类型“IO a0”与实际类型匹配

[英]Haskell IO: Couldn't match expected type `IO a0' with actual type

I am new to Haskell, and I try to understand how to do IO correctly. 我是Haskell的新手,我试着理解如何正确地执行IO。

The following works ok: 以下工作正常:

main = do
  action <- cmdParser
  putStrLn "Username to add to the password manager:"
  username <- getLine
  case action of
    Add -> persist entry
      where
        entry = Entry username "somepassword"

Whereas the following results in compilation error: 以下结果导致编译错误:

main = do
  action <- cmdParser
  case action of
    Add -> persist entry
      where
        entry = Entry promptUsername "somepassword"

promptUsername = do
  putStrLn "Username to add to the password manager:"
  username <- getLine

The error is here: 错误在这里:

Couldn't match expected type `IO b0' with actual type `[Char]'
Expected type: IO b0
  Actual type: String
In the expression: username
[...]

What is going on here? 这里发生了什么? Why the first version works, while the second one does not? 为什么第一个版本有效,而第二个版本没有?

I know that in Stack Overflow there are a few similar questions like this, but none of them seemed to explain this problem to me. 我知道在Stack Overflow中有一些类似的问题,但是他们似乎都没有向我解释这个问题。

username is a String , but promptUsername is an IO String . username是一个String ,但promptUsername是一个IO String You need to do something like: 你需要做一些事情:

username <- promptUsername
let entry = Entry username "somepassword"
persist entry

Here's another variant. 这是另一种变体。

main = do
  action <- cmdParser
  case action of
    Add -> do username <- promptUsername
              let entry = Entry username "somepassword"
              persist entry

promptUsername :: IO String
promptUsername = do
  putStrLn "Username to add to the password manager:"
  getLine

-- fake definitions to make things compile

persist :: Entry ->  IO ()
persist = print

cmdParser :: IO Add
cmdParser = fmap (const Add) getLine

data Add = Add deriving Show
data Entry = Entry String String deriving Show

The problem is just that promptUsername is an action not a String. 问题是promptUsername是一个动作而不是String。 The action 'returns a String', so it has type IO String , but it is itself nothing like a String. 该操作'返回一个String',因此它具有类型IO String ,但它本身就不像String。 Since Entry xy requires a String in the x position, something in the shape of an action could no more fit there than a number or boolean could. 由于Entry xy需要x位置的String ,因此动作形状的某些东西不能比数字或布尔值更合适。 So in defining your complex action, main , you must 'extract' the string that will result from the simpler action promptUsername in any case of execution, and give the String as the first argument the entry. 因此,在定义复杂动作main ,必须“提取”在任何执行情况下由更简单的动作promptUsername产生的String ,并将String作为条目的第一个参数。 Then you do the persist action on the resulting Entry 然后对生成的Entry执行persist操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型[a0]与实际类型IO()匹配 - couldn't match expected type [a0] with actual type IO () Haskell中的软件事务存储器:无法将预期的STM a0类型与实际的IO类型进行匹配() - Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO () Haskell错误:无法将预期类型&#39;ServerPartT IO a0&#39;与实际类型&#39;[Response]&#39;相匹配 - Haskell error: couldn't match expected type ‘ServerPartT IO a0’ with actual type '[Response]' 无法将预期的类型“ IO()”与实际类型“ a0-&gt; m0 a0”匹配 - Couldn't match expected type `IO ()' with actual type `a0 -> m0 a0' 无法将预期的类型“ IO a0”与实际类型“ a1-&gt; IO()”匹配 - Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO ()’ Haskell“无法将预期类型“ a”与实际类型“ [a0]”匹配” - Haskell “Couldn't match expected type ‘a’ with actual type ‘[a0]’” 有人可以解释此类型错误“无法将期望的类型&#39;IO()&#39;与实际类型&#39;a0-&gt; c0&#39;匹配” - could someone explain this type error “Couldn't match expected type `IO ()' with actual type `a0 -> c0' ” Haskell无法将预期类型[char]与实际类型IO匹配 - Haskell couldn't match expected type [char] with actual type IO 无法将预期的类型“ IO b0”与实际类型“ [a0]”匹配 - Couldn't match expected type `IO b0' with actual type `[a0]' 无法将类型“[]”与“IO”匹配 预期:IO (IO ()) 实际:[IO ()] - Couldn't match type `[]' with `IO' Expected: IO (IO ()) Actual: [IO ()]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM