moacirbishopcamata

moacirbishopcamata

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.

Showing Posts 1 to 7

roflbobl

roflbobl

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
hauleth

hauleth

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.

andyleclair

andyleclair

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.

al2o3cr

al2o3cr

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

moacirbishopcamata

moacirbishopcamata OP

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.

sodapopcan

sodapopcan

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.

derek-zhou

derek-zhou

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
— All posts loaded —

Where Next? Top

Trending in Proposals: Ideas Top

woylie
We are seeing a lot of warning logs like this: navigate event to "https://someurl" failed because you are redirecting across live_sessio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews