gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Boolean in Bit Strings

Open varnerac opened this issue 2 years ago • 6 comments

It'd be cool to support 1 -> True and 0 -> False (and vice versa) in bit string patterns and values.

varnerac avatar Jun 13 '22 02:06 varnerac

Maybe, though I worry that there's not a universal binary format for bools. How should they be encoded?

lpil avatar Jun 16 '22 08:06 lpil

So, I was thinking only of this case:

<<0:1> -> False
<<1:1>> -> True

varnerac avatar Jun 16 '22 13:06 varnerac

Also, the syntax could probably benefit most boolean encodings. For example, if you happen to encode a boolean as a byte, you could match on <<0:unsigned-int-size(7), my_flag:bool>>

varnerac avatar Jun 17 '22 12:06 varnerac

I've asked on twitter and it seems we're split between <<0:1>> and <<0:8>>. Erlang uses <<0:8>>

1> erlang:term_to_binary(1).
<<131,97,1>>
2> erlang:term_to_binary(0).
<<131,97,0>>

lpil avatar Jun 21 '22 08:06 lpil

<<b1:bool-size(1)>>
<<b2:bool-size(8)>>

varnerac avatar Jun 21 '22 11:06 varnerac

Let's have it default to 1 byte

lpil avatar Jun 23 '22 16:06 lpil

Closing as this hasn't been asked for in a while and the alternative is easy.

lpil avatar Oct 07 '23 20:10 lpil