ycv005

ycv005

How to assignment in the case condition?

How to assign a variable to a value in the case condition. Is the following way correct? If a better solution exists, let me know. Your help will be highly appreciable.

    case stage_id do
      "" ->
        stage_id = JobStage.get_job_stage(job_opening_id, 1).id
    end

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
stage_id = case stage_id do
  "" -> JobStage.get_job_stage(job_opening_id, 1).id
  _ -> stage_id 
end
NobbZ

NobbZ

I’m currently running 1.8.2-otp-21 and see this behaviour:

iex(1)> defmodule M do
...(1)> def f do
...(1)>   x = :outer
...(1)>   case "" do
...(1)>     "" ->
...(1)>       x = :inner
...(1)>   end
...(1)>   IO.puts x
...(1)> end
...(1)> end
warning: variable "x" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:6
iex(2)> M.f
outer
:ok 

As you can see, I see a warning about the “inner” x not beeing used, but I do not see a warning about the imperativ assignment, instead “outer” x stays unchanged.

peerreynders

peerreynders

It would likely generate the following warning:

Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable “stage_id”, you will have to explicitly return the variable.

Last Post!

ycv005

ycv005

Thanks to all.

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31771 143
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
dblack
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
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

We're in Beta

About us Mission Statement