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

ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement