cardillan / mindcode

A high level language for Mindustry Logic and Mindustry Schematics.

Home Page:http://mindcode.herokuapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Continue and Break can't be used inside functions

limonovthesecond2 opened this issue · comments

Functions with continue or break generate a compilation error.
Example:

for i in 0 .. 1
    foo(i)
end

def foo(i)
    if cell1[i] == 0
        continue
    end
    print(i)
end

Isn't that the expected behavior? Most languages do not allow having continue and break statements that are not syntactically contained within their respective control structures, just like you can't return out of a higher order function from inside a callback.

@JeanJPNM is right, this works as expected. break and continue must be enclosed in a loop. They cannot jump out of a function, even if the function would always be called from a loop.

You need to move the continue to the loop itself, something like this:

for i in 0 .. 1
    if foo(i) continue end
    // Do something else here, otherwise the continue is not needed at all
end

def foo(i)
    if cell1[i] == 0
        return 1
    end
    print(i)
    return 0
end

It should get optimized pretty well when the function gets inlined.