f# 中的缩进错误?

发布于 2024-10-31 07:43:12 字数 269 浏览 1 评论 0原文

考虑以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红尘作伴 2024-11-07 07:43:12

F# 允许各种类型的“缩进”,允许您使用较小的缩进,但仍保留在同一表达式内。

其中一项合法的“取消”是针对运营商的。您可以编写

    foo
        |> bar
        |> baz

or

    foo
    |> bar
    |> baz

or Even

    foo
 |> bar
 |> baz

,并且中缀运算符在后续行中继续相同的表达式。规则是允许您“取消缩进”“中缀运算符的长度加一个空格”,其目的是允许您对齐正在使用的值。常见情况就像数字表,例如

let x = 
    42
  + 21
  + 62

允许 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

    foo
        |> bar
        |> baz

or

    foo
    |> bar
    |> baz

or even

    foo
 |> bar
 |> baz

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

let x = 
    42
  + 21
  + 62

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.

怪我闹别瞎闹 2024-11-07 07:43:12

fn2 被解析为:

let fn2 () =
  let b = 8. - b

let 块未完成,它需要一个返回值,例如。

let fn2 () =
  let b = 8. - b
  b

我建议您在二元运算符周围使用空格,并且在一元运算符之后不使用空格。
同样,x - 2(减法)和 x -2(使用参数 -2 调用函数 x)之间也存在差异。

fn2 is parsed as:

let fn2 () =
  let b = 8. - b

The let block is unfinished, it needs a return value, eg.

let fn2 () =
  let b = 8. - b
  b

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) and x -2 (call function x with argument -2).

没企图 2024-11-07 07:43:12

这是你对8的缩进。
当我输入此代码时:

let fn () =
    let b =
        8.
    -b

let fn2 () = 
    let b =
        8.
    - b

它可以正确编译。发生的情况是,在第一个示例中, fn2 相当于:

let b = 8 - b

并且编译器需要其他东西来完成 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:

let fn () =
    let b =
        8.
    -b

let fn2 () = 
    let b =
        8.
    - b

It compiles correctly. What's happening is that in the first example fn2 is equivalent to:

let b = 8 - b

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文