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’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
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
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
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
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
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
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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!Also Liked
Eiji
Weird… when I try this code:
then everything is working without any problem.
moduleis print properly and there are no warnings or errors …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
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
Last Post!
Eiji
It’s not a part of public
APIand 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.exfile …There was some talk about this issue … I think it was about
Elixirscripts i.e.exsfiles called likeelixir my_script.exs. There was a question I think aboutecto’s macros not working on same level where module is defined, but working inside some function … Somebody fromElixir’s core team (most probablyJosé Valim) commented it and that it’s known limitation.