alishir
Cyclic module usage error
Hi all, I have the following simple snippet but I got cyclic module usage error. Do you know how should I resolve the error?
defmodule Cycle do
alias Cycle.Handle
def hello(%Handle{resource: _resource}) do
:world
end
defmodule Handle do
defstruct resource: nil, reference: nil
def wrap_resource(resource) do
%__MODULE__{
resource: resource,
reference: make_ref()
}
end
end
end
The error is:
== Compilation error in file lib/cycle.ex ==
** (CompileError) lib/cycle.ex:16: Cycle.Handle.__struct__/0 is undefined, cannot expand struct Cycle.Handle. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
lib/cycle.ex:16: (module)
Most Liked Responses
Nicd
We don’t need to be that drastic in fact, we can just move the Handle definition higher up in the file. This will work:
defmodule Cycle do
defmodule Handle do
defstruct resource: nil, reference: nil
def wrap_resource(resource) do
%__MODULE__{
resource: resource,
reference: make_ref()
}
end
end
def hello(%Handle{resource: _resource}) do
:world
end
end
Note that the alias is also not needed.
I use this style quite often with my GenServers to define Options and State structs, like this example: lib/geo_therminator/pump_api/device/server.ex · 8fa72cbfa5b0402c46c53e13e5e994bf5a390f89 · Mikko Ahlroth / GeoTherminator · GitLab
3
Popular in Questions
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
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
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
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
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New








