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
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Weird… when I try this code:
then everything is working without any problem.
moduleis print properly and there are no warnings or errors …shahryarjb
Hello @Eiji, you could not be able to create something
Opp.sample()?Eiji
Ah, I see … You have tried my example to create a top-level module in another module.
First of all you need to understand something … As same as you can check
ASTwithquote do … endas same you can check how modules are “defined” inside usingIO.puts(module).With this you should understand why
sub_field :Opp do … enddoes not generatesOppmodule. However it’s easy to do so. Simply addElixir.prefix instead of:, so:You can also do that within macro using for example Module.concat/2
shahryarjb
Thank you for the time you explain this, but I need to convert atom inside
sub_fieldmacro, and user just put anatomCode:
And
Error
I think it just accept a Tuple for creating submodule
If I put
sub_field(Oop, String.t(), enforce: true) do, it worksI am improving my macro
zachallaun
On my phone, so apologies if some of this is incorrect as I can’t test it, but I’d try the following:
I’d really need to test to be sure, and it can be a little tricky to follow with the nested unquote/bind_quotes bit, but essentially you need to access
__MODULE__in the calling module.Eiji
Take a look at this code for inspiration:
Note: I have tried
Module.split/1, but it does not support non-Elixir modules (those havingElixir.prefix when changing toString).Edit: I have forgot about
Macro.camelize/1, so I have updated my code. Thanks @zachallaun!zachallaun
Credit where it’s due: I just copied it from @shahryarjb’s post above mine!
Eiji
right, my bad
shahryarjb
I have this eror for this:
shahryarjb
Yes this place we can concat the module!! but when I use this inside my macro it does not work and has error.
Error