I’ve got a macro that I’d like to look like this:
-define(_decode_bool(Variable, Input, Rest),
{Variable, Rest} = case Input of
<<0:8/big, Rest_/binary>> -> {false, Rest_};
<<1:8/big, Rest_/binary>> -> {true, Rest_}
end
).
It’s used in generated code that looks like this:
decode_preferences(Bin) ->
?_decode_bool(LikesCats, Bin, Rest1),
?_decode_bool(LikesDogs, Rest1, Rest2),
{#{likes_cats => LikesCats, likes_dogs => LikesDogs}, Rest2}.
But when I write it like that, I get a warning: “variable ‘Rest1’ exported from ‘case’”. This is similar to my earlier question.
So, to force scoping, I’ve had to introduce a function, like this anonymous one, here:
-define(_decode_bool(Variable, Input, Rest),
{Variable, Rest} = (fun
(<<0:8/big, Rest/binary>>) -> {false, Rest};
(<<1:8/big, Rest/binary>>) -> {true, Rest}
end)(
Input
)
).
I’m wondering if this is the best I can do. Specifically: is there any way to do some token-pasting magic with Rest_
in the original macro (using ?LINE
, for example) to make those variables unique, so the compiler doesn’t warn about exporting them?