3 ms·
While not integer/number specific I like the idea of Guards in Elixir: def sum(a, b) when is_integer(a) and is_integer(b) do a + b end def s
by andy_ppp 11y ago
While not integer/number specific I like the idea of Guards in Elixir:
def sum(a, b) when is_integer(a) and is_integer(b) do
a + b
end
def sum(a, b) when is_list(a) and is_list(b) do
a ++ b
end
def sum(a, b) when is_binary(a) and is_binary(b) do
a <> b
end
sum 1, 2
#=> 3
sum [1], [2]
#=> [1,2]
sum "a", "b"
#=> "ab"
- lifeisstillgood 11y agoThat's it - going to set aside some time for getting Elixr on my bare competence skill set. I love that
- arethuza 11y agoThat's pretty nice - like CLOS multiple dispatch with arbitrary predicates rather than types. Are there any restriction on what the code in the guards can do?
- lectrick 11y agoYes. But the guard code are just macros, so... ;) But I do believe that Erlang underneath does have actual limits.