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.

rm-rf-etc

rm-rf-etc

Love this solution. I would mark it as the solution in this thread but I can’t apparently because of the discussion type I chose.

One question, is there a way to check if __CALLER__.module is a submodule or child module of another module – i.e. ModA.ModB is child of ModA?

Where Next?

Popular in Questions 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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
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
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement