[erlang-questions] Bit syntax matching gotchas

Björn Gustavsson <>
Wed Feb 3 16:06:49 CET 2016


On Wed, Feb 3, 2016 at 12:00 PM, José Valim
<> wrote:
> Björn, in solution #1, would you warn only when matching or also when
> constructing? Is the warning only at compile-time or also at runtime? For
> example, would you warn for:
>
>     X = -1.
>     <<X>>.

Yes, I have considered warning for construction too.
The warnings would only occur at compile-time.

There would NOT be a warning for your example, though.
More on that below.

> We may have a third option which is to control the masking behaviour with a
> new flag. From Erlang 19, we could warn if you are relying on masking and
> you would need to pick to mask (/integer-mask) or fix your code. The default
> after the warning is removed is to always raise. The mask option wouldn't be
> supported when matching, making it clear the masking behaviour is
> construction only.

How could the compiler warn you that you depend on masking?

Consider this code:

f(A) ->
  <<A:16>>.

The compiler has no way of knowing whether masking is
needed or not.

We have no plans for changing how construction works in
OTP 19.


Regarding construction, note that signed/unsigned is
ignored. All of the following examples construct the
same binary:

<<255>>
<<-1>>
<<-1/signed>>
<<-1/unsigned>>

Since signed/unsigned has never been enforced
for construction, starting to warn for <<-1>> and
<<-1/unsigned>> would cause many new annoying
warnings. Therefore, no warnings should be
produced.

However, the following examples should produce
warnings:

<<-55555:8>>
<<100000:8>>

The two values above cannot be represented in
8 bits, which points to a real bug.

/Björn


-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list