[erlang-questions] bit syntax: 0-sized segments
Fred Hebert
mononcqc@REDACTED
Thu Mar 7 15:02:59 CET 2013
Agreed. There are also some fun issues related to this:
1. [X || <<X:0>> <= <<1,2,3>>]
2. [X || <<_:0,X/binary>> <= <<1,2,3>>]
Both of these list comprehensions end up being infinite loops. The
second one also builds a list infinitely, and can make a node go out of
memory.
Maybe a compiler warning for explicit 0-length patterns followed by a
/binary type (which ends up not moving forward?) would be nice.
In fact, there's also a fun behaviour with the length of /binary that
Yurii Rashkovskii found. In the shell:
1> [X || <<_:8, X/binary>> <= <<"tis a silly place">>].
[<<"is a silly place">>]
Which I would expect. But from a compiled module using the same binary
generator:
2> test:bc().
[<<"is a silly place">>,<<"s a silly place">>,
<<" a silly place">>,<<"a silly place">>,<<" silly place">>,
<<"silly place">>,<<"illy place">>,<<"lly place">>,
<<"ly place">>,<<"y place">>,<<" place">>,<<"place">>,
<<"lace">>,<<"ace">>,<<"ce">>,<<"e">>,<<>>]
Now the X/binary pattern keeps on matching the entire binary, but only
the _:8 pattern is consuming the binary, giving us repeated matching of
the 'tail'. Yurii and I wondered which of the two behaviours was the
'correct' one. I personally do prefer the shell's way of doing things.
Regards,
Fred.
On 03/07, Kostis Sagonas wrote:
> In revising some HiPE test suites for bit syntax, I noticed some
> pattern matching with 0-sized segments. They can be illustrated by
> the following:
>
> Eshell V5.10.2 (abort with ^G)
> 1> <<X:0>> = <<>>.
> <<>>
> 2> X.
> 0
>
> I think this is interesting and some may consider the above pattern
> matching a bit weird, possibly preferring an exception instead.
>
> The documentation is mute about the presence of these 0-sized segments:
>
> http://www.erlang.org/doc/reference_manual/expressions.html#bit_syntax
>
> I think that they need to be explicitly mentioned there, possibly
> together with some rationale for their existence.
>
> Kostis
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list