This expression should have type 'unit', but has type 'ConsoleKeyInfo'

蓝咒 提交于 2019-12-28 12:57:41

问题


I just wanted to pause in an F# console application, so I wrote:

Console.ReadKey()

But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'.

What can I do to fix this?


回答1:


Solution:

Console.ReadKey() |> ignore

Explanation: Console.ReadKey() returns an object of type 'ConsoleKeyInfo' but you're using it as a statement without assigning the return value to anything. So F# warns you that you're ignoring a value. ignore takes any type and returns nothing. It could be defined like this:

let ignore _ = ()


来源:https://stackoverflow.com/questions/324947/this-expression-should-have-type-unit-but-has-type-consolekeyinfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!