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

continue/break-with-value & else clauses in loops #40372

Closed
jakobjpeters opened this issue Apr 6, 2021 · 1 comment
Closed

continue/break-with-value & else clauses in loops #40372

jakobjpeters opened this issue Apr 6, 2021 · 1 comment
Labels
speculative Whether the change will be implemented is speculative

Comments

@jakobjpeters
Copy link
Contributor

jakobjpeters commented Apr 6, 2021

These are a couple of ideas to extend those proposed in #22891.

  • As a natural extension to break-with-value in a loop, I propose continue to append a value to a returned array.

  • I had initially thought default would be more descriptive than else and may lessen objections to it (break-with-value and for/else implementation #23260 (comment)). However, @tkluck pointed out (break-with-value and for/else implementation #23260 (comment)) that this would not be backwards compatible because 'default' is not a keyword. What about finally? It makes sense in context with loops; after iteration it is the final block of code to run.

  • The return keyword could be used to explicitly exit the finally block.

  • Are there any issues with using a type declaration on the assigned variable that would need to be addressed as well?

values = rand(1:100, 10)

evens::Array{Int64} = for i in values # optional return type
    if iseven(i)
        continue i # append to returned array
    elseif i == 13
        continue "not Int64" # throw an exception
    end
finally # or `else`
    newvalues = values * 2
    return newvalues # explicit return
end

P.S. This is my first foray into github, I welcome constructive criticism/feedback :)

@tkluck
Copy link
Contributor

tkluck commented Apr 6, 2021

Thanks for sharing the ideas! FYI I'm not a committer to Julia but I'll share some thoughts that are hopefully helpful.

  • Backwards compatibility. New syntax has to be a syntax error in current versions of Julia -- that way you can be sure it doesn't already have a meaning. This was the problem with default and it's the same with events::Array{Int64} = for ...: this is currently valid syntax that will throw a runtime error when trying to assign the loop expression's value (nothing) to the variable.

  • Selling point. What gap in the language does this fill? For example, for for/else we could find applications in the standard library (break-with-value and for/else implementation #23260 (comment)). Maybe you can do the same? It's up to you to prove that you have "product/market fit". If you're able to point at other languages that have a feature like this, even better.

For comparison here's a way of achieving what you want in current Julia:

mapreduce((a,b) -> push!(a,b...), values, init=Int64[]) do i
    if iseven(i)
        return (i,)
    elseif i == 13
        throw("not Int64")
    else
        return ()
    end
end

It's a little bit more cumbersome, but on the other hand it's able to push several values in a single iteration (which is incidentally a shortcoming of using continue for accumulation).

Hope that's helpful! And to set expectations, the chances of something like this being merged are very small -- #23260 also didn't make the triage. You could have a look at good first issue Indicates a good issue for first-time contributors to Julia so you can start with rewarding experiences before sinking your teeth in the big ones :)

@stevengj stevengj added the speculative Whether the change will be implemented is speculative label Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

3 participants