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
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
7
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.
2
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.
1
Last Post!
ycv005
Popular in Discussions
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
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
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
Phoenix Live View is now publicly available on GitHub.
Here’s Chris McCord’s tweet announcing making it public.
New
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
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
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
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









