# Side discussion on gotos

**URL:** <https://discourse.julialang.org/t/side-discussion-on-gotos/16821>\
**Category:** Internals & Design\
**Created:** [October 26, 2018, 10:50am UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821 "2018-10-26T10:50:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 26, 2018, 10:50am UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821/1 "2018-10-26T10:50:30Z")

</div>

> [@New scope solution](https://discourse.julialang.org/t/new-scope-solution/16707/61):
>
> > question what language construct `@goto` and `@label` is I could probably ask in future in another topic. 😉
> 
> Goto does what’s written on the label 😉 Sorry for the pun. A label labels a position in your code (so it does nothing).

Sorry for off-topic! (if somebody has rights then please cut it into another one)

I meant that understanding macro means understanding code to which it is expanded.

```julia
macro goto(name::Symbol)
    return esc(Expr(:symbolicgoto, name))
end

julia> @macroexpand @goto a
:($(Expr(:symbolicgoto, :a)))

```

I mean that there is not “standard” syntax for `goto` in Julia language and `@goto` macro is expanded into something I am not able to understand in this moment. I just feel that it is similar to something like this:

```julia
# ! mighty lisp please translate this comment as label A
if repeat
   # ! mighty lisp please translate this comment as go to label A
end

```

BTW how to write equivalent code with these macros expanded?

```julia
julia> @macroexpand @label A
:($(Expr(:symboliclabel, :A)))

julia> begin
       i = 1
       :($(Expr(:symboliclabel, :A))) # this doesn't work
       i += 1
       if i<10 
          :($(Expr(:symbolicgoto, :a))) # this doesn't work
       end
       end

```

---

<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:** [October 26, 2018, 12:09pm UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821/2 "2018-10-26T12:09:51Z")

</div>

> [@New scope solution](https://discourse.julialang.org/t/new-scope-solution/16707/64):
>
> Sorry for off-topic! (if somebody has rights then please cut it into another one)

You can just post a reply in a new thread instead of asking an admin to do it.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [October 26, 2018, 12:10pm UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821/3 "2018-10-26T12:10:44Z")

</div>

Afaik, goto is a primitive. It is simply a decision of surface syntax that you use it as a macro instead of keyword. If you look at `code_typed(...; optimize=false)` and `code_lowered` you will see it all over the place. As far as I understood, Stefan’s solution disregards the abstract syntax tree (the thing manipulated by macros, with expressions) and instead considers lowered code and the resulting control flow graph. Lowered code does not contain loops:

```julia
julia> function f(A)
       x=A[1]
       @goto skip
       return y
       @label skip
       while x>0
       x-=1
       end
       x
       end;

julia> code_lowered(f, Tuple{Vector{Int}})
1-element Array{Core.CodeInfo,1}:
 CodeInfo(
2 1 ─ x = (Base.getindex)(A, 1) │
3 └── goto #3 │
4 2 ─ return Main.y │
6 3 ┄ %4 = x > 0 │
  └── goto #5 if not %4 │
7 4 ─ x = x - 1 │
  └── goto #3 │
9 5 ─ return x

```

In this sense, loops are not primitive structures in julia. Stefan’s rule needs to operate on a level between AST and lowered code: We see that currently, the lowering already decides whether bindings are local or global. With his rule, we would need to “semi-lower” without this knowledge, then search the CFG, then annotate local/global, and then go on as before. People who want to _really_ understand scoping in top-level would need to understand the lowering process as well as parsing (AST construction).

PS. Thanks @StefanKarpinski for moving us out of the main thread!

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 26, 2018, 12:59pm UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821/4 "2018-10-26T12:59:20Z")

</div>

Are you able to write `@goto` and `@label` expanded?

```julia
julia> function f()
             i = 1
             eval(:($(Expr(:symboliclabel, :A)))) # (1)
             i += 1
             if i<10 
                eval(:($(Expr(:symbolicgoto, :A)))) # (2)
             end
             end
julia> f()
ERROR: syntax: label "A" referenced but not defined

```

similar (\*) error if we change (1) to `@label A` or if we change (2) to `@goto A`. (obviously if we change both it works 😉 )

It is because `eval` is “post-lisp-phase” execution?

(\*) second one is without stack trace which support “lisp-phase” hypothesis…

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 26, 2018, 2:17pm UTC](https://discourse.julialang.org/t/side-discussion-on-gotos/16821/5 "2018-10-26T14:17:02Z")

</div>

```julia
julia> @eval begin
       function f()
           i = 1
           $(Expr(:symboliclabel, :A))
           i += 1
           if i<10
               println(i)
               $(Expr(:symbolicgoto, :A))
           end
       end
       end
f (generic function with 3 methods)

julia> f()
2
3
4
5
6
7
8
9

```
