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 Post!

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.

Most Liked

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

Last Post!

alishir

alishir OP

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

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
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
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
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
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
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
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
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
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