Fl4m3Ph03n1x

Fl4m3Ph03n1x

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 ... end

one should write:

def foo(a, b \\ :default)
def foo(:first_clause, b) do ... end
def foo(:second_clause, b) do ... end

def 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:

  1. passing a state
  2. with no arguments
  3. passing an options keyword list

How can I fix this?

Showing Posts 1 to 4

idi527

idi527

:waving_hand:

Just following the suggestion in the warning, maybe:

def init(opts \\ [])
def init(%State{} = state), do: {:ok, state, 1_000}
def init(opts) do
  state = struct(State, opts)
  {:ok, state, 1_000}
end
Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

This is confusing to me. By doing it that way, I am saying that state is actually an opts, right ?

LostKobrakai

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
NobbZ

NobbZ

Looking at your code, for me it seems much more like opts beeing a listified %State{}

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews