我开始使用 Elixir。我读到 Elixir 有弱等式 ==
和严格等式 ===
运算符。
来自 JavaScript 背景,==
感觉是个大错误。在 Elixir 中有什么不同吗?缺少什么吗?什么时候应该在 Elixir 中分别使用弱运算符和强运算符?
到目前为止我知道,在 JavaScript 中 "1"== 1
产生 true
而在 Elixir 中它产生 false
。这意味着至少保留了类型信息。如果是这样,那么弱平等有什么用呢?
最佳答案
The difference between
==
and===
is that the latter is more strict when comparing integers and floats:iex> 1 == 1.0 true iex> 1 === 1.0 false
否则它们几乎是一样的:
→ iex
Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> "bro" == "bro"
true
iex(2)> "bro" === "bro"
true
iex(3)> 1 == 1
true
iex(4)> 1.0 == 1.0
true
iex(5)> 1 == 1.0
true
iex(6)> 1 === 1.0
false
关于elixir - Elixir 中的 == 和 === 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51555499/