Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: "extending" an existing module with new functions

Tags:

erlang

I'm currently writing some functions that are related to lists that I could possibly be reused.

My question is:

Are there any conventions or best practices for organizing such functions?

To frame this question, I would ideally like to "extend" the existing lists module such that I'm calling my new function the following way: lists:my_funcion(). At the moment I have lists_extensions:my_function(). Is there anyway to do this?

I read about erlang packages and that they are essentially namespaces in Erlang. Is it possible to define a new namespace for Lists with new Lists functions?

Note that I'm not looking to fork and change the standard lists module, but to find a way to define new functions in a new module also called Lists, but avoid the consequent naming collisions by using some kind namespacing scheme.

Any advice or references would be appreciated.

Cheers.

like image 536
stantona Avatar asked Sep 18 '25 08:09

stantona


2 Answers

To frame this question, I would ideally like to "extend" the existing lists module such that I'm calling my new function the following way: lists:my_funcion(). At the moment I have lists_extensions:my_function(). Is there anyway to do this?

No, so far as I know.

I read about erlang packages and that they are essentially namespaces in Erlang. Is it possible to define a new namespace for Lists with new Lists functions?

They are experimental and not generally used. You could have a module called lists in a different namespace, but you would have trouble calling functions from the standard module in this namespace.

like image 186
Alexey Romanov Avatar answered Sep 23 '25 12:09

Alexey Romanov


I give you reasons why not to use lists:your_function() and instead use lists_extension:your_function():

  • Generally, the Erlang/OTP Design Guidelines state that each "Application" -- libraries are also an application -- contains modules. Now you can ask the system what application did introduce a specific module? This system would break when modules are fragmented.

However, I do understand why you would want a lists:your_function/N:

  • It's easier to use for the author of your_function, because he needs the your_function(...) a lot when working with []. When another Erlang programmer -- who knows the stdlb -- reads this code, he will not know what it does. This is confusing.
  • It looks more concise than lists_extension:your_function/N. That's a matter of taste.
like image 34
Pindatjuh Avatar answered Sep 23 '25 12:09

Pindatjuh