alishir
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)
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Other Trending Topics
New BEAM There, Done That with Maxim Kharchenko, creator of Ling - not a BEAM fork, a clean Erlang VM implementation running on Xen with ...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 4 of 4 Posts
Qqwy
The problem is that to compile
Cycle, you needCycle.Handle.Cycle.Handledoes not really depends onCycle. However, since both are in the same file, it still ‘sort of’ depends on it.And as a result, the compiler is unable to continue.
The solution is to move the nested module to a separate file.
LostKobrakai
Cycledepends onCycle.Handleat compile time (struct usage requires keys to be known), butCycle.Handlecannot be compliled withoutCyclebeing compiled. So you’re in a cyclic dependency. You’ll either need to use different files or putCycle.Handle’s definition before and outside ofCycle’s definition.Nicd
We don’t need to be that drastic in fact, we can just move the
Handledefinition higher up in the file. This will work: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
alishir
I thank all of you @Qqwy, @LostKobrakai and @Nicd.