rm-rf-etc

rm-rf-etc

Did we get private modules?

I see there was a long discussion on the proposal of private modules, but I can’t tell what decision was reached. I’m writing a library which has an internal-only __using__/1 macro, and I want to know how I can make it private to the library.

Marked As Solved

mudasobwa

mudasobwa

Creator of Cure

To hard-restrict calling macros from allowed places only, one might use __CALLER__ special form inside macro.

@allowed_callers Foo.Bar, Foo.Baz
defmacro any_macro do
  raise unless __CALLER__.module in @allowed_callers
  ...
end

Also Liked

mudasobwa

mudasobwa

Creator of Cure

There is no such thing as “submodule” or “child module” in Elixir. Foo.Bar, despite it shares the first part of the name with Foo is absolutely not related to Foo by any means.

If you meant “to check whether a module shares a namespace,” that’d be easy to achieve with Module.split/1 and List.starts_with?/2, like

{m1, m2} = {Foo.Bar.Baz, Foo.Bar}

[m2, m1] |> Enum.map(&Module.split/1) |> Enum.reduce(&List.starts_with?/2)
#⇒ true
[m1, m2] |> Enum.map(&Module.split/1) |> Enum.reduce(&List.starts_with?/2)     
#⇒ false
Nicd

Nicd

There are no private modules, only private functions. It might be possible to do something of the sort with macro / metaprogramming trickery, but there is no standard feature for it. There is a library called Boundary that provides some guarantees to prevent using modules you shouldn’t.

kokolegorille

kokolegorille

I made the change to the topic and set the solution as requested.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement