Bitstring not matching as expected

Hi,
This is probably a silly question, but while performing a match on bitstring, I got bit (yes!) by a mistake that seems to me easy to do; I found the hard way that the following pattern was not matching as expected: <<FullData:FullLen/binary, _NextChunk>> -> [...], whereas <<FullData:FullLen/binary, _NextChunk/binary>> did.

My question: what would be the meaning of the first form? If no sensible one existed, could it be rejected by the compiler, or at least could a warning be issued?

Thanks in advance for any hint,
Best regards,

Olivier.

The default size is a unsigned byte. If you write this:

<<A,B,C>> = <<1,2,3>>

It makes more sense as the syntax of the left side then reflects the right side.

3 Likes

Thanks Lukas for your answer! When reading Expressions — Erlang System Documentation v27.2.2 I could see that, in the first form, _NextChunk was falling in the case described as When used in a bit string matching, Value must be a variable, or an integer, float, or string. where it was here an (unbound) variable with no Size or TypeSpecifier. I understand that making it correspond to an integer / unsigned byte is indeed the most logical candidate, but I could not confirm conclusively it by reading this Bit Syntax Expressions section. Maybe I misread it, or it is not so clearly stated there. The point that is a bit less intuitive in that case is that often specifying a mute variable is interpreted as a request to “match anything”.

Thanks again!
Olivier.

I too cannot find any place where the default type is described, only what the deafult sizes are for the different types. A PR adding that information would be most welcome.

That’s fair! Here is a proposal: Clarify default type of matched values in bit strings by Olivier-Boudeville · Pull Request #9425 · erlang/otp · GitHub

3 Likes