[erlang-questions] Booleans in bit syntax

Viktor Söderqvist viktor@REDACTED
Sat Jan 20 14:32:28 CET 2018


Adding a boolean type specifier in the bit syntax would be useful, I
think. Then you could write

    decode_stuff(<<Flag1/boolean, Flag2/boolean, 0:6, Rest/binary>>) ->
        {Flag1, Flag2, Rest}.

instead of

    decode_stuff(<<F1:1, F2:1, 0:6, Rest/binary>>) ->
        Flag1 = case F1 of 1 -> true;
                           0 -> false
                end,
        Flag2 = case F2 of 1 -> true;
                           0 -> false;
                end,
        {Flag1, Flag2, Rest}.

Many binary protocols contain flags and indicators, typically used as
booleans in application logic with 1 for true, 0 for false.

The default size would be 1 bit, i.e. Size = 1 and Unit = 1. It would be
for constructing binaries as well. It is intuitive and it would be
backward compatible.

What do you think? Any reason for not adding it?

Viktor



More information about the erlang-questions mailing list