<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="309768" data-post-id="309768">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cmo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cmo/120/19618_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cmo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Editors can help with readability without adding extra syntax to the variable.  Rider adds an underline for mutable variables in F#</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/f/0/f0ce09e3007f6772028eef65d4a4d2496de3b286.png" data-download-href="https://forum.elixirforum.com/uploads/default/f0ce09e3007f6772028eef65d4a4d2496de3b286" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/f/0/f0ce09e3007f6772028eef65d4a4d2496de3b286.png" alt="image" data-base62-sha1="ymg1LZPeik9GfYX1EtIwBpYuKZo" width="454" height="399"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">454×399 14 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><p></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309768" data-batch-url="/posts/batch_likers">
                        8
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-309768" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309768"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #42"></div>
  </section>
</div>
    <div class="postbit" id="309769" data-post-id="309769">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="adw632" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  adw632
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="39" data-topic="60093">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>Definitely, which is why it is one of several proposals on this topic. Your suggestions are similar in semantics to the previous one, except with a different syntax, so I really recommend reading the past thread.</p>
<p>I am not saying we should accept this because the previous one was rejected. After all, we can still leave the solution space open, but we should avoid rehashing non-accepted solutions.</p>
</blockquote>
</aside>
<p>I have re-read the past thread with the <code>let</code> proposal. What I proposed is not the same, the major difference being the variables bound in <code>let</code> need to be returned as the tuple from the block.</p>
<p>I didn’t have a problem with <code>for let</code> but I didn’t like that proposal because the return tuple from the comprehension is not connected to the let binding in the next round in an obvious or intuitive way. Needing to provide a result tuple with the results ordered in a particular way and the usage of <code>let</code> didn’t convey mutation/rebinding on each iteration. It would be better conveyed with say <code>var</code> instead of <code>let</code>.</p>
<p>I am suggesting similarly declaring the state in the comprehension (let’s not worry about syntax for now) but <em>automatically</em> using <em>whatever</em> value they bind to for the next “iteration”. If that state is also to be returned as a result of the comprehension (i.e. binding to variables in the parent scope), it similarly will take the current binding of whatever the comprehension set or left them at.</p>
<p>This way the comprehension state is continuously being reused and rebound as needed each iteration without manual effort to explicitly return it. It’s basically like <code>mut</code> <em>within</em> the comprehension and some way to return this state to the outer scope at the end which is not part of the <code>for</code> map/reduce result. You could return a tuple containing the result and the state (not as obvious IMO) or provide some other way to express the binding between the comprehension state values and the parent scope.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309769" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-309769" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309769"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #43"></div>
  </section>
</div>
    <div class="postbit" id="309780" data-post-id="309780">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adw632" data-post="44" data-topic="60093">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/9d8465/48.png" class="avatar"> adw632:</div>
<blockquote>
<p>I have re-read the past thread with the <code>let</code> proposal. What I proposed is not the same, the major difference being the variables bound in <code>let</code> need to be returned as the tuple from the block.</p>
</blockquote>
</aside>
<p>This was certainly discussed at some point, so perhaps it was in a past-past proposal or in a branched thread?</p>
<p>In any case, it is worth pointing out that tying it to comprehensions in your example is purely a syntactic restriction. The implementation complexity would be pretty much the same and anyone could get the same behaviour as this proposal by doing:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for _ &lt;- 1..1, ^session_counter, ^lesson_counter do
  for lesson &lt;- lessons do
    ...
  end
end
</code></pre>
<p>So tying it to comprehensions only makes the comprehensions feel more bloated (beyond generators, filters, into, and reduce), which was one of the main concerns before, and less likely to be used.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309780" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/45">Post #44</a>
	                </div>
	            </div>
              <div id="likers-container-309780" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309780"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #44"></div>
  </section>
</div>
    <div class="postbit" id="309781" data-post-id="309781">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lud" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/120/14382_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lud
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I fear that the mental model for the code execution will be suddenly way harder because I can see less experienced developers (at least reagarding FP patterns) abusing those mutable variables because they think in a mutable way by default.</p>
<p>And it will be hard to argue with them that the code is a mess because yes, there is more whitespace delimitations, less state carrying, etc. So obviously the code is easier to understand.</p>
<p>Except it’s not. Now whenever you have a 15-lines functions you will have to constantly check if the variable is mutable whenever you see <code>=</code>. A different operator or assignment form has limitations, (cannot mix mut and regular variables in assignment) but at least it would limit those problems.</p>
<p>I fear there will be many cases of assignment where the semantics would not be clear:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">mut x = 10

x = 
  case {:a, 1} do
    {:a, x} -&gt; x + 1
    {:b, x} -&gt; x * 2
  end
</code></pre>
<p>Or the compiler will forbid a lot of stuff and get in the way.</p>
<p>So I would say no to that. But I like the proposal anyway, I’m glad this would be possible in Elixir with some compiler magic.</p>
<p>I feel that the first step towards the section/lesson problem should be smaller. For insance turn this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">vars = []
vars = if opts[:foo], do: [{:foo, get_foo()} | vars], else: vars
vars = if opts[:bar], do: [{:bar, get_bar()} | vars], else: vars
</code></pre>
<p>into this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">vars = []
vars ^= if opts[:foo], do: [{:foo, get_foo()} | vars]
vars ^= if opts[:bar], do: [{:bar, get_bar()} | vars]
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309781" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-309781" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309781"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #45"></div>
  </section>
</div>
    <div class="postbit" id="309786" data-post-id="309786">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="adw632" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  adw632
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="45" data-topic="60093">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>So tying it to comprehensions only makes the comprehensions feel more bloated (beyond generators, filters, into, and reduce), which was one of the main concerns before, and less likely to be used.</p>
</blockquote>
</aside>
<p>I do take your point that it’s a lot of special tratment for a single construct rather than a broader language capability.</p>
<p>Comprehensions do appear to have something about them that, pardon my pun, “reduces” their use in actual code vs other idioms.</p>
<p>I can certainly see where comprehensions are useful for flattening the processing with multiple generators. My feeling is that comprehensions don’t compose with pipelines so we just use Enum or Stream which does.</p>
<p>Perhaps an <code>Enum.for</code> that works similar to <code>for</code> where the generator input goes on the left and can have additional generators and state would cross the cavern, but I’m just hypothesizing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309786" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-309786" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309786"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #46"></div>
  </section>
</div>
    <div class="postbit" id="309797" data-post-id="309797">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="thomasbrus" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thomasbrus/120/40196_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  thomasbrus
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<ol>
<li>What should we call them? Local state? Local accumulators?</li>
</ol>
</blockquote>
<p>Calling them local accumulators makes the concept a lot easier to understand imho.</p>
<p>It fits the reason for proposing this feature in the first place (to locally accumulate some values).</p>
<p>The moment state or mutability is mentioned it gets confusing. Since you can also keep track of state in Elixir via processes. And as you mentioned the compiled code is still immutable.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309797" data-batch-url="/posts/batch_likers">
                        8
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-309797" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309797"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #47"></div>
  </section>
</div>
    <div class="postbit" id="309800" data-post-id="309800">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bit4bit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bit4bit/120/21134_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bit4bit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote group-livebook_core_team" data-username="josevalim" data-post="45" data-topic="60093">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/48/1787_2.png" class="avatar"> josevalim:</div>
<blockquote>
<p>So tying it to comprehensions only makes the comprehensions feel more bloated (beyond generators, filters, into, and reduce), which was one of the main concerns before, and less likely to be used.</p>
</blockquote>
</aside>
<p>Ok, now i understand that it’s about not losing the possibility of using the “accumulators” in all the mechanisms that comprehensions offers. However, opening the accumulators outside of the comprehensions seems to open the door to new (or old) strange code</p>
<p>I am sure i will start reading code like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">let $flag = false
if $flag == false do
  $flag = true
end
....nested block...
     $flag = false
</code></pre>
<p>and lost the restrictions that allows to think in a different way.</p>
<p>what about a new kind of comprehensions, maybe <code>forlet</code> or some other that limits the scope of accumulators only to the concept of comprehensions?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309800" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/49">Post #48</a>
	                </div>
	            </div>
              <div id="likers-container-309800" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309800"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #48"></div>
  </section>
</div>
    <div class="postbit" id="309801" data-post-id="309801">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="thiagomajesk" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thiagomajesk/120/31904_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  thiagomajesk
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes! Separating the concept from plain variables is imperative and it makes a lot of sense… In this case, I think the possible <code>@@</code> syntax mentioned by <a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a> would fit the mental model very well.  IMHO because we already use <code>@</code> to define attributes (which can be used somewhat like compile-time accumulators) it makes the mental jump a lot easier and avoids confusion.</p>
<p>I must say that I like this version a lot..</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
@@list = []

if some_value? do
  @@list = ["prefix" | list]
end
</code></pre>
<p>And I find its explicitness very appealing…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@@section_counter = 0
@@lesson_counter = 0

for section &lt;- sections do
  if section["reset_lesson_position"] do
    @@lesson_counter = 0
  end

  @@section_counter = @@section_counter + 1

  lessons =
    for lesson &lt;- section["lessons"] do
      @@lesson_counter = @@lesson_counter + 1
      Map.put(lesson, "position", @@lesson_counter)
    end

  section
  |&gt; Map.put("lessons", lessons)
  |&gt; Map.put("position", @@section_counter)
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309801" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/50">Post #49</a>
	                </div>
	            </div>
              <div id="likers-container-309801" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309801"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #49"></div>
  </section>
</div>
    <div class="postbit" id="309815" data-post-id="309815">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="thiagomajesk" data-post="50" data-topic="60093">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/thiagomajesk/48/31904_2.png" class="avatar"> thiagomajesk:</div>
<blockquote>
<p>IMHO because we already use <code>@</code> to define attributes (which can be used somewhat like compile-time accumulators) it makes the mental jump a lot easier and avoids confusion.</p>
</blockquote>
</aside>
<p>I completely agree with this as well. I think it also helps the mental model around “what happens when I pass this to a function”? In both @ and @@ the answer is the same: you just get a regular value. They aren’t defining special values, just special syntax. From the perspective of a called function everything is as normal.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309815" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/51">Post #50</a>
	                </div>
	            </div>
              <div id="likers-container-309815" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309815"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #50"></div>
  </section>
</div>
    <div class="postbit" id="309832" data-post-id="309832">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>Why cannot solve this “problem” using some macro magic?</p>
</blockquote>
<p>It’s actually easy to do this withing a scoped block using macros defined in a 140-line file. I’ve uploaded a gist here: <a href="https://gist.github.com/tmbb/bdac264c5e4974818db074038fb41fd2" class="inline-onebox" rel="noopener nofollow ugc">dark_mutable.ex · GitHub</a> (I can’t push it to the main github repo because right now I’m on a network that doesn’t suppor SSH).</p>
<p>You can see it being used in a simple factorial function (note that mutable assignment is done using the <code>&lt;&lt;~</code> operator; I don’t think that mutable variables should be assigned with the <code>=</code> operator, even though it’s very easy to implement it):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule DarkKernel.DarkMutableTest do
  use ExUnit.Case, async: true
  import DarkKernel.DarkMutable

  def factorial(n) do
    # Define a mutable variable, which is only accessible inside the block
    mutable! result: 1 do
      for i &lt;- 1..n do
        # Update the variable in a way that's propagaded "up" the AST scopes
        # in the entire `mutable!` block.
        result &lt;&lt;~ result * i
      end

      # Return the "mutable variable"
      result
    end
  end

  test "factorial function works" do
    assert factorial(1) == 1
    assert factorial(2) == 2
    assert factorial(3) == 6
    assert factorial(4) == 24
    assert factorial(5) == 120
  end
end
</code></pre>
<p>This generates the following code:</p>
<pre data-code-wrap="plaintext"><code class="lang-plaintext">18:07:07.305 [debug] test/dark_kernel/dark_mutable_test.exs:7
┌────────────────────────────────────────────────────────────────────────────────────────────────────
│ ⤷ Code:
├╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶
│ for i &lt;- 1..n do
│   DarkKernel.DarkMutable.__set__(result, DarkKernel.DarkMutable.__get__(result) * i)
│ end
│
│ DarkKernel.DarkMutable.__get__(result)
├────────────────────────────────────────────────────────────────────────────────────────────────────
│ ⤷ Generated code:
├╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶
│ result = make_ref()
│
│ try do
│   DarkKernel.DarkMutable.__set__(result, 1)
│
│   (
│     for i &lt;- 1..n do
│       DarkKernel.DarkMutable.__set__(result, DarkKernel.DarkMutable.__get__(result) * i)
│     end
│
│     DarkKernel.DarkMutable.__get__(result)
│   )
│ after
│   DarkKernel.DarkMutable.__delete__(result)
│ end
├────────────────────────────────────────────────────────────────────────────────────────────────────
</code></pre>
<p>The code above doesn’t generate the</p>
<p><a class="mention" href="/u/josevalim" rel="nofollow">@josevalim</a>, given that I have implemented this in about 1h30min, I don’t think major language changes are needed, and I don’t see much use for the <code>mut</code> keyword.</p>
<p>I’d even argue that having the variables scoped inside a clearly delimited block is a much simpler solution. And this implementation automatically gaurantees that external processes can’t change the references, because there isn’t a way of sending the reference somewhere else. Reference values are always passed by copy.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309832" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/koka-inspired-local-mutable-variables-for-cleaner-comprehensions/60093/52">Post #51</a>
	                </div>
	            </div>
              <div id="likers-container-309832" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309832"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #51"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/60093/load_more?page=6">Load more posts (20 remaining)</a>
</div></template></turbo-stream>