# Why for loops don't return the last evaluated expression as their value?

**URL:** <https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748>\
**Category:** General Usage\
**Created:** [December 13, 2017, 10:27pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748 "2017-12-13T22:27:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [December 13, 2017, 10:27pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/1 "2017-12-13T22:27:55Z")

</div>

I was wondering why `for` loops don’t return the last evaluated expression – similar to `if` statements, for example.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 13, 2017, 11:41pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/2 "2017-12-13T23:41:32Z")

</div>

This is just speculation, but perhaps it’s because it’s not clear what to return when the iterator is empty? That is, if you wrote:

```julia
y = for i in 1:N 
  i + 1
end

```

then what should `y` be if `N < 1` (in which case the body of the loop is never executed)? We could decide that `nothing` is a sensible default, but then that would make assigning the result of the loop inherently type-unstable, since the returned type (Int or Void) would depend on the value of N.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2017, 12:34am UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/3 "2017-12-14T00:34:38Z")

</div>

Correct, it’s an inherent type instability. See also:

[https://github.com/JuliaLang/julia/pull/23260](https://github.com/JuliaLang/julia/pull/23260)

This PR implements break-with-value and for with else, which together mean that it actually does make sense for a loop to return a value. Still a speculative feature, but we’ve got an implementation and may add this feature in the future (1.x).

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [December 14, 2017, 8:27am UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/4 "2017-12-14T08:27:27Z")

</div>

Thanks

@rdeits How would that be any different from

```julia
z = if false
  "moo"
end

```

Only through the fact that one can optionally add an `else` as a safeguard against type instability?

@StefanKarpinski Can’t remember seeing these constructs in any other languages that I’ve used, but I find them super-cool. Very useful syntax innovations, I’d love to see them available.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2017, 8:40am UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/5 "2017-12-14T08:40:36Z")

</div>

That’s a good point – it’s not really any different. I suppose we could make loops return the last value evaluated in the loop. It might be annoying in the REPL when you write loops.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [December 14, 2017, 9:01am UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/6 "2017-12-14T09:01:13Z")

</div>

That would certainly make the behavior consistent. But to be truly useful, in the absence of more exotic `for-else` or `break-with-value` constructs, `break` would also have to return the last evaluated expression. That’s how I stumbled into this actually, trying to do something in the line of:

```julia
_path, _extension = for file_extension in [".html", ".md", ".mt"]
  if isfile(file_path * file_extension)
    file_path * file_extension, file_extension
    break
  end
end

```

To me it looks like a legit usage of the language, something I definitely expected to work and it took me quite a bit to understand the error I was getting.

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [December 14, 2017, 12:34pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/7 "2017-12-14T12:34:22Z")

</div>

> [@essenciary](#):
>
> That would certainly make the behavior consistent.

Looks like this might be a good fit for the [API consistency review issue](https://github.com/JuliaLang/julia/issues/20402).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 14, 2017, 12:44pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/8 "2017-12-14T12:44:45Z")

</div>

I can certainly see the appeal — I occasionally think about syntactic extensions that would make sense in a given context, but at the same time I admire the restraint in the design of Julia that kept complexity and clever variants of syntax with subtly different semantics to a minimum.

Is

```julia
result = some_default
for elt in itr
    ...
end
result

```

that inconvenient? I find it to be a good trade-off between readability and compactness.

I could also imagine a Lisp-like

```julia
@prog1 result = some_default for elt in itr
    ...
end

```

but again, I don’t think it is worth it.

---

<div class="post-metadata">

**Author:** ![Steven\_Sagaert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steven_sagaert/32/29578_2.png) [@Steven\_Sagaert](https://discourse.julialang.org/u/Steven_Sagaert)\
**Post date:** [December 14, 2017, 2:20pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/9 "2017-12-14T14:20:17Z")

</div>

You could do it like in Scala where if you want the for to return a value you need a yield statement inside the loop.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2017, 11:21pm UTC](https://discourse.julialang.org/t/why-for-loops-dont-return-the-last-evaluated-expression-as-their-value/7748/10 "2017-12-14T23:21:31Z")

</div>

Yes, that’s what the PR I linked to above does.
