Reason
  • 문서
  • 해보기
  • API
  • 커뮤니티
  • 블로그
  • Languages icon한국어
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • 번역 돕기
  • GitHub

›언어 기본

소개

  • What & Why

Setup

  • 설치
  • 에디터 플러그인

언어 기본

  • 개요
  • Let 바인딩
  • 원시 타입
  • 기본 자료구조
  • 타입
  • 레코드
  • Variant
  • Options and nullability
  • 함수
  • 재귀
  • 비구조화
  • 패턴 매칭
  • Mutable Bindings
  • 반복문
  • Modules

Advanced Features

  • JSX
  • 외부 접근
  • 예외
  • 오브젝트

JavaScript

  • 연동
  • 문법 치트시트
  • Pipe First
  • 프라미스
  • 라이브러리
  • JS에서 변환

추가 사항

  • 자주 물어보는 질문
  • 추가적으로 매력적인 것들
Translate

Options and nullability

Quick overview: Options

Options are a built-in variant that represent the presence or absence of a value. It is similar to the concept of "nullable" values in other languages. Options are used often.

In Reason there is no "null" value that any type might assume. Instead if a value needs to communicate that it might (or might not) be available, then the option type is used.

정의

The option type is a variant with two constructors:

  • Some represents the presence of a value and accepts that value as an argument
  • None represents the absence of a value and acceps no arguments
type option('value) =
  | None
  | Some('value);

Note: This type is already defined and automatically available for use everywhere.

예제

It is common for None to be the default value before something is setup.

type person = {
  name: string,
  age: int,
};

let nobody: option(person) = None;

Logging in can set up the user's data:

let login = () =>
  Some({
    name: "Alice",
    age: 42,
  });

let alice = login();

Now when writing functions that deal with users we must make sure that there is actually a user present:

let happyBirthday = (user) => {
  switch (user) {
  | Some(person) => "생일 축하헤, " ++ person.name
  | None => "로그인부터 해주세요"
  };
};
← Variant함수 →
  • 정의
  • 예제