[Proposal] Converting @type/@spec to contract/asserts at compile time?

47 views
Skip to first unread message

gasp...@gmail.com

unread,
Sep 29, 2017, 12:06:55 PM9/29/17
to elixir-lang-core
Hi all!

I increasingly find myself writing more or less exactly same logic as in @type/@spec in body of the function to check incoming arguments.

Would it be helpful if Elixir compiler could wrap function into assert check for incoming arguments and also result generated based on typespec?

I think it would be very useful for

- while running unit tests immediately see where components interaction breaks
- keeping unit tests data in sync while developing - creates more crashes when some stub/test data does not comply with changed logic in function
- running system and checking where it breaks the contracts between components in run-time/while debugging (dyalizer does a great job to extract information during static analysis phase, but runtime can provide more incorrect data :)
- encourage writing more typespecs because benefits of doing so will be visible immediately and not when (occasionally) dyalizer will be run. So make it type specification first class citizen that is not just for documenting code, but also to enforce constraints.

I personally would also love to have possibility for extended typespec that would allow also specify relation between arguments too. Like assert(arg1 + arg2 > 0).

It seems that it may be achieved mostly by rewriting def/defp macros and embedding asserts code generated from @spec around function body.

This will slow down the execution of code and should be compiler option to turn it on/off for production/dev/testing environment separately with on setting in development/testing by default.

What do you think?

/Gaspar

Louis Pilfold

unread,
Sep 30, 2017, 7:36:30 AM9/30/17
to elixir-lang-core

Heya

You could write a library that does this rather than modifying Elixir. It would be a cool project :)

Cheers,
Louis


--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/638bb1fe-5276-49a3-9013-a784625226ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gasp...@gmail.com

unread,
Sep 30, 2017, 7:42:54 AM9/30/17
to elixir-lang-core


On Saturday, September 30, 2017 at 2:36:30 PM UTC+3, Louis Pilfold wrote:

Heya

You could write a library that does this rather than modifying Elixir. It would be a cool project :)

Cheers,
Louis


I wonder if I can redefine  'def' macro and inject it into every module without modifying module to have 'use Something' in them.

/Gaspar

José Valim

unread,
Sep 30, 2017, 7:46:04 AM9/30/17
to elixir-l...@googlegroups.com
No, they have to "use something". Being unable to have a magic command that modifies all modules globally is what helps meta-programming in Elixir be a bit sane.



José Valim
Founder and 
Director of R&D

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/0c977fbd-d5a9-4c74-8209-741b550ce43e%40googlegroups.com.

Ben Wilson

unread,
Sep 30, 2017, 7:46:17 AM9/30/17
to elixir-lang-core
No, there is intentionally no way to globally apply a macro.

gasp...@gmail.com

unread,
Sep 30, 2017, 7:48:04 AM9/30/17
to elixir-lang-core


On Saturday, September 30, 2017 at 2:46:04 PM UTC+3, José Valim wrote:
No, they have to "use something". Being unable to have a magic command that modifies all modules globally is what helps meta-programming in Elixir be a bit sane.



Okay, then small shell (or perl :) script will do it :) :P

 

gasp...@gmail.com

unread,
Sep 30, 2017, 8:50:06 AM9/30/17
to elixir-lang-core
Hi Jose!

Ok, just to speed up my development - where in Elixir source tree can I find type/spec parser? Because it definitely parses them and compains about callbacks signature mismatch :)

/Gaspar

On Saturday, September 30, 2017 at 2:46:04 PM UTC+3, José Valim wrote:

Shunsuke Kirino

unread,
Sep 30, 2017, 5:07:49 PM9/30/17
to elixir-l...@googlegroups.com
Hi Gasper,

You might be interested in `defun` macro provided by croma:
Hope this helps!

Shunsuke Kirino


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/ddb36f63-0b6c-4d61-a7a3-bc6bba980b8e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages