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
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
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...
New
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
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
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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...
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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









