Elixir

Date Types

Internal Data Representation

Package Management

Syntax

Double equals (==) v.s. Triple equals (===)

Elixir offers two options for comparing string equality, == and === operators. The == operator is generally the simplest though the other produces the same result.

가장 큰 차이는 float 비교이다. ===이 좀 더 strict한 비교를 한다.

iex> 1 == 1    #true
iex> 1 == 1.0  #true
iex> 1 === 1   #true
iex> 1 === 1.0 #false

iex> 1 != 2    #true
iex> 1 != 1.0  #false
iex> 1 !== 2   #true
iex> 1 !== 1.0 #true

그 외에는 차이가 없다고 봐도 무방하다.

번외로, Elixir와 Erlang에서의 문법적 차이도 존재한다.

Elixir | Erlang
==     | ==
===    | =:=
!=     | /=
!==    | =/=

출처: StackOverflow, StackOverflow 2

Single-quoted (') v.s. Double-quoted (") Strings

iex> is_list('Example')
true
iex> is_list("Example")
true

iex> is_binary('Example')
false
iex> is_binary("Example")
true

iex> 'Example' == "Example"
false

분명 두 개에는 차이가 존재한다. 어떤 차이가 있을까?

Single quoted string, is actually a charlist, aka a list of chars. If you want to concatenate them, you need to use list concatenation operator '++'.

'hello' ++ ' ' ++ 'world' 

Double quoted string, is a UTF-8 encoded 'binary', aka a series of bytes. Their concatenation is done via '<>'.

"hello" <> " " <> "world"

정리하자면, single quoted string은 charlist (list of chars)고, double quoted string은 UTF-8로 인코딩된 "바이너리"이다. 즉, 일련의 바이트다.

따라서 double quoted string의 경우 다음이 성립한다.

iex> is_binary("Example")
true
iex> <<"Example">> === "Example"

단, 주의할 점은 Erlang에서는 다르다.

1> is_list('Example').
false
2> is_list("Example").
true
3> is_binary("Example").
false
4> is_binary(<<"Example">>).
true

Elixir와 Erlang의 차이는 하단의 Elixirschool에서 확인해보는 것이 좋다.

출처: Elixirschool, Blogspot

Last updated