Function guard with comma

Hi all

When I comparing function between erlang and elixir I feel erlang looks more natural.
I think, it would be nice to have comma instead of and.

def sum(a, b) when is_integer(a), is_integer(b) do
a + b
end

What is your opinion?

Thanks

The problem is that a comma meaning of and feels rather arbitrary - why and for a comma and not or? For someone not familiar with Erlang it could be confusing.

3 Likes

For example you have three parameters, that you want to check, if it fits your needs.
So for every guard, you have to write the and statement instead of comma.

For saving space I would’ve understood the suggestion of “&” or the like, but “,” doesn’t communicate the same thing nowadays as it may have done when Erlang was created. On top of that, I would assume they only used it because of the Prolog influence.

I think Erlang’s function definitions would’ve been better if they had one version that worked for most/all cases instead of the ones that exist: “and” / “andalso” / “,”… It’s also the kind of priority I think any modern language needs; making one correct, real choice instead of three half-choices.

1 Like

and and andalso are semantically different and not equivalent. In a guard they are basically equivalent. However, in a guard or (or orelse) and ; are semantically different so you need them both.

1 Like