F#:这个表达式应该有'unit'类型,但是类型'ConsoleKeyInfo'

编程入门 行业动态 更新时间:2024-10-24 02:00:25
本文介绍了F#:这个表达式应该有'unit'类型,但是类型'ConsoleKeyInfo'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个简单的问题,但由于F#非常新,我无法追踪答案。我只是想在一个F#控制台应用程序中暂停,所以我写道:

Console.ReadKey()

但是这给出了警告:这个表达式应该有'unit'类型,但是类型是'ConsoleKeyInfo'。任何帮助将不胜感激。

谢谢, - Lee

解决方案解决方案:

Console.ReadKey()|>忽略

解释: Console.ReadKey()返回类型为'ConsoleKeyInfo'的对象,但是您将其用作语句而不将返回值分配给任何内容。所以F#警告你,你忽略了一个值。 忽略取任何类型并且不返回任何内容。它可以这样定义: $ p $ let ignore _ =()

This is a simple question, but I'm having trouble tracking down an answer since F# is so new. 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'. Any help would be greatly appreciated.

Thanks, - Lee

解决方案

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

更多推荐

F#:这个表达式应该有'unit'类型,但是类型'ConsoleKeyInfo'

本文发布于:2023-11-17 03:13:17,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   表达式   unit   ConsoleKeyInfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!