F#中括号的使用 [英] Usage of parenthesis in F#

查看:117
本文介绍了F#中括号的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解F#中括号的用法时遇到问题.为了说明一个简单的示例,以下两个控制台应用程序的行为有很大不同.第一个不等我输入任何内容:

I am having an issue understanding the usage of parenthesis in F#. To illustrate with a simple example, the 2 following console apps behave very differently. The first one doesn't wait for me to type in anything:

open System
let Main =
   Console.WriteLine "Hello"
   Console.ReadLine

第二个是:

open System
let Main =
   Console.WriteLine "Hello"
   Console.ReadLine()

我应该如何理解差异?

推荐答案

如果函数不带参数,则可以指定单位值()作为参数,如下面的代码行所示.

If a function takes no parameters, you specify the unit value () as the argument, as in the following line of code.

initializeApp()

一个函数的名称本身就是一个函数值,因此,如果省略表示单位值的括号,则仅引用该函数,而不调用该函数.

The name of a function by itself is just a function value, so if you omit the parentheses that indicate the unit value, the function is merely referenced, not called.

http://msdn.microsoft.com/en-us/library/dd233229.aspx

这就是为什么您必须执行Console.ReadLine()而不是Console.ReadLine(后者返回一个函数委托)的原因

That is why you have to do Console.ReadLine() rather than Console.ReadLine ( the latter returns a function delegate)

这篇关于F#中括号的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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