此表达式的类型应为“unit",但类型为“ConsoleKeyInfo" [英] This expression should have type 'unit', but has type 'ConsoleKeyInfo'

查看:18
本文介绍了此表达式的类型应为“unit",但类型为“ConsoleKeyInfo"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在 F# 控制台应用程序中暂停,所以我写道:

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

Console.ReadKey()

Console.ReadKey()

但这给出了警告:此表达式的类型应该为unit",但类型为ConsoleKeyInfo".

我该怎么做才能解决这个问题?

What can I do to fix this?

推荐答案

解决方案:

Console.ReadKey() |> ignore

说明:Console.ReadKey() 返回类型为ConsoleKeyInfo"的对象,但您将其用作语句而不将返回值分配给任何内容.因此,F# 会警告您忽略了一个值.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 _ = ()

这篇关于此表达式的类型应为“unit",但类型为“ConsoleKeyInfo"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆