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

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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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 39247 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement