Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for/else #1247

Open
bbrk24 opened this issue May 15, 2024 · 3 comments
Open

for/else #1247

bbrk24 opened this issue May 15, 2024 · 3 comments

Comments

@bbrk24
Copy link
Contributor

bbrk24 commented May 15, 2024

From Python. Real-world situation where I want it:

        fail .= false
        for i .= engine.board.tableau[src.1 as number]# - 1; i >= src.2!; --i
          unless engine.moveTableauToFoundation src.1 as number, dest.1 as 'asc' | 'desc', dest.2 as number
            fail = true
            break
        if fail
          // May have made partial progress; restore from history
          engine = Engine.fromJSON history.-1
        else
          unHighlight()
          updateRender()

vs with for/else:

        for i .= engine.board.tableau[src.1 as number]# - 1; i >= src.2!; --i
          unless engine.moveTableauToFoundation src.1 as number, dest.1 as 'asc' | 'desc', dest.2 as number
            // May have made partial progress; restore from history
            engine = Engine.fromJSON history.-1
            break
        else
          unHighlight()
          updateRender()
@edemaine
Copy link
Collaborator

We discussed this at some length here: #1083 (and probably Discord before that)
Ha, there's even an ancient partial PR for it: #359

While I agree for..else is intuitive if you come from Python, Daniel pointed out that it's ambiguous whether else is the break case or the non-break case. Both are common, depending on the use-case. (Does break mean "I succeeded" or "I failed"?) The idea was to allow both options, via if break or unless break (and else clauses). I feel like things then got a little more complicated with assigning to break and if break? and I'm not sure we ended up in the best proposal... but perhaps we can refine it to find the best form.

@STRd6
Copy link
Contributor

STRd6 commented May 15, 2024

Some relevant Python references:

I'm a fan of the capability but I'd like to find a better way to name it. The Python for ... else way seems to be confusing even for Python fans. It would also be nice to have a clause for the break path as well, similar to how we have try ... catch ... else ....

@wmstack
Copy link

wmstack commented Oct 24, 2024

I think another interesting ambiguity, and one that arises in certain situations, perhaps during debugging if nowhere else, is whether the loop ran at all. If the body of the loop body didn't run then else block is executed, otherwise else block is not executed.

This is a natural and perhaps useful way to think of an else block, as it mirrors the if else statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants