집합 이론 타입 치트시트

집합 이론 타입 치트시트 (Set-theoretic types cheatsheet)

Elixir의 타입 시스템은 타입을 집합처럼 다루는 집합 이론 타입(set-theoretic types)을 기반으로 해요. 이 치트시트는 그런 타입들을 표현하는 문법을 한눈에 볼 수 있게 정리한 페이지예요. 타입 검사기나 다이얼라이저를 쓸 때 이 표기법을 거의 그대로 마주치게 될 거예요.

출처: Set-theoretic types cheatsheet (Elixir v1.20.4)

본문

집합 연산자 (Set operators)

타입은 집합으로 취급되기 때문에, 연산자를 이용해 타입끼리 합치거나 교집합을 구하고, 차집합을 만들 수 있어요.

합집합 (Union)

type1 or type2

교집합 (Intersection)

type1 and type2

차집합 (Difference)

type1 and not type2

부정 (Negation)

not type

데이터 타입 (Data types)

넓은 타입 (Broad types)

bitstring()
binary()
empty_list()
integer()
float()
pid()
port()
reference()

binary()bitstring()의 하위 타입이에요.

아톰 (Atoms)

모든 아톰
atom()
개별 아톰
:ok
:error
SomeModule

함수 (Functions)

모든 함수
function()
n항 함수
(-> :ok)
(integer() -> boolean())
(binary(), binary() -> binary())
여러 절 (Multiple clauses)
(integer() -> binary()) and (binary() -> atom())

맵 (Maps)

모든 맵
map()
빈 맵
empty_map()
아톰 키를 가진 맵
# name, age 키만 가짐
%{name: binary(), age: integer()}

# name 키를 가지며 age는 선택적
%{name: binary(), age: if_set(integer())}

# name, age 키를 가지며 다른 키도 있을 수 있음 (열린 맵)
%{..., name: binary(), age: integer()}

# name 키를 가지며 다른 키도 있을 수 있고, age는 없음
%{..., name: binary(), age: not_set()}
영역 키(domain key)를 가진 맵 (영역 키는 항상 선택적으로 취급돼요)
# 아톰, 바이너리 키를 가짐
%{atom() => binary(), binary() => binary()}

# 아톰, 바이너리 키를 가지며 다른 키도 있을 수 있음 (열린 맵)
%{..., atom() => binary(), binary() => binary()}
혼합 키를 가진 맵
# 아톰 키에 바이너리 값, 그러나 `:root` 키는 integer 타입
%{atom() => binary(), root: integer()}

# 위와 같되 다른 키도 있을 수 있음
%{..., atom() => binary(), root: integer()}
영역 키에는 atom(), binary(), integer(), float(), fun(), list(), map(), pid(), port(), reference(), tuple()이 올 수 있어요

비어 있지 않은 리스트 (Non-empty lists)

정규 리스트 (Proper lists)
non_empty_list(elem_type)
비정규 리스트 (Improper lists) — tail_type에 리스트가 포함되지 않을 때만
non_empty_list(elem_type, tail_type)

튜플 (Tuples)

모든 튜플
tuple()
n원소 튜플
{:ok, binary()}
{:error, binary(), term()}
{pid(), reference()}
최소 n원소 튜플
{binary(), binary(), ...}

편의를 위한 추가 타입 (Additional types for convenience)

흔한 별칭 (Common aliases)

boolean() = true or false
number() = integer() or float()

리스트 별칭 (List aliases)

list() = empty_list() or non_empty_list(term())
list(a) = empty_list() or non_empty_list(a)
list(a, b) = empty_list() or non_empty_list(a, b)

더 알아보기