<div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Hi,</div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
I'm wondering why this </div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<div>test(<<Field:Len/binary, Rest/binary>>, Len) -></div><div> {Len, Field, Rest}.</div></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">does not compile, complaining that "variable 'Len' is unbound", while this</div><div><br><div><font color="#222222" face="arial, sans-serif">test(<<Field:2/binary, Rest/binary>>, Field) -></font></div>
<div><font color="#222222" face="arial, sans-serif"> {Field, Rest}.</font></div></div><div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">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? </font></div>
<div><font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">BTW supplying a variable for Len does work in this case:</font></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<br></div><div><div><font color="#222222" face="arial, sans-serif">test(Bin, Len) -></font></div><div><font color="#222222" face="arial, sans-serif"> <<Field:Len/binary, Rest/binary>> = Bin, </font></div><div>
<font color="#222222" face="arial, sans-serif"> {Len, Field, Rest}.</font></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br></div></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">
Any insights?</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px"><br></div><div><font color="#222222" face="arial, sans-serif">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.</font></div>
<div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<br></div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Thanks,</div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Erik.</div>