# \`depart\` for \`return\`, a syntactic sugar for blocks with early-stop

**URL:** https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407
**Category:** New to Julia
**Created:** [February 14, 2022, 10:19am UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407 "2022-02-14T10:19:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![complyue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/complyue/32/33802_2.png) [@complyue](https://discourse.julialang.org/u/complyue)
#### Post date: [February 14, 2022, 10:19am UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/1 "2022-02-14T10:19:18Z")

</div>

Julia has `let` block in case you need a local scope, so you don’t often need ad-hoc defined+called anonymous function for that as in JavaScript. But another sugar in the ad-hoc called function syntax is, you can early-return inside that block.

I’m not sure this is my new discovery in Julia, though the approach is rather simple.

```julia
function depart(f, args...) f(args...) end

```

Then for lengthy blocks those don’t deserve a dedicated function name:

```julia
m, n = 2, -7
#...

depart(3n, m) do x, y

  if x > 10
    println("Big x=$x vs $y")
    return
  end

  if x < 0
    println("Negative x=$x vs $y")
    return
  end

  @assert false "Not that anyway expected."

end

```

I like it quite much, and I hope it be appropriate Julia practice, what’s your thoughts?

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [February 14, 2022, 12:31pm UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/2 "2022-02-14T12:31:14Z")

</div>

I’ve definitely wished on few occasions to be able to “break out” of the current `let` (or similar) block. But `break` can’t be used for that in a non-breaking julia change, and I’d guess the usefulness of this is too marginal to introduce a new keyword for it. Your trick with `depart` is cool, although I would probably not bother in general, and instead just use `if/else` blocks, or even `@goto end_of_block` if it’s more practical.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 14, 2022, 6:07pm UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/3 "2022-02-14T18:07:32Z")

</div>

Doesn’t `map` work the same way?

```julia
julia> m, n = 2, -7
(2, -7)

julia> map(3n,m) do x, y
             if x > 10
           println("Big x=$x vs $y")
           return
         end

         if x < 0
           println("Negative x=$x vs $y")
           return
         end

         @assert false "Not that anyway expected."

       end
Negative x=-21 vs 2

```

---

<div class="post-metadata">

### Author: ![complyue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/complyue/32/33802_2.png) [@complyue](https://discourse.julialang.org/u/complyue)
#### Post date: [February 14, 2022, 6:16pm UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/4 "2022-02-14T18:16:51Z")

</div>

Yes, someone at r/Julia pointed that too: [https://www.reddit.com/r/Julia/comments/ss7jel/comment/hwxer6n/?utm\_source=share&utm\_medium=web2x&context=3](https://www.reddit.com/r/Julia/comments/ss7jel/comment/hwxer6n/?utm_source=share&utm_medium=web2x&context=3)

But `map` would collect the results into a vector, which is bloating if you don’t need that. Also `depart` seem to express the intention clearer, i.e. marking a return point for nested `return`s.

---

<div class="post-metadata">

### Author: ![patrick-kidger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick-kidger/32/20378_2.png) [@patrick-kidger](https://discourse.julialang.org/u/patrick-kidger)
#### Post date: [February 14, 2022, 6:59pm UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/5 "2022-02-14T18:59:06Z")

</div>

This is pretty neat. I like it.

I’ve definitely found myself running into needing something like this every now and again.

I think it can probably be simplified slightly: `depart(f) = f()` and then simply

```julia
depart() do
    # capture variables through closure
end

```

e.g.

```julia
x = 2
depart() do
    if x > 10
        ...
    end
end

```

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [February 14, 2022, 7:06pm UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/6 "2022-02-14T19:06:41Z")

</div>

> [@complyue](#):
>
> But `map` would collect the results into a vector, which is bloating if you don’t need that.

That particular aspect can be solved by replacing `map` with `foreach`. But of course there are other differences.

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [February 15, 2022, 4:41am UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/7 "2022-02-15T04:41:22Z")

</div>

Nice idea. Regarding the name, I find `depart` a bit misleading - it _enables_ departing, doesn’t perform a depart itself. Some names that occur off the top of my head are:

- `departable(3n, m) do ...` - seems fine; I would prefer the `return` keyword was more directly referred to somehow, but one can get used to this; _maybe_ someone unfamiliar could read it as something to do with tables, though that also seems a minor first-use-only issue.
- `exitable(3n, m)` - I can’t decide if this sounds cool or sounds weird. Leaning towards weird tbh.
- `withreturn(3n, m)` - I like that it explicitly mentions the `return` which makes the point of the block immediately clear. That makes me slightly prefer this over `departable`
- `returnable(3n, m)` - this makes it sound like this is something _to be returned_, not something in which `return` happens
- `breakable(3n, m)` - this is like `departable`, but uses a more familiar keyword, and one can quickly learn to see `return`s within the block as `break`s
- For those that don’t mind verbose names, `breakableblock` is a good name too.

(One could turn either of the last two into a macro and have it accept actual `break` statements, and replace those with `return` statements in the macro - but let’s keep it simple here.)

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [February 15, 2022, 4:55am UTC](https://discourse.julialang.org/t/depart-for-return-a-syntactic-sugar-for-blocks-with-early-stop/76407/8 "2022-02-15T04:55:29Z")

</div>

Oh, and for those familiar with Lisp-y languages, `apply` is a nice name too. For those with that context, the `return`s will have obvious meaning.

PS: On that note, I find it surprising that Julia doesn’t have a `apply`/`call` function or statement (unless I’m forgetting something?). Neither `invoke` nor `invokelatest` is directly equivalent to a normal function call in all situations. One can do this using the non-allocating `Iterators.map` and `only`, but it’s clunky (in terms of syntax):

```julia

julia> Iterators.map(3n, m) do x, y
                if x > 10
                  println("Big x=$x vs $y")
                  return
                end
       
                if x < 0
                  println("Negative x=$x vs $y")
                  return
                end
       
                @assert false "Not that anyway expected."
       
              end |> only
Negative x=-21 vs 2

```
