Papillon6814
Handling nil in case
Hello. I’m thinking the way to handle nil in case.
|> case do
x when not is_nil(x) ->
...
{:ok, "xxxxxx"}
nil ->
...
{:error, "xxxxxxxx"}
end
|> case do
nil ->
...
{:error, "xxxxxxxx"}
topic ->
...
{:ok, "xxxxxxxx"}
end
When I handle nil in case, I write codes in a latter way. What do you think of that?
Most Liked
dimitarvp
In case, I always go from most concrete to least concrete, like so:
case something do
nil -> "nil"
:a -> "the atom :a"
text when is_binary(text) -> "text"
list when is_list(list) -> "list"
other -> raise(ArgumentError, "unexpected value: #{inspect(other)}"
end
5
kokolegorille
You can also do like this, if topic is some kind of struct.
|> case do
%Topic{} = topic -> ...
nil -> ...
end
1
dmitrykleymenov
About piping into case a good mention was in Good and Bad Elixir post. May be you should consider to match it to some variable? Especially with nil result possibility, you could rewrite it to if some_result, do: something, else: another
1
dmitrykleymenov
At least that is said in the article
If you think it makes sence, yeah, you may assign intermediate result into variable and use it in case explicit. But it’s hard to tell without upstream code.
1
Popular in Questions
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
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
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
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
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New







