[erlang-questions] Booleans in bit syntax

Guilherme Andrade g@REDACTED
Sat Jan 20 14:37:27 CET 2018


Hi Viktor,

I've recently wondered exactly the same. It would be very helpful in doing
away with boilerplate helpers for converting single bits into booleans and
vice-versa.

On 20 January 2018 at 13:32, Viktor Söderqvist <viktor@REDACTED>
wrote:

> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
Guilherme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180120/19e14d56/attachment.htm>


More information about the erlang-questions mailing list