# Named blocks and return

**URL:** https://discourse.julialang.org/t/named-blocks-and-return/1267
**Category:** General Usage
**Tags:** question
**Created:** [January 3, 2017, 7:47pm UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267 "2017-01-03T19:47:03Z")
**Posts on this page:** 6
**Page:** 1

<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: [January 3, 2017, 7:47pm UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/1 "2017-01-03T19:47:03Z")

</div>

Is there some control flow construct which would allow returning from an enclosing block instead of the current function? I am thinking of something similar to [COMMON-LISP:RETURN-FROM](http://clhs.lisp.se/Body/s_ret_fr.htm). For example, it would be useful in early termination, eg in

```julia
function matched_positions(keys, argnames)
    positions = [findfirst(keys, key) for key in argnames]
    any(positions .== 0) ? false : positions
end

```

which could be rewritten as

```julia
function matched_positions(keys, argnames)
    function _find(keys, key)
        p = findfirst(keys, key)
        p == 0 ? return_from matched_positions false : p # hypothetical
    end
    [_find(keys, key) for key in argnames]
end

```

I have seen a `@goto` in `Base`, so I could probably emulate it, but I am wondering if this is a good idea.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [January 3, 2017, 8:06pm UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/2 "2017-01-03T20:06:45Z")

</div>

I know that this isn’t really what you’re asking for, but `any` and `all` now always short-circuit on their own on master.

This kind of behavior is a surprising non-local effect that Julia has typically steered clear from. I think that you’d be surprised by the result even in your toy example if this were implemented. I think it’d end up returning an array of length `length(argnames)`… but with uninitialized gibberish for trailing indices beyond the short-circuit.

---

<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: [January 3, 2017, 9:02pm UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/3 "2017-01-03T21:02:05Z")

</div>

Thanks. Is there a way to write the above to terminate early when possible? I could not figure it out.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 4, 2017, 1:42am UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/4 "2017-01-04T01:42:33Z")

</div>

You can use `break` to terminate a loop early.

---

<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: [January 4, 2017, 10:02am UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/5 "2017-01-04T10:02:14Z")

</div>

> [@mbauman](#):
>
> I think it’d end up returning an array of length length(argnames)… but with uninitialized gibberish for trailing indices beyond the short-circuit.

Apparently the issue has come up, and

```julia
function matched_positions(keys, args)
    return [begin
            p = findfirst(keys, arg)
            if p == 0 @goto nm end
            p
            end
            for arg in args]
    @label skip
    return nothing
end

```

does not even compile. See

> <https://github.com/JuliaLang/julia/issues/13617>
>
> Consider this little snippet
> 
> \`\`\` jl
> while true
> @goto stop
> error()
> end
> @…label stop
> \`\`\`
> 
> When run in the REPL as is or from an \`include()\` call, Julia returns an error
> 
> \`\`\`
> ERROR: syntax: label "stop" referenced but not defined
> \`\`\`
> 
> Putting these lines in a \`function\` block results in the expected no-op behavior, since it introduces local scope.
> 
> However, wrapping these five lines in a \`begin... end\` block, however, \_also\_ allows this code to work, even though no new scope is introduced within this block.
> 
> It appears that the behavior of this code depends on whether it is being interpreted in one case or compiled in another case?
> 
> Discussed with @mbauman
> 
> P.S. \`@goto\` is also not documented in our manual, but that's a separate issue.

---

<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: [January 4, 2017, 10:16am UTC](https://discourse.julialang.org/t/named-blocks-and-return/1267/6 "2017-01-04T10:16:56Z")

</div>

> [@stevengj](#):
>
> You can use break to terminate a loop early.

Sorry, I can’t figure out how that would help me. I find that

```julia
function findfirst_allornothing(A, B)
    positions = similar(B, Int)
    for (index, b) in enumerate(B)
        p = findfirst(A, b)
        p == 0 && return nothing
        positions[index] = p
    end
    positions
end

```

works, ie

```julia
julia> findfirst_allornothing([:a,:b,:c], [:b,:a])
2-element Array{Int64,1}:
 2
 1

julia> findfirst_allornothing([:a,:b,:c], [:d])
# nothing

```

If someone could post a version with `break` that is more elegant, I could learn from that. I don’t see a way to combine `break` with collections though.
