[erlang-questions] Bit syntax matching gotchas

Ameretat Reith ameretat.reith@REDACTED
Fri Feb 5 01:35:33 CET 2016

On Wed, 3 Feb 2016 07:17:02 +0100
Björn Gustavsson <bjorn@REDACTED> wrote:
> We have decided to try do something about it in OTP 19. We have
> discussed various solutions internally in the OTP group, and I have
> spent far too much time lately thinking about it.

Thanks for working on this.

> The reason I reject it is that the matching previously bound variables
> is uncommon. Even in the compiler test suites it is uncommon (test
> suites typically match bound variables more often than production code
> do).

Not that common but nothing prevents coder to write:

update_token(NewToken, ID) ->
  case NewToken of
    <<Time:16, _:PrefLen/bytes, ID:Len/bytes>> when (Now-Time) < 60

That looked logical to me and it took some time to understand I should
change my code to:

update_token(NewToken, ID) ->
  case NewToken of
    <<Time:16, _:PrefLen/bytes, ReceivedID/bytes>> when
      ReceivedID =:= <<ID:Len/bytes>>, (Now-Time) < 60

> Therefore, solution #2 would make behaviour of matching more
> consistent with construction, but would not significantly increase the
> number of correct programs. Also, if clauses that previously didn't
> match start to match, then code that has not been executed before will
> be executed.

I don't think this is very bad.  The code that now will be executed is
most probably intended by coder to be executed and it may solve a
hidden bug too.  My above code surely intended to match, otherwise why
I bothered myself to specify the size?

Alas, this change effects cannot warned to coder.


Now that I know how matching and construction differ, I can accept
solution #1 and I prefer less language changes too.  But if language
correctness really matters, solution #2 is better.

More information about the erlang-questions mailing list