3 ms·
This will work with both maps and keywords: iex(1)> foo = %{a: 1, b: "test"} %{a: 1, b: "test"} iex(2)> foo[:bar] || :some_default_value :some_
by nivertech 5y ago
This will work with both maps and keywords:
iex(1)> foo = %{a: 1, b: "test"}
%{a: 1, b: "test"}
iex(2)> foo[:bar] || :some_default_value
:some_default_value
iex(3)> foo = [a: 1, b: "test"]
[a: 1, b: "test"]
iex(4)> foo[:bar] || :some_default_value
:some_default_value
This looks like some Perl-ism/Ruby-ism.
I personally dislike truth-y/false-y values, as they do more harm than good, and making it harder for dialyzer to infer boolean types.
Also Map.get/3 and Keyword.get/3 are probably faster.