nhpip

nhpip

How do you work with nested modules?

So what’s the idiomatically correct way to do nested modules? I know when applications you tend to do it the first way and follow the directory hierarchy, but when to choose one over the other because I do see it done both ways? The only time I do it the second way is if all I need are a bunch of structs.

defmodule Foo do
   ...
end

then

defmodule Foo.Bar do
    ...
end

VS

defmodule Foo do
 
    defmodule Foo.Bar do
        ...
    end
    ....
end

Most Liked

voughtdq

voughtdq

The only time I ever nest in the file is if it’s for something like state in a GenServer that will only be used internally by that GenServer.

defmodule SomeGenServer do
  use GenServer

  defmodule State do
    # Note that this is actually SomeGenServer.State
    defstruct [x: "", y: "", z: ""]
  end

  @impl true
  def init(_), do: {:ok, %State{}}

  @impl true
  def handle_call(:x_the_x, _from, state) do
    %{x: x} = state
    x = x_the_x(x)
    {:reply, x, %{state | x: x}}
  end

  def x_the_x(x) do
    to_string(x) <> to_string([Enum.random(?A .. ?z)])
  end
end

Otherwise, I define each module in its own file and have the directory structure match that nesting.

tomkonidas

tomkonidas

I would say to let each module have its own file. And to follow the directory tree structure.
It makes it easier to find what you need later on.

voughtdq

voughtdq

I’ve done it before, but it doesn’t feel as explicit. It’s kind of magic for magic’s sake. Thinking back to when I first started using Elixir, I would have been very confused by this because they appear as two separate “declarations”. So I would be looking for what State was and ask, “why is it just referring back to the module?” not realizing that the module itself is the struct. On the contrary, defining it as a nested module gives the beginner a clear understanding of where %State{} actually came from.

Last Post!

ityonemo

ityonemo

Of course, but it will raise compileError if your struct field names drift.

Where Next?

Popular in Questions Top

New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement