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

前端 未结 1 399
既然无缘
既然无缘 2020-12-09 07:32

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\

相关标签:
1条回答
  • 2020-12-09 08:23

    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 _ = ()
    
    0 讨论(0)
提交回复
热议问题