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

rekkice
I’m building an editor integration to evaluate Elixir code in an IEx session. While Code.eval_string/3 allows tracking variable bindings,...
New
manhvu
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 su...
New
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
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
rhcarvalho
Hi all, I would like to gather some feedback before a more intentional proposal to add a new :depth option when specifying a Git depende...
New
mikesax
I was writing code that included a function I would call later (so it wasn’t called yet). I got a warning, and I thought I could put an u...
New
dli
Ecto currently supports some data-modifying WITH statements / CTEs for Postgres: Options: […] :operation - one of :all , :update_all ,...
New
ffloyd
The Problem Currently, if I define a struct in the following way: defmodule MyStruct do # Both x and y will have the FIXED values unti...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
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

Other popular topics Top

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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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