Enuzo
Order of precedence in string matching
Hello there,
I know that:
str = "_forum"
"elixir" <> str
"elixir_forum"
But the following fails:
"elixir" <> str = "_forum"
** (MatchError) no match of right hand side value: "_forum"
And the following works:
iex(1)> "elixir" <> str = "elixir_forum"
"elixir_forum"
iex(2)> str
"_forum"
This is making my head spin. Please, can someone help me with a logical breakdown of how this matching, binding, and concatenation operation works and the rationale behind the ordering?
Most Liked
kip
ex_cldr Core Team
The key is to recognise that = is really a match operator, with the side affect of binding the variable.
Therefore:
str = "_forum"matches and binds. There is no pattern so anything will bind."elixir" <> strhas no pattern matching (there’s no left and right), its just a call to the functionKernel.<>/2with the parameters"elixir"andstr"elixir" <> str = "_forum"is a pattern match (there is a left and right around thematchoperator=. It can’t match because"_forum"cannot match"elixir" <> str"elixir" <> str = "elixir_forum"matches just fine as I think you may have worked out by now
Hope that’s sufficiently clear, if not ask away
7
kip
ex_cldr Core Team
Not really analagous, but but they are both matches.
"elixir" <> str = "elixir_forum"is a binary match (thats indicated by the<>). Basically it can be read as "match a string that starts withelixirand bind anything that comes after that tostr.strend up being bound to"_forum"["elixir", str] = ["elixir", "forum"]is asking the question: are both sides of the match operator (the=) of the same shape? In this case, yes - they are both lists with 2 elements. Then do the elements match? For the first element, yes,"elixir"=="elixir. And the second element binds"forum"tostr.
3
hauleth
"elixir" <> str = "elixir_forum"
Is exactly the same as:
<<"elixir", str::binary>> = "elixir_forum"
3
Popular in Questions
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I have a super simple question about elixir - how would I take a file like this
foo
bar
baz
and output a new file that enumerates th...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








