hokie81
Differing ways to specify default for Elixir case statement - are these the same?
(pretty new to Elixir here…)
sometimes I see:
case [...] do
{:ok, [...]} -> [...]
# using _ as default
{_, other} -> {:error, other}
end
other times:
case [...] do
{:ok, [...]} -> [...]
# not using _ as default
other -> {:error, other}
end
and other times:
case [...] do
{:ok, [...]} -> [...]
# using _xxx as default
_other -> :ok
end
Are these the same thing? If not what are the differences between them and considerations for use of one over the other?
Tyia.
Most Liked
thomas.fortes
Imagine that you have a value response that could be a few things:
response = {:ok, result}
response = {:error, reason}
response = {:invalid, :other, :fields}
Then the cases would be:
# This would work for the first two cases returning result in the first one,
# {:error, other} for the second one and raise an exception for the third one (no match at all).
case response do
{:ok, result} -> result
{_, other} -> {:error, other}
end
# This would work for all the cases and return result for the first case,
# {:error, other} for the second case and
# {:error, {:invalid, :other, :fields}} for the third one
case response do
{:ok, result} -> result
other -> {:error, other}
end
# This would return result for the first case and :ok for any other response.
case response do
{:ok, result} -> result
_other -> :ok
end
Usually you will match a value that comes from a function and the clauses of the match will depend of the format of the result of that function.
2
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
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
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
I would like to know what is the best IDE for elixir development?
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
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
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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







