[erlang-questions] Booleans in bit syntax

Ian Hobson hobson42@REDACTED
Sun Jan 21 12:25:04 CET 2018


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




More information about the erlang-questions mailing list