[Proposal] Module.define/3

35 views
Skip to first unread message

Michał Muskała

unread,
Jun 17, 2017, 10:27:02 AM6/17/17
to elixir-l...@googlegroups.com
Hello everybody.

Another day, another proposal 😅

The problem: 
A lot of macros, when called, need to define a function - be it a private, helper function, or a final public function. This is very easy, when the macro is top-level (called directly in the module body). Unfortunately it's much harder to do, when the macro is called inside a function. 
Currently, there are two solutions:
1. Register a before_compile hook and accumulate data in some module attribute
2. Use Module.eval_quoted.

Both solutions are rather ugly, possibly slow (especially in case of eval) and can lead to a very hard-to-debug code.

Solution:
I propose adding a Macro.define/3 function, that would allow to add a function or macro to a module under compilation, from any place. This way, the solution is obvious, does not require using any kind of eval and is local (unlike the module attribute solution proposed above).

Michał.

José Valim

unread,
Jun 17, 2017, 10:50:14 AM6/17/17
to elixir-l...@googlegroups.com
Can you provide some actual code samples of before and after? Because while I don't have anything against Module.define/3, I am not sure it would actually be any better.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/599467ac-4032-4ce0-a230-2b2c1265c292%40Spark.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages