Local accumulators for cleaner comprehensions

I can see how something like this would make Elixir more approachable to new devs. I know abusing this can produce some really hard-to-debug code, but so do macros (I know I did some hideous stuff with macros), and they are one of the most powerful features of the language.

We can always guide people to write more declarative, expression-first code through documentation and code reviews, something we won’t have the chance to do if new devs can’t get past learning how a reduce works (especially those without a functional background).

So I’m all for it, as it will be useful in some corner cases, and help devs transition from an imperative to a more functional/declarative style, which should be very useful with the advance of ML/data science tooling in Elixir and more folks without a strong CS background might have a chance of joining the boat.

I’m not too concerned about this causing the same problems having assignments inside if/else caused, as it is a very specific syntax. You don’t need to worry about a loop changing random variables under your nose, values mutating within function calls, or anything like that.

Even in a fairly large function body (which should be avoided in the first place), you just need to watch for a @@ and how it evolves, which is much better than tracking all the variables as any of them might be reassigned at any moment.

@@sum = 0

I’m still not sold on the @@ though haha

I can see how it would help bring the dev’s attention to these vars though, so I guess I could learn to love it.

Would be interesting to see what some alternatives look like if this is approved though. :smile:

2 Likes