Overly Helpful Compiler Warning?

I see this compiler warning,

a binary field without size is only allowed at the end of a binary pattern and never allowed in binary generators

when trying this pattern match,

fn %{"key" => value} -> ...

It seems a perfectly fine message, just not very relevant here?

Or am I missing something?

Is there a better way?

Can you provide a full example which triggers the error?

Now I see, it’s the string interpolation that triggers this, i.e. something like,

fn %{"#{key}" => value} -> ...

(i.e. literally matching one key in the Map)

but should I be triggering the warning nevertheless?

This compiles roughly to "" <> to_string(key) <> "", and the binary length of the function call can’t be determined at compile time, therefore this is forbidden.

You can make it work like this:

key = to_string(key)
fn %{^key => value} -> ... end
4 Likes