[erlang-questions] Booleans in bit syntax

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Sun Jan 21 19:00:17 CET 2018


Most languages with static typing does not permit the mixing of integers
and boolean values unless you explicitly convert between them. At leas the
languages which are sane do.

Whenever you propose a change for syntactic convenience, it is important to
think about the dual as well: if we introduce the feature, does it lead to
increased error rates due to misunderstanding. Here, a crash is
considerably less of a problem than a silent acceptance of some value which
the programmer might not have wanted.

This is why it is sometimes better to have the conversion be explicit in
the program, because the removal of (dual) errors makes up for the extra
typing that could be needed in the process.

Any bit on a wire is a representation of some underlying state or value. If
I were to handle this, I would seek to map this representation into a
meaningful internal state. And here I note that a boolean value isn't
always what you want because it bears too little meaning. It is often
better to invent meaningful atoms and pass those around as it improves the
readability of the code. For instance, we could have written
gen_tcp:listen/2 and gen_tcp:listen/16 and then supplied a large list of
true false values, but it wouldn't work out. Rather, we pass in a list of
options which are then parsed and interpreted. Some of those become bits in
the underlying protocol, but we hide this from the high-level interface.

On Sun, Jan 21, 2018 at 12:25 PM Ian Hobson <hobson42@REDACTED> wrote:

>
> On 20/01/2018 13:32, Viktor Söderqvist wrote:
> > Many binary protocols contain flags and indicators, typically used as
> > booleans in application logic with 1 for true, 0 for false.
>
> Your description of the convention is loose and incomplete. I fear it
> risks the wrong decision being made.
>
> The convention is better expressed as...
>    0 and anything that auto-converts to 0 is false.
>    Everything else that can be compared is true.
>    The system returns 0 for false, and 1 for true
>
> Examples:
> Php has  0, 0.0, "", null, and an empty array as all false,
> while -1, NaN, and resources are all true.
>
> In Java Script 0, -0, "", undefined, null, NaN, and false are false.
> Everything with value is true.
>
> C has had 0 for false, while true was often 1 but varied with version
> and programmer. Since C99 implementation has been standardized and
> hidden inside stdbool.h.
>
> C++ would be the same as C.
>
> Python has None, False, zero in any form, '', {}, [] and (), and user
> defined types with a len() of zero as false. Everything else is True.
> Also the operations 'and' and 'or' return one of their operands, and not
> 1 or 0.
>
> As for your proposal, I think it adds complication without adding utility.
>
> Ian
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180121/57280fc8/attachment.htm>


More information about the erlang-questions mailing list