You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 typeifiseven(i)
continue i # append to returned arrayelseif i ==13continue"not Int64"# throw an exceptionendfinally# or `else`
newvalues = values *2return newvalues # explicit returnend
P.S. This is my first foray into github, I welcome constructive criticism/feedback :)
The text was updated successfully, but these errors were encountered:
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 issueIndicates a good issue for first-time contributors to Julia
so you can start with rewarding experiences before sinking your teeth in the big ones :)
These are a couple of ideas to extend those proposed in #22891.
As a natural extension to
break
-with-value in a loop, I proposecontinue
to append a value to a returned array.I had initially thought
default
would be more descriptive thanelse
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 aboutfinally
? 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 thefinally
block.Are there any issues with using a type declaration on the assigned variable that would need to be addressed as well?
P.S. This is my first foray into github, I welcome constructive criticism/feedback :)
The text was updated successfully, but these errors were encountered: