为什么我可以把 |>在下一行但是 <|给出语法错误?

发布于 2024-11-07 09:44:38 字数 204 浏览 3 评论 0 原文

我可以这样做:

let foo = bar
    |> baz

但是当我这样做时出现语法错误:

let foo = bar
    <| baz

为什么?

当我定义自己的中缀运算符并尝试以这种方式使用它们时,我也会遇到语法错误。

I can do this:

let foo = bar
    |> baz

but I get a syntax error when I do this:

let foo = bar
    <| baz

why?

I also get the syntax error when ever I define my own infix operators and try to use them in this manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

长安忆 2024-11-14 09:44:38

除非我遗漏了什么,否则这两个运算符(以及任何其他中缀运算符)的行为是相同的。当我定义 barbaz 并使用您的示例时,我在这两种情况下都会收到错误。 F# 中的越位规则允许您将运算符缩进距左侧较远的位置,但参数列必须匹配:

let bar = 1
let baz x = x + 1

let foo1 = bar 
        |> baz

let foo2 = baz 
        <| bar

这两个都会给出错误消息:(

let foo1 = bar 
  |> baz

let foo2 = baz 
  <| bar

尽管您可能只看到第一个错误,因为 F#编译器在语法错误后停止报告其他错误 - 可能是因为它不能保证它们是明智的)

规则在 MSDN 上的代码格式指南

Unless I'm missing something, the two operators (as well as any other infix operator) behave the same. When I define bar and baz and use your example, I get an error in both of the cases. The off-side rule in F# allows you to indent the operator less far from the left, but the column of the arguments has to match:

let bar = 1
let baz x = x + 1

let foo1 = bar 
        |> baz

let foo2 = baz 
        <| bar

Both of these give an error message:

let foo1 = bar 
  |> baz

let foo2 = baz 
  <| bar

(Although you may see only the first error, because the F# compiler stops reporting additional errors after syntax error - probably because it cannot guarantee that they will be sensible)

The rules are quite well described in Code Formatting Guidelines on MSDN.

书间行客 2024-11-14 09:44:38

我没有发现缩进错误,因为以下两项都有效:

match r.IsDBNull index  with
| true -> None
| _ -> r.GetValue index
    :?> 'a
    |> Some

let param p =
    db.CreateParameter p
    |> cmd.Parameters.Add
    |> ignore

即使我只按了一次 Tab 键。然而,我在这里按了一次制表符,它没有正确缩进:

let foo2 = proc "getfoo2" 
    <| [ "x",Int32,In;
         "y",String(15),In ]

它应该是两者中的任何一个:

let foo2 = proc "getfoo2" 
         <| [ "x",Int32,In;
              "y",String(15),In ]

let foo2 = 
    proc "getfoo2" 
    <| [ "x",Int32,In;
         "y",String(15),In ]

所以我的误解是空格并不重要,只要它比之前的行缩进得更多。事实并非如此。

I did not catch the indentation error because both of the following were working:

match r.IsDBNull index  with
| true -> None
| _ -> r.GetValue index
    :?> 'a
    |> Some

let param p =
    db.CreateParameter p
    |> cmd.Parameters.Add
    |> ignore

even though I had only hit tab once. Yet I hit tab once here, and it was not correctly indented:

let foo2 = proc "getfoo2" 
    <| [ "x",Int32,In;
         "y",String(15),In ]

it should have been either of the two:

let foo2 = proc "getfoo2" 
         <| [ "x",Int32,In;
              "y",String(15),In ]

let foo2 = 
    proc "getfoo2" 
    <| [ "x",Int32,In;
         "y",String(15),In ]

So my misunderstanding was that whitespace didn't matter so long as it was further indented than the line before it. This is not the case.

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