[erlang-questions] Binary match in function head doesn't compile
Erik Pearson
erik@REDACTED
Fri Oct 26 20:27:06 CEST 2012
Hi,
I'm wondering why this
test(<<Field:Len/binary, Rest/binary>>, Len) ->
{Len, Field, Rest}.
does not compile, complaining that "variable 'Len' is unbound", while this
test(<<Field:2/binary, Rest/binary>>, Field) ->
{Field, Rest}.
does. For some reason the compiler doesn't see the Len from the match spec
in the arguments, but it does see Field. Is that by design?
BTW supplying a variable for Len does work in this case:
test(Bin, Len) ->
<<Field:Len/binary, Rest/binary>> = Bin,
{Len, Field, Rest}.
Any insights?
The reason I ask is that a way of saving memory allocation when parsing
binaries might be to walk the binary recursively, incrementing the Len,
while matching on something else that follows. This would result in a
sub-binary reference and not a sub-binary copy. This is not my idea (thanks
for the tip Dimitry) but I think it loses efficiency if the match state
can't be carried recursively in the function head.
Thanks,
Erik.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121026/dd6457d3/attachment.htm>
More information about the erlang-questions
mailing list