当与一般术语一起使用时,Erlang中的==和=:=有什么区别? [英] What is the difference between == and =:= in Erlang when used with terms in general?
问题描述
除了 =:=
阻止不必要的整数转换的事实:
1> 1 =:= 1.0。
false
使用 =:=
一般的条款?
更好的表现?
=:=
的最大优点是与模式匹配相同的方式仅对相同的术语返回true。所以你可以确定它们是一样的。 1
和 1
是相同的术语, 1
与 1.0
不是。而已。如果您在A =:= B - >时写入 foo(A,B)当A =:= B - >时,A。
和 bar(A,B) B。
他们将表现相同。如果你使用 ==
它将不会是相同的功能。它只是防止惊喜。例如,如果您进行某些键/值存储,如果您使用键 1
存储值,则会不正确,然后如果要求键$ code,则获取此值> 1.0 。是的,有一点点性能损失与 ==
,但最令人惊讶的是更重要。只要使用 =:=
和 = / =
,那么您打算比较相同的条款。只有当您打算比较数字时,才能使用 ==
和 / =
。
Apart from the fact that =:=
prevents unwanted integer casts:
1> 1=:=1.0.
false
What is the advantage of using =:=
with terms in general?
Better performance?
The biggest advantage of =:=
is it returns true only for same terms in the same way as pattern matching. So you can be sure they are same. 1
and 1
are same terms and 1
with 1.0
are not. That's it. If you write function like foo(A, B) when A =:= B -> A.
and bar(A, B) when A =:= B -> B.
they will behave same. If you use ==
it will not be same functions. It simply prevents surprise. For example, if you make some key/value storage it would not be right if you store value with key 1
and then get this value if ask for key 1.0
. And yes, there is a little bit performance penalty with ==
but least astonishment is far more important. Just use =:=
and =/=
when it is your intent to compare same terms. Use ==
and /=
only if it is your intent to compare numbers.
这篇关于当与一般术语一起使用时,Erlang中的==和=:=有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!