“let () =” 是什么意思?奥卡梅尔 是什么意思?
有些代码类似于
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个
let
表达式中的()
没有什么特别的,它只是一个模式。所有let
表达式看起来都像let
pattern
=
表达式
在
其他表达式
中。这里的模式将始终匹配,因为print_string
返回unit
,而()
是该类型的唯一值。这样,当第一个表达式实际上更像是一个语句(返回unit
)时,这只是将两个表达式组合成一个表达式的另一种方式。所以你是对的,该构造与使用
;
运算符的含义几乎相同。唯一真正的区别在于优先级。例如,如果您编写,您会发现
f x
总是被调用。;
的优先级太低,无法将第二个表达式置于if
的控制之下。这就是很多人(包括我)养成使用let () =
表达式
习惯的原因。如果你把上面的代码写成f x
仅当x
小于 3 时才被调用,这通常是我想要的。本质上,let
的优先级远高于;
。当然,可能还有其他方法可以达到这种效果,但是使用
let
的好处是您不必稍后在代码中添加任何内容(例如右括号或>结束
)。如果您将print_string
添加为调试语句,这是一种将更改保留在一个位置的便捷方法。There's nothing special about
()
in thislet
expression, it's just a pattern. Alllet
expressions look likelet
pattern
=
expression
in
other-expression
. Here the pattern will always match, becauseprint_string
returnsunit
, 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 (returnsunit
).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 writeyou would find that
f x
is always called. The precedence of;
is too low to pull the second expression under the control of theif
. That's the reason many people (including me) get into the habit of usinglet () =
expression
. If you write the above asthe
f x
is only called whenx
is less than 3, which is usually what I want. In essence, the precedence oflet
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 anend
). If you're adding theprint_string
as a debugging statement, this is a handy way of keeping the changes local to the one spot.Jeffrey 的答案是绝对正确的,但还有一点:
如果你写
And you badly the result type of
fx "something"
编译器将发出警告,该警告可能会在编译过程中丢失。另一方面,如果您编写:编译器将类型检查
fx "something"
的结果是否可以与()
匹配,即它确实是类型 <代码>单位。因此,如果你搞砸了,就会产生错误,这通常更安全。还可以编写
仅获得 Jeffrey 提到的优先级效果,但不进行任何类型检查,因为
_
可以与任何类型的值进行匹配。Jeffrey's answer is absolutely correct, but one more point:
if you write
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:The compiler will type check that the result of
fx "something"
can be matched against()
, i.e. that it is really of typeunit
. Thus if you messed up an error is produced, which is usually more secure.There also is the possibility to write
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.