简体   繁体   English

使用逗号的F#let语句的语义

[英]Semantics of F# let statement with comma

I'm learning F#. 我正在学习F#。 I started by looking over the F# samples from Microsoft. 我首先查看了Microsoft的F#示例

I ran across this statement: 我跑过这个声明:

let line1,line2 = 
    use sr = System.IO.File.OpenText @"test.txt"
    let line1 = sr.ReadLine() 
    let line2 = sr.ReadLine() 
    (line1,line2)

Can anyone explain this statement to me? 谁能向我解释这个陈述?

What type is being defined here? 这里定义的是什么类型? A function? 一个功能? A tuple? 一个元组?

Why do line1 and line2 have to be redefined within the definition of line1,line2 ( let line1 = ... let line2 = )? 为什么line1line2必须在line1,line2的定义中重新定义( let line1 = ... let line2 = )?

What's with the final line, (line1, line2) and how does this tie into the type of the original definition? 什么是最后一行, (line1, line2)第1行,第2行(line1, line2) ,这与原始定义的类型有什么关系? Is this the function return? 这是函数返回吗?

Is "statement" even the right word to use for a definition in F#? “声明”甚至是用于F#中定义的正确单词吗?

Thanks. 谢谢。

The general form for binding identifier values in F# is F#中绑定标识符值的一般形式是

let pattern = expression

In this case, the pattern is "line1, line2", which is a tuple pattern, it will expect to bind to a 2-tuple of values and assign the names "line1" and "line2" to those two values. 在这种情况下, 模式是“line1,line2”,它是一个元组模式,它将期望绑定到2元组的值,并将名称“line1”和“line2”分配给这两个值。

The expression is the next 4 lines. 表达式是接下来的4行。 Inside that expression there are local variables. 在该表达式中有局部变量。 They happen to also be named "line1" and "line2", but they could easily have been renamed "x" and "y" or whatever - the scope of those identifiers is local to this indented expression. 它们碰巧也被命名为“line1”和“line2”,但它们很容易被重命名为“x”和“y”或其他 - 这些标识符的范围是这个缩进表达式的本地。 (The fact that the same names are used as the names in the outer scope has no effect as far as the compiler is concerned.) (就编译器而言,相同名称用作外部作用域中的名称这一事实无效。)

The final line if the expression is the 'return value' of the expression. 如果表达式是表达式的“返回值”,则为最后一行。 In this case it returns the 2-tuple of values "line1" and "line2" (or "x" and "y" if you rename them for clarity of exposition). 在这种情况下,它返回值为“line1”和“line2”的2元组(或“x”和“y”,如果为了清晰的说明而重命名它们)。 Incidentally, since these two values each have type "string", the type of the return expression is "string*string", which is a 2-tuple where each value is a string. 顺便说一下,由于这两个值都有类型“string”,因此返回表达式的类型是“string * string”,它是一个2元组,其中每个值都是一个字符串。 This means the original "line1" and "line2" names on the first line will each be inferred to have type "string". 这意味着第一行上的原始“line1”和“line2”名称将被推断为具有类型“string”。

F# is functional, and so in a sense "everything is an expression" and "there are no statements" (only sequences of expressions that are sequentially evaluated), but it is ok IMO to (ab)use the term "statement" to describe the inner "let" lines, unless you're trying to be very precise. F#是功能性的,因此从某种意义上说“一切都是表达”和“没有语句”(只有顺序评估的表达式序列),但IMO(ab)使用术语“语句”来描述是可以的内在的“让”线,除非你想要非常精确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM