alishir

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)

First 4 of 4 Posts Switch mode

Qqwy

Qqwy

TypeCheck Core Team

The problem is that to compile Cycle, you need Cycle.Handle.
Cycle.Handle does not really depends on Cycle. 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

LostKobrakai

Cycle depends on Cycle.Handle at compile time (struct usage requires keys to be known), but Cycle.Handle cannot be compliled without Cycle being compiled. So you’re in a cyclic dependency. You’ll either need to use different files or put Cycle.Handle’s definition before and outside of Cycle’s definition.

Nicd

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

alishir

alishir OP

I thank all of you @Qqwy, @LostKobrakai and @Nicd.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
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
roeland
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 Top

ancatrusca
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
JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
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
ausimian
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
type1fool
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

We're in Beta

About us Mission Statement