Binding operator - is it possible to add the ‘:=’ operator for binding assignments and keep the ‘=’ operator for pattern matching

Good morning to the Elixir team.

Please excuse me if I’m commenting in the wrong place; please indicate where I should discuss this.

I would like to know if it’s possible to add the ‘:=’ operator for binding assignments and keep the ‘=’ operator for pattern matching.

This would make teaching the features of Elixir/Erlang easier (as a language promoter).

Thank you very much for your attention.

Can you give an example of what you mean?

You can already do stuff like this:

data = {:ok, %{name: "Alice"}}

case data do
    {:ok = status, user} -> 
        // here we have both status and user variable available
end

But there is no such thing as “binding assignment operator” in Elixir. There is only pattern matching (with potential rebinding). So I do not really get how these operators would differ.

6 Likes

Pattern-matching IS binding assignments, there’s no difference. An example:

{:ok, something} = MyFunc.call(params)
response = MyFunc.call(params) # Returns same, now `response` is bound as {:ok, something}

If you are a “language promoter” with the goal of teaching Elixir, you should probably spend some more time writing it, so you’re comfortable with how the language works.

3 Likes

I’m not following what’s gained by distinguishing these two uses of =. Can you provide some examples?

1 Like

Thank you for your feedback.

I asked this question because my team and I come from non-functional languages.

I am learning and really enjoying the practicality and ergonomics of Elixir and the capabilities of Erlang/OTP.

Without a doubt, I must and need to improve my understanding of the language further.

I thought that differentiating ‘Pattern matching’ from ‘binding assignments/rebinding’ would be simpler for understanding the Functional Paradigm, which is the heart of functional languages.

It was not my intention to modify the language into something meaningless.

I greatly appreciate the answers.

At first I also had a hard time accepting that there was no difference between pattern matching and assignment, especially when you’re told = is not “assign” but “match” and then get an example of foo = "bar" # "bar"

The guides have been improved a bit since I initially read them by immediately showing the “inverse” expression:

iex> x = 1
1
iex> 1 = x
1

1 = x is an brow-raiser for newbies but I think they must get some idea that it’s a special case to put a variable on the right hand side or something? I’m only guessing, but I find that showing them this last example really tends to drive it home that = really indeed not an assignment operator:

iex> 1 = 1
1

I always explain it as assignment being a side effect of matching. I know that’s sorta overloading a term, but it seems to help in the beginning, lol.

7 Likes

Think of it this way: ‘=’ is binding assignment within a pattern matching. There is no pattern matching without binding operator because it would be a useless thing to do, other than being an assertion. If you must, you can wrap a scope around it to avoid pollution:

iex(13)> x = 1
1
iex(14)> if true, do: x = 2
warning: variable "x" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  iex:14

2
iex(15)> x
1
1 Like