Hi,
I’ve been stuck with the following problem for some time now.
What I’m trying to achieve is to generate a family of functions for binary matching various integer sizes and signednesses, something like this:
for bits <- [8, 16, 32, 64], {schar, sign} <- [{"i", :signed}, {"u", :unsigned}] do
def unquote(:"get_#{schar}#{bits}")(bin) do
<<val::unquote(sign)-unquote(bits), bin::binary>> = bin
{val, bin}
end
end
While the size specifier unquote(bits)
works, the unquote(sign)
fails at compile time with an error “unknown bitstring specifier: :signed”.
Of course I can work around this by “unrolling” the sign — I can def unquote(:"get_i#{bits})"
and def unquote(:"get_u#{bits}")
, but that does not help me to better understand the language.
So, could anyone with stronger Elixir-fu shed some light on this, please? Is there a way to do this, or is it too much an obscure thing to do?