以下代码:
我不理解错误:
当
Prelude> :t putStrLn
putStrLn :: String -> IO ()
Prelude> putStrLn "test"
test
it :: ()
Prelude> putStrLn "test" ++ "test"
<interactive>:25:1:
Couldn't match expected type ‘[Char]’ with actual type ‘IO ()’
In the first argument of ‘(++)’, namely ‘putStrLn "test"’
In the expression: putStrLn "test" ++ "test"
Prelude> :t "test"
"test" :: [Char]
Prelude> :t "test" ++ "test"
"test" ++ "test" :: [Char]
Prelude>
我不理解错误:
" Couldn't match expected type ‘[Char]’ with actual type ‘IO ()’
In the first argument of ‘(++)’, namely ‘putStrLn "test"’
In the expression: putStrLn "test" ++ "test""
当
putStrLn
接受[Char]类型作为第一个参数时(我认为它会被隐式转换为String?),但如果将"test" ++ "test"
作为参数传递,则无法进行类型检查,即使"test" ++ "test"
也是[Char]类型?
String
只是[Char]
的类型同义词,也就是完全相同的东西。发生的是多态值(例如数字字面量)的特化,但一旦选择了具体类型,它不会自行更改为其他类型。 - duplode