They aren't exactly equivalent.

The difference comes into play with code loading. The local captures will 
reference the version of the module they were captured with, while 
remote/external captures will always reference the latest version of the module.

Depending on what the closure is doing, the latter may be perfectly fine, and 
is more efficient - but there are many situations where that would result in 
bugs/unexpected behavior. As a result, the compiler cannot make a choice that 
works in all situations. Obviously, in situations without hot code loading, 
this is irrelevant - but you can't know if that is the case at compile-time.

Hope that provides some useful context on why things work the way they do!

Paul

On Sat, Mar 23, 2024, at 12:50 PM, 'Rudolf Manusadzhian' via elixir-lang-core 
wrote:
> Hi there! Might be a naive question.
> 
> Why don't we treat all local captures as external? Like at compile time, if 
> it's possible, we could treat *&fun/0* as *&__MODULE__.fun/0* and get a free 
> performance boost, if in the end they behave same.
> 
> I was going thought the docs for :telemetry.attach/4 
> <https://hexdocs.pm/telemetry/1.2.1/telemetry.html#attach/4> and the 
> suggestion to avoid using *local* captures raise that thought.
> 
> If that's not possible, does it make sense to add a note in the docs 
> Kernel.SpecialForms.&/1 # Capture 
> <https://hexdocs.pm/elixir/1.16.2/Kernel.SpecialForms.html#&/1-capture> about 
> the performance implications of local capture vs external or a recommendation?
> 
> Thanks.
> 
> 
> -- 
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/elixir-lang-core/c7065947-b48d-43e6-a450-387f6d4f1846n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/elixir-lang-core/c7065947-b48d-43e6-a450-387f6d4f1846n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/09582a78-ee51-4ac2-9e75-5171cc73763b%40app.fastmail.com.

Reply via email to