wurstscript / WurstScript

Programming language and toolkit to create Warcraft III Maps
https://wurstlang.org
Apache License 2.0
226 stars 28 forks source link

How can I use 'return' in closure? #1044

Closed tester1101 closed 2 years ago

tester1101 commented 2 years ago

I want to use return in clousre like this.

[case 1]

EventListener.add(EVENT_PLAYER_UNIT_DEATH) -> 
        boolean array bl

        if not bl[0]
            return
        print("bl[0] is true")

        if not bl[1]
            return
        print("bl[1] is true")

        if not bl[2]
            return
        print("bl[2] is true")

        if not bl[3]
            return
        print("bl[3] is true")

This emits error in vscode.

But of cousre, [case 2]

    EventListener.add(EVENT_PLAYER_UNIT_DEATH) -> 
        boolean array bl

        if bl[0]
            print("bl[0] is true")
            if bl[1]
                print("bl[1] is true")
                if bl[2]
                    print("bl[2] is true")
                    if bl[3]
                        print("bl[3] is true")

I can write [case 1] like this, but this looks so nested.

If using return statement in closure is not allowed in wurstscript, is there other way to escape code?

Frotty commented 2 years ago

You can't use return right now. Escaping via ifs should usually suffice. The example seems very artificial, why would you check a boolean array? I think in general you should try to avoid doing big if-conditionals and instead use multiple closures in a reactive style. Add the code to be executed in the ifs in single closures, then add/remove them from a list dynamically. When the event happens simply execute all closures in the list (or only the first if execution shall stop after 1).