35
votes

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
You'll get pretty much the same warning in OCaml, BTW. You can disable it in F# with the --no-warn flag (though I'm not sure what the code number for that particular warning is). - Chris Conway

1 Answers

63
votes

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 _ = ()