Enum.each of defs

95 views
Skip to first unread message

Luke Imhoff

unread,
Oct 29, 2016, 11:14:47 PM10/29/16
to elixir-lang-core
I had thought that `def` returned (the equivalent [because it's really in elixir_bootstrap.erl] of a) quoted block and in order for a `def` to count in a loop, you'd need either a `for` comprehension, or `Enum.map`, so that the `quote` from each `def` is concatenated together to be merged into the caller's quoted form, but [Postgrex.Message.decode_auth_type/1 uses Enum.each](https://github.com/elixir-ecto/postgrex/blame/master/lib/postgrex/messages.ex#L370-L372).  How are all the `def`s inside the `Enum.each` being preserved when `Enum.each` doesn't return the value of each function call? This is related to https://github.com/KronicDeth/intellij-elixir/issues/472.

José Valim

unread,
Oct 30, 2016, 3:13:25 AM10/30/16
to elixir-l...@googlegroups.com
Def, like defmodule, module attributes and friends, is a side-effect. It stores information in the module table. Returned quoted expressions wouldn't quite work because we would need to collect them until the end. Check the expanded code and it may clarify things a bit.


On Sunday, October 30, 2016, Luke Imhoff <kroni...@gmail.com> wrote:
I had thought that `def` returned (the equivalent [because it's really in elixir_bootstrap.erl] of a) quoted block and in order for a `def` to count in a loop, you'd need either a `for` comprehension, or `Enum.map`, so that the `quote` from each `def` is concatenated together to be merged into the caller's quoted form, but [Postgrex.Message.decode_auth_type/1 uses Enum.each](https://github.com/elixir-ecto/postgrex/blame/master/lib/postgrex/messages.ex#L370-L372).  How are all the `def`s inside the `Enum.each` being preserved when `Enum.each` doesn't return the value of each function call? This is related to https://github.com/KronicDeth/intellij-elixir/issues/472.

--
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/a768af02-fdc1-45be-ad69-e0661f79139b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--


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

Luke Imhoff

unread,
Oct 30, 2016, 10:05:00 AM10/30/16
to elixir-l...@googlegroups.com
Since the output of the `Enum.each`, `for`, and `Enum.map` is effectively ignored because only the entries in the  defs_table (https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_def.erl#L207) matter, is there a performance benefit to replacing any `for` or `Enum.map` around `def` with `Enum.each` so the compilation doesn't spend the time and memory creating a list that'll just be throw away?


--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/rbAlgKeavJU/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAGnRm4KW5Pn6%2Byzvz-9gSx6OCepwscj3WT5JO9RB%2Bo%2B4%2BcFG-g%40mail.gmail.com.

José Valim

unread,
Oct 30, 2016, 10:13:37 AM10/30/16
to elixir-l...@googlegroups.com
"for" will not build a list if you don't use its output value and that's always the case if you have it between expressions, such as:

    a = 1
    for .... do ... end
    b = 2

In this case, because you never use the result of for, we never build a list. Enum.map will build a list regardless but I doubt it would matter to the point of being noticeable.



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

Reply all
Reply to author
Forward
0 new messages