I would love to see binary literals in Elm. We have decimal literals (
1337) and hexadecimal literals (
0xFF) for now. But in some cases, binary literals would help to produce clearer code. Since we now got
elm/bytes, we have more use-cases for low-level bit manipulation.
One example where binary literals are helpful to produce clearer code is bitmasking for multiple bits at once. To check if bits 2, 3 and 4 are set, we currently have to write
Bitwise.and 0x1C value == 0x1C. In this case it’s not obvious which bits are being checked for. If we had binary literals, it could look like
Bitwise.and 0b11100 value == 0b11100 where the checked bits are written down explicitly.
For now, I always write masks like this in hexadecimal and add a comment with the actual bit-pattern for better readability.
I am dabbling with implementing DEFLATE which can then be used to implement
elm-gzip. With that project, binary literals are even more helpful, as you have to work with groups of bits smaller than 8 a lot.
I thought I bring this issue up and see what the community thinks about this. Maybe this can lead to binary literals being implemented or at least a start a discussion why they shouldn’t.