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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
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!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
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
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
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
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
Other Trending Topics
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 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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
- #deployment
- #library
- #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
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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