EEP: Singleton Binary Literal Types

This EEP extends EEP 8 to allow binary literals such as <<"start">> and <<"stop">> as singleton types in -type, -spec, -callback, and record field type declarations. It also supports encoding annotations (<<"café"/utf8>>) and sigil syntax (~"hello") for binary literal types. This brings binaries to parity with atoms and integers, which already support singleton type values in Erlang’s type system.

For example, currently a function that accepts only specific binary values cannot express this constraint:

%% Intent: only <<"start">> or <<"stop">> accepted
-spec handle(binary()) -> ok.

With this EEP, the programmer can write:

-type cmd() :: <<"start">> | <<"stop">>.
-spec handle(cmd()) -> ok.

Other supported forms:

%% Encoding annotation for non-Latin-1 characters
-type utf8_name() :: <<"café"/utf8>>.

%% Sigil syntax (always UTF-8)
-type greeting() :: ~"hello".

%% In -callback, records, and maps
-callback on_event(<<"open">> | <<"close">>) -> ok.
-record(msg, {tag :: <<"hello">> | <<"world">>}).
-type headers() :: #{<<"accept">> => binary()}.

EEP: Add EEP for singleton binary literal types by williamthome · Pull Request #84 · erlang/eep · GitHub
Implementation PR: Implement singleton binary literal types by williamthome · Pull Request #10816 · erlang/otp · GitHub

8 Likes