hokie81

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

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.

Where Next?

Popular in Questions Top

shahryarjb
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
aalberti333
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
Harrisonl
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
dokuzbir
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
ashish173
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
chewm
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
hpopp
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
belgoros
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
romenigld
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 Top

dotdotdotPaul
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
openscript
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
aalberti333
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
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...
548 27727 240
New
rms.mrcs
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
ovidiubadita
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement