“let () =” 是什么意思?奥卡梅尔 是什么意思?

发布于 2024-12-06 05:48:49 字数 194 浏览 1 评论 0原文

有些代码类似于

let () = print_string "something" in
fn

某些 OCaml 代码。

这意味着什么? “()”有什么特殊含义吗?或与以下含义相同

print_string "something";
fn

There are codes like

let () = print_string "something" in
fn

in some OCaml codes.

What does this mean? Is there special meaning on "()"? or Is it same meaning as

print_string "something";
fn

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

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

发布评论

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

评论(2

灼痛 2024-12-13 05:48:49

这个 let 表达式中的 () 没有什么特别的,它只是一个模式。所有 let 表达式看起来都像 let pattern = 表达式 其他表达式中。这里的模式将始终匹配,因为 print_string 返回 unit,而 () 是该类型的唯一值。这样,当第一个表达式实际上更像是一个语句(返回 unit)时,这只是将两个表达式组合成一个表达式的另一种方式。

所以你是对的,该构造与使用 ; 运算符的含义几乎相同。唯一真正的区别在于优先级。例如,如果您编写,

if x < 3 then
    print_string "something";
    f x

您会发现f x总是被调用。 ; 的优先级太低,无法将第二个表达式置于 if 的控制之下。这就是很多人(包括我)养成使用 let () = 表达式 习惯的原因。如果你把上面的代码写成

if x < 3 then
    let () = print_string "something"
    in f x

f x 仅当 x 小于 3 时才被调用,这通常是我想要的。本质上,let的优先级远高于;

当然,可能还有其他方法可以达到这种效果,但是使用 let 的好处是您不必稍后在代码中添加任何内容(例如右括号或 >结束)。如果您将 print_string 添加为调试语句,这是一种将更改保留在一个位置的便捷方法。

There's nothing special about () in this let expression, it's just a pattern. All let expressions look like let pattern = expression in other-expression. Here the pattern will always match, because print_string returns unit, and () is the only value of that type. In this way, it's just another way of combining two expressions into one when the first one is really more of a statement (returns unit).

So you're right, the construct has pretty much the same meaning as using the ; operator. The only real difference is in the precedence. If, for example, you write

if x < 3 then
    print_string "something";
    f x

you would find that f x is always called. The precedence of ; is too low to pull the second expression under the control of the if. That's the reason many people (including me) get into the habit of using let () = expression. If you write the above as

if x < 3 then
    let () = print_string "something"
    in f x

the f x is only called when x is less than 3, which is usually what I want. In essence, the precedence of let is much higher than ;.

Of course there are may other ways to get this effect, but the nice thing about using let is that you don't have to add anything later on in the code (like a closing parenthesis or an end). If you're adding the print_string as a debugging statement, this is a handy way of keeping the changes local to the one spot.

何处潇湘 2024-12-13 05:48:49

Jeffrey 的答案是绝对正确的,但还有一点:

如果你写

fx "something";
fn

And you badly the result type of fx "something" 编译器将发出警告,该警告可能会在编译过程中丢失。另一方面,如果您编写:

let () = fx "something" in
fn

编译器将类型检查 fx "something" 的结果是否可以与 () 匹配,即它确实是类型 <代码>单位。因此,如果你搞砸了,就会产生错误,这通常更安全。

还可以编写

let _ = fx "something" in
fn

仅获得 Jeffrey 提到的优先级效果,但不进行任何类型检查,因为 _ 可以与任何类型的值进行匹配。

Jeffrey's answer is absolutely correct, but one more point:

if you write

fx "something";
fn

And you messed up the result type of fx "something" the compiler will emit a Warning, that might get lost during compilation. On the other hand if you write:

let () = fx "something" in
fn

The compiler will type check that the result of fx "something" can be matched against (), i.e. that it is really of type unit. Thus if you messed up an error is produced, which is usually more secure.

There also is the possibility to write

let _ = fx "something" in
fn

which will only get the precedence effect that Jeffrey mentioned, but not do any type checking, since _ can be matched against values of any type.

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