[erlang-questions] Booleans in bit syntax
Jacob
jacob01@REDACTED
Sat Jan 20 15:53:29 CET 2018
On 20.01.2018 14:32, Viktor Söderqvist 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}.
What about
decode_stuff(<<F1:1, F2:1, 0:6, Rest/binary>>) ->
{F1 =:= 1, F2 =:= 1, Rest}
it's even shorter than your proposal and already works (okay, encoding
bools is a different thing).
> What do you think? Any reason for not adding it?
In my opinion, it changes much without really filling a gap. So far, the
<<>> expressions/matches only convert numbers to binaries/bitstrings and
vice versa.
Also, encoding 'true' as 1 and 'false' as 0 is just one possible
encoding (even if it is common).
I prefer to have explicit conversions, IMO they are much better to
maintain than using conversion magic.
Jacob
More information about the erlang-questions
mailing list