Fl4m3Ph03n1x
Fix definitions with multiple clauses and optional values that only have 1 param
I have the following code in my GenServer:
def init(%State{} = state), do: {:ok, state, 1_000}
def init(opts \\ []) do
state = struct(State, opts)
{:ok, state, 1_000}
end
This code, as some of you may guess will generate the following warning:
definitions with multiple clauses and default values require a header. Instead of:
def foo(:first_clause, b \\ :default) do ... end def foo(:second_clause, b) do ... endone should write:
def foo(a, b \\ :default) def foo(:first_clause, b) do ... end def foo(:second_clause, b) do ... enddef init/1 has multiple clauses and defines defaults in one or more clauses
The warning is pretty good, but I don’t see how I can apply it. Mainly because init can be called in 3 completely different ways:
- passing a state
- with no arguments
- passing an options keyword list
How can I fix this?
Marked As Solved
LostKobrakai
The problem is that it’s elixir creating multiple function heads for you here:
def init(), do: init([])
def init(%State{} = state), do: …
def init(opts), do: …
So it doesn’t really care about your patter match at all. It just needs to know that the first parameter is optional and shall be passed [] if not present. You could do:
def init(state_or_opts \\ [])
def init(%State{} = state), do: {:ok, state, 1_000}
def init(opts) do
state = struct(State, opts)
{:ok, state, 1_000}
end
4
Popular in Questions
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
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
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
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
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
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 list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New







