为什么我可以把 |>在下一行但是 <|给出语法错误?
我可以这样做:
let foo = bar
|> baz
但是当我这样做时出现语法错误:
let foo = bar
<| baz
为什么?
当我定义自己的中缀运算符并尝试以这种方式使用它们时,我也会遇到语法错误。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非我遗漏了什么,否则这两个运算符(以及任何其他中缀运算符)的行为是相同的。当我定义
bar
和baz
并使用您的示例时,我在这两种情况下都会收到错误。 F# 中的越位规则允许您将运算符缩进距左侧较远的位置,但参数列必须匹配:这两个都会给出错误消息:(
尽管您可能只看到第一个错误,因为 F#编译器在语法错误后停止报告其他错误 - 可能是因为它不能保证它们是明智的)
规则在 MSDN 上的代码格式指南。
Unless I'm missing something, the two operators (as well as any other infix operator) behave the same. When I define
bar
andbaz
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:Both of these give an error message:
(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.
我没有发现缩进错误,因为以下两项都有效:
即使我只按了一次 Tab 键。然而,我在这里按了一次制表符,它没有正确缩进:
它应该是两者中的任何一个:
所以我的误解是空格并不重要,只要它比之前的行缩进得更多。事实并非如此。
I did not catch the indentation error because both of the following were working:
even though I had only hit tab once. Yet I hit tab once here, and it was not correctly indented:
it should have been either of the two:
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.