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
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
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
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
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
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
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
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
- #metaprogramming
- #hex
- #security











Showing Posts 15 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.shahryarjb
Hi again
, 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:
The error:
But if I move the module out of test macro, it works like:
If i put the output of
__struct__inside a variable, it print the output but it shows me this warningEiji
__CALLER__looks good for me and there should not be any problem. As always just an example to check what you worry about:The above code prints
A.B.C.shahryarjb
Ahh, Thank you yes you are right
but I think it just supports one level nested, because I use
__CALLER__I think it does not support
so I wont be able to fix it I create another post for it. thank you
Eiji
vs what your code do:
:oop→"oop"→"Oop"→Elixir.OopSomething in your code tries to reference this long module where it should
Oopor you should do other concatenation (not withElixir, but for example a parent module i.e.__MODULE__). Since I have shared small example I have concatenated said module withExamplemodule.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
shahryarjb
I have this eror for this:
Eiji
right, my bad
zachallaun
Credit where it’s due: I just copied it from @shahryarjb’s post above mine!
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!