简体   繁体   English

Elixir中==和===有什么区别?

[英]What is the difference between == and === in Elixir?

I am starting out with Elixir. 我从Elixir开始。 I read that Elixir has weak equality == and strict equality === operator. 我读到Elixir具有弱等于==和严格等于===运算符。

Coming from the JavaScript background, == feels like a big mistake. 来自JavaScript背景, ==感觉像是一个大错误。 Is it any different in Elixir? Elixir有什么不同吗? Am missing something? 缺少什么吗? When should I use weak and strong operator respectively in Elixir? 我应该何时在Elixir中分别使用弱运算符和强运算符?

So far I know, In JavaScript "1" == 1 yields true while in Elixir it yields false . 到目前为止,我知道,在JavaScript中"1" == 1产生true而在Elixir中则产生false It means at least type information is preserved. 这意味着至少保留类型信息。 If this is so, then what is the use of weak equality? 如果是这样,那么弱平等的用途是什么?

According to the official documentation : 根据官方文件

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 

Otherwise they're pretty much the same: 否则,它们几乎是相同的:

→ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM