I am writing a function that will push a value to a list on a map if the key already exists, or init the key on the map with a new list (containing the pushed element) if it does not already exist.
I tried making this part of the function guard expression but adding this guard expression to the function was illegal. I then tried making this an if but once again the guard expression was illegal. Finally I used a case which was fine.
I believe I’ve read that only a handful of specific BIFs are allowed in function guard expressions, but why is this BIF in the if statement also illegal?
If:
map_push(M, R, V) ->
if
maps:is_key(R, M) == true -> #{R := Row} = M, M#{R := [V|Row]};
maps:is_key(R, M) == false -> #{R => [V]}
end.
---
illegal guard expression
% 42| maps:is_key(R, M) == false -> #{R => [V]}
% | ^
Case:
map_push(M, R, V) ->
case maps:is_key(R, M) of
true -> #{R := Row} = M, M#{R := [V|Row]};
false -> #{R => [V]}
end.
I think the case is preferred here anyway, but I’d like to understand why this illegal in the if.