f# 中的缩进错误?
考虑以下代码:
let fn () =
let b =
8. // any expression
-b
let fn2 () =
let b =
8. // any expression
- b
“fn”可以编译,而“fn2”则不能(注意“b”前面的空格)。错误信息是:
此“let”之后的块未完成。期待一个表情。
这是为什么?
Consider the following code:
let fn () =
let b =
8. // any expression
-b
let fn2 () =
let b =
8. // any expression
- b
"fn" compiles whereas "fn2" does not (notice the space in front of "b"). The error message is:
Block following this 'let' is unfinished. Expect an expression.
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
F# 允许各种类型的“缩进”,允许您使用较小的缩进,但仍保留在同一表达式内。
其中一项合法的“取消”是针对运营商的。您可以编写
or
or Even
,并且中缀运算符在后续行中继续相同的表达式。规则是允许您“取消缩进”“中缀运算符的长度加一个空格”,其目的是允许您对齐正在使用的值。常见情况就像数字表,例如
允许 42 以下的行以两个空格的较小缩进开始,以便下一个数字在前一个数字的下方对齐。
所以无论如何,这条规则在这里生效,如果没有空格,“二进制减号”优先于“一元减号”,然后中缀取消缩进规则生效。
F# allows for various kinds of "undentation", where you are allowed to use a lesser indent, but still stay within the same expression.
One such legal "undent" is for operators. You can write
or
or even
and the infix operators continue the same expression on a subsequent line. The rule is that you are allowed to "undent" the "length of the infix operator plus one space", the intent being that this allows you to align the values you're using. A common case is like a table of numbers, like
where the line below 42 is allowed to start at a two-space lesser indent so that the next number is aligned under the prior one.
So anyway, that rule is kicking in here, and without a space, the 'binary minus' takes precedence over the 'unary minus' and then the infix undent rule kicks in.
fn2 被解析为:
let
块未完成,它需要一个返回值,例如。我建议您在二元运算符周围使用空格,并且在一元运算符之后不使用空格。
同样,
x - 2
(减法)和x -2
(使用参数 -2 调用函数 x)之间也存在差异。fn2 is parsed as:
The
let
block is unfinished, it needs a return value, eg.I suggest you to use spaces around binary operators, and no spaces after unary operators.
In the same way, there's a difference between
x - 2
(substraction) andx -2
(call function x with argument -2).这是你对8的缩进。
当我输入此代码时:
它可以正确编译。发生的情况是,在第一个示例中, fn2 相当于:
并且编译器需要其他东西来完成 let 块(我总是将 let 块读为“let foo = bar in expr”)。所以你错过了 expr 部分。在 fn 中,您将得到“let b = 8. in -b”。
It's your indenting of the 8.
When I enter this code:
It compiles correctly. What's happening is that in the first example fn2 is equivalent to:
and the compiler needs something else to finish the let block (I always read let block as "let foo = bar in expr"). So you're missing the expr part. In the fn, you're getting "let b = 8. in -b".