shahryarjb

shahryarjb

Is there possible to find parent module in nested module?

Hi, I have this nested module:

defmodule AA do
  defmodule BB do
    defmodule CC do
      ...
    end
  end
end

now is there a way get all the parents reversely?

For example:

[AA.BB.CC, AA.BB, AA]

It should noted, maybe the user named a module like Testy.AA, I just need to explore nested module, I mean module inside the other module. and I have always the last one module name, AA.BB.CC

Thank you

First Post!

cmo

cmo

Module.split and Module.concat may be what you’re after.

Module.split(Very.Long.Module.Name.And.Even.Longer)
["Very", "Long", "Module", "Name", "And", "Even", "Longer"]
Module.split("Elixir.String.Chars")
["String", "Chars"]

Most Liked

tomekowal

tomekowal

I’d do it this way:

defmodule MyModuleHelpers do
  def parent(module) do
    module # e.g. Very.Long.Module
    |> Module.split() # ["Very", "Long", "Module"]
    |> remove_last()
    |> Module.concat() # opposite to split Very.Long
  end

  # Elixir modules are atoms underneath that always start with "Elixir"
  # e.g. :"Elixir.Very" is pritned in console as Very
  # We use the `:Elixir` atom as recursion end
  # and we say that :Elixir does not have parents
  def parents(:Elixir), do: []

  # here is the recursive clause
  # the returned list starts with current module
  # and then calls itself recursively with parent
  # we use the fact that list constructor is itself recursive
  # [Very.Long.Module, Very.Long, Very] == [Very.Long.Module | [Very.Long | [Very | []]]]
  def parents(module) do
    [module | parents(parent(module))]
  end

  # there is no "remove last element" function, so the trick is to reverse and remove first
  defp remove_last(list) do
    list # ["Very", "Long", "Module"]
    |> Enum.reverse() # ["Module", "Long", "Very"]
    |> tl() # tail function gives everything BUT the first element ["Long", "Very"]
    |> Enum.reverse() # ["Very", "Long"]
  end
end
codeanpeace

codeanpeace

In case it gives you more ideas, there’s a Module.__info__/1 as well.

iex(5)> AA.BB.CC.__info__(:module)
AA.BB.CC

It’s also worth noting that modules aren’t truly nested in Elixir since they’re all flattened into a top level if I recall correctly. The superficial “nesting” via the dot/period is just a namespacing convention.

al2o3cr

al2o3cr

There’s not a good way, and IMO that’s intentional - the apparent hierarchy of dotted names (Foo.Bar.Baz) doesn’t correspond to any real nesting.

For instance, naming a module Foo.Bar.Baz says precisely nothing about the existence of modules named Foo and Foo.Bar. They could exist, but they aren’t required to like in other languages (for instance, Ruby).

There’s no way to go in the opposite direction either: given a module named Foo.Bar, there’s no API to retrieve every module named Foo.Bar.<whatever>.

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement