#TIL #elixirlang ! 문자로 끝나는 함수 이름 컨벤션

Enum.fetch/2, Enum.fetch!/2 함수의 차이점은? 에러 발생 여부다

iex> Enum.fetch([2, 4, 6], 2)
{:ok, 6}

iex> Enum.fetch([2, 4, 6], 4)
:error

:error 리턴. 튜플 리턴이 기본값.

iex> Enum.fetch!([2, 4, 6], 2)
6

iex> Enum.fetch!([2, 4, 6], 4)
** (Enum.OutOfBoundsError) out of bounds error

에러 발생. 원소값 리턴이 기본값.

느낌표 따위 함수 이름으로 얼마든지 쓸 수 있다. 에러를 일으키는 함수 이름 끝에는 ! 문자를 붙인다.

Feedback plz <3 @ohyecloudy, ohyecloudy@gmail.com

A Random Post