shahryarjb

shahryarjb

Hello friends,
I want to create a module inside another module with an atom name:

For example:

  defmacro sub_field(name, _type, opts \\ [], do: block) do
    ast = register_struct(block, opts)

    quote do
      defmodule unquote(name) do
        unquote(ast)
      end
    end
  end

If I use Module name, it is okey and creates a sub module for me like:

sub_field(Oop, String.t(), enforce: true) do
    field(:title, String.t())
    field(:fam, String.t())
end

But If I replace Oop with :opp it has invalid module name error.

I tried to convert atom to string and use Macro.camelize() and convert it to atom again ( :Oop), but it does not accept.

Then I print :opp, it just returns :opp in my macro but if I put module name Opp it returns something like this:

{:__aliases__,
 [
   counter: {MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct, 3},
   line: 520
 ], [:Opp]}

now how can use atom to do this?

Thank you in advance

Showing Posts 15 to 6

Eiji

Eiji

It’s not a part of public API and therefore no answer guarantees you a working solution using it. Why cant you use %module{…} notation?

First of all why you do this? Much more common is to place it in test/fixtures/my_fixture.ex file …

There was some talk about this issue … I think it was about Elixir scripts i.e. exs files called like elixir my_script.exs. There was a question I think about ecto’s macros not working on same level where module is defined, but working inside some function … Somebody from Elixir’s core team (most probably José Valim) commented it and that it’s known limitation.

shahryarjb

shahryarjb OP

Hi again :rose: , I have a question, when I move my module and macro inside a test, it does not work, I think inside test it takes a time to compile and the functions are called faster than the macro

for example:

  test "nested macro field" do
    defmodule TestNestedStruct do
      use GuardedStruct

      guardedstruct do
        field(:title, String.t())
        field(:subject, String.t())

        sub_field(:oop, String.t(), enforce: true) do
          field(:title, String.t())
          field(:fam, String.t())

          sub_field(:soos, String.t(), enforce: true) do
            field(:fam, String.t())
          end
        end

        field(:site, String.t())
      end
    end

    IO.inspect(TestNestedStruct.__struct__())
    IO.inspect(TestNestedStruct.Oop.__struct__())
    IO.inspect(TestNestedStruct.Oop.__info__(:functions))
    IO.inspect(TestNestedStruct.Oop.Soos.__struct__())
    IO.inspect(TestNestedStruct.Oop.Soos.__info__(:functions))
    IO.inspect(TestNestedStruct.keys())

    assert %TestNestedStruct.Oop{
             fam: nil,
             title: nil
           } = TestNestedStruct.Oop.__struct__()
  end

The error:

error: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.__struct__/0 is undefined, cannot expand struct MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop. 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
  test/guarded_struct_test.exs:542: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1

But if I move the module out of test macro, it works like:

  defmodule TestNestedStruct do
    use GuardedStruct

    guardedstruct do
      field(:title, String.t())
      field(:subject, String.t())

      sub_field(:oop, String.t(), enforce: true) do
        field(:title, String.t())
        field(:fam, String.t())

        sub_field(:soos, String.t(), enforce: true) do
          field(:fam, String.t())
        end
      end

      field(:site, String.t())
    end
  end

  test "nested macro field" do
    IO.inspect(TestNestedStruct.__struct__())
    IO.inspect(TestNestedStruct.Oop.__struct__())
    IO.inspect(TestNestedStruct.Oop.__info__(:functions))
    IO.inspect(TestNestedStruct.Oop.Soos.__struct__())
    IO.inspect(TestNestedStruct.Oop.Soos.__info__(:functions))
    IO.inspect(TestNestedStruct.keys())

    assert %TestNestedStruct.Oop{
             fam: nil,
             title: nil
           } = TestNestedStruct.Oop.__struct__()
  end

If i put the output of __struct__ inside a variable, it print the output but it shows me this warning

....warning: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.__struct__/0 is undefined (module MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop is not available or is yet to be defined)
  test/guarded_struct_test.exs:541: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1
Eiji

Eiji

__CALLER__ looks good for me and there should not be any problem. As always just an example to check what you worry about:

defmodule MyLib do
  defmacro my_macro(name, do: block) do
    module =
      name
      |> Atom.to_string()
      |> Macro.camelize()
      |> String.to_atom()
      |> then(&Module.concat(__CALLER__.module, &1))

    quote do
      defmodule unquote(module) do
        _ = unquote(block)
      end
    end
  end
end

defmodule A do
  import MyLib

  my_macro :b do
    my_macro :c do
      IO.inspect(__MODULE__)
    end
  end
end

The above code prints A.B.C. :+1:

shahryarjb

shahryarjb OP

Ahh, Thank you yes you are right

  defmacro sub_field(name, _type, opts \\ [], do: block) do
    ast = register_struct(block, opts)

    name =
      name
      |> Atom.to_string()
      |> Macro.camelize()
      |> String.to_atom()

    %Macro.Env{module: mod} = __CALLER__
    module_name = Module.concat(mod, name)

    quote do
      defmodule unquote(module_name) do
        unquote(ast)
      end
    end
  end

but I think it just supports one level nested, because I use __CALLER__

I think it does not support

  defmodule TestNestedStruct do
    use GuardedStruct

    guardedstruct do
      field(:title, String.t())
      field(:subject, String.t())

      sub_field(:oop, String.t(), enforce: true) do
        field(:title, String.t())
        field(:fam, String.t())
        sub_field(:sop, String.t(), enforce: true) do
          field(:title, String.t())
        end
      end

      field(:site, String.t())
    end
  end

so I wont be able to fix it I create another post for it. thank you

Eiji

Eiji

vs what your code do:
:oop"oop""Oop"Elixir.Oop

Something in your code tries to reference this long module where it should Oop or you should do other concatenation (not with Elixir, but for example a parent module i.e. __MODULE__). Since I have shared small example I have concatenated said module with Example module.

shahryarjb

shahryarjb OP

Yes this place we can concat the module!! but when I use this inside my macro it does not work and has error.

  defmacro sub_field(name, _type, opts \\ [], do: block) do
    ast = register_struct(block, opts)

    name =
      name
      |> Atom.to_string()
      |> Macro.camelize()
      |> String.to_atom()

    module_name = Module.concat(Elixir, name)

    quote do
      defmodule unquote(module_name) do
        unquote(ast)
      end
    end
  end

Error

➜  mishka_developer_tools git:(master) ✗ mix test
Compiling 1 file (.ex)
error: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop.__struct__/0 is undefined, cannot expand struct MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct.Oop. 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
  test/guarded_struct_test.exs:534: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1


== Compilation error in file test/guarded_struct_test.exs ==
** (CompileError) test/guarded_struct_test.exs: cannot compile module MishkaDeveloperToolsTest.GuardedStructTest (errors have been logged)
    (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3
    (stdlib 5.0.2) lists.erl:1706: :lists.mapfoldl_1/3
    (ex_unit 1.15.1) expanding macro: ExUnit.Assertions.assert/1
    test/guarded_struct_test.exs:534: MishkaDeveloperToolsTest.GuardedStructTest."test nested macro field"/1
shahryarjb

shahryarjb OP

I have this eror for this:

error: unquote called outside quote
  test/guarded_struct_test.exs:521: MishkaDeveloperToolsTest.GuardedStructTest.TestNestedStruct (module)
Eiji

Eiji

right, my bad :sweat_smile:

zachallaun

zachallaun

Credit where it’s due: I just copied it from @shahryarjb’s post above mine!

Eiji

Eiji

Take a look at this code for inspiration:

defmodule Example do
  def sample(module) when is_atom(module) do
    module |> Atom.to_string() |> Macro.camelize() |> String.to_atom()
  end
end

module = Example

[Opp, :Opp, :opp]
|> Enum.map(&Example.sample/1)
|> Enum.map(&Module.concat(module, &1))
|> IO.inspect()

# returns: [Example.Opp, Example.Opp, Example.Opp]

Note: I have tried Module.split/1, but it does not support non-Elixir modules (those having Elixir. prefix when changing to String).

Edit: I have forgot about Macro.camelize/1, so I have updated my code. Thanks @zachallaun!

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
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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

Other Trending Topics Top

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

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews