manhvu

manhvu

Proposal: Add support namespace for module

In a large repo, working with module need to add alias too much is quite annoyed and not good for organizing code.

I think better add support for namespace can bring more convenient for dev.

This is a basic example for namespace cross multi modules.

Example:

defmodule MyApp.A do
  namespace MyApp.DataProcessing

  def a do
  # do somethings
  end

  defmacro macroA(name, opts) do
    #...
  end
end

defmodule MyApp.B do
 namespace MyApp.DataProcessing

 def b do
  # somethings
 end
end

defmodule MyApp.C do
  namespace MyApp.DataProcessing

  def do_something(data) do
    data
    |> A.a()
    |> B.b()
  end

 A.macroA :wrapper, type: :json
end

LS can easily to suggest dev in a namespace.

Pros:

  • Less typo like add alias for module.
  • Make code clean than before.
  • Better for LS suggest in case work in namespace.

Cons:

  • Make complicated if bring support for import, require or use.
  • Can make dev confused.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This idea is essentially implemented in the library Boundary GitHub - sasa1977/boundary: Manage and restrain cross-module dependencies in Elixir projects

Do you have any thoughts on how this proposal differs, beyond basically the implicit aliasing?

FlyingNoodle

FlyingNoodle

Please don’t. I know this isn’t a democracy but I vote against it.

BartOtten

BartOtten

At mobile so in short:

defmodule DataProcessingAliases do
  defmacro __using__(opts) do
    quote do
       alias MyApp.A
       alias MyApp.B
       alias MyApp.C
     end
   end
end

Now you can use DataProcessingAliases in the modules. Mixed ground between explicit and implicit…?

BartOtten

BartOtten

What would you gain with that? Imho at maximum a few lines less at the cost of macro in macro and a separate config.

Notice you can add multiple namespaces in a single file. So the example I gave can be copied over and over in a ‘config’ file.

As a benefit of the ‘manual macro’ solution you could also add common aliases not in the same ‘namespace’ such as Ecto.Repo. Enforce no aliases in the modules themselves and you get a boundary overview of all namespaces in a single file: every ‘out of pattern’ alias sticks out like a sore thumb.

defmodule Namespace do
  defmacro __using__(namespace) do
     Namespace.get(namespace)
   end

 def get(MyApp.Datastore) do
    quote do
       alias MyApp.DataStore.A
       alias MyApp.DataStore.B
       alias MyApp.DataStore.C
       alias MyApp.OtherMod.A # crossing boundaries
       alias Ecto.Repo
     end
  end

   def get(MyApp.OtherMod) do
      ….
    end
end

# usage

use Namespace, MyApp.DataStore
BartOtten

BartOtten

Ah, forgot about that. Need to expand it before passing it as function argument. Small change but then you can just use module names.

Where Next?

Popular in Proposals: Ideas Top

kip
Sumary of proposal DateTime.from_iso8601/3 adjusted to: set all numeric fields to the values as parsed (not shifted to UTC), preservi...
New
maxpohlmann
In our application, we have many structs that contain lists of floats and, especially in test, we often use pattern matching on these obj...
New
Asd
Process.resolve/1 What it does Takes any possible name of the process and returns a pid of the process or port (or nil if none found). Si...
New
MeerKatDev
many times we do stuff like (e.g. test setups, json views in phoenix) aaa = ... bbb = ... ccc = ... %{aaa: aaa, bbb: bbb, ccc: ccc} and...
New
cevado
Being able to build nested relations in a schemaless changeset would be helpful to deal with complex forms on Phoenix without the need to...
New
markevans
Hi! I’m excited about everything that’s going on re. gradual typing and am really pleased to see that Jose and the team seem to be think...
New
sodapopcan
So after complaining about this for the third or fourth time on this forum, I figured I should make a proposal. TL;DR with can be hard t...
New
nhpip
So the other day I was rearranging the supervisor hierarchy of our product and I had a thought. In addition to creating a child spec with...
New
cevado
IEx is a very powerfull shell and it would be awesome to have all this power integrated inside a code editor. Clojure enables something l...
New
cheerfulstoic
I feel like Elixir is getting big enough and old enough that I’m starting to experience problems with conflicting dependencies. An examp...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement