-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
disallow return
outside function?
#30499
Comments
|
👍 to an error. It looks like this is a lowering thing? julia> Meta.@lower y = let
# some ugly calc
return 42
end
42
julia> Meta.@lower y = let
# some ugly calc
return 42
32
end
:($(Expr(:thunk, CodeInfo(
1 ─ return 42
2 ─ y = 32
└── return 32
)))) |
return
outside function
return
outside functionreturn
outside function?
Thinking this over, I'm not sure disallowing |
That was how I had hoped to use the syntax... Another side effect from this issue is that the variable name seems to be created in a module when using julia> module Mod
x = let
return 42
end
const y = let
return 42
end
end
Main.Mod
julia> names(Mod, all=true)
6-element Array{Symbol,1}:
Symbol("#eval")
Symbol("#include")
:Mod
:eval
:include
:y
julia> Mod.y
ERROR: UndefVarError: y not defined |
Allowing |
Let's just document that |
From discussion on triage, in older Julias the actual behavior was that if you didn't evaluate the |
also add some missing doc strings and improve others
also add some missing doc strings and improve others
also add some missing doc strings and improve others (cherry picked from commit 0bb0332)
I have found out that this can lead to some hard-to-diagnose bugs when return statements are incorrectly embedded in macros that are called within functions. Consider following function:
Clearly, In this case, it is obvious, but the situation becomes much more opaque if such return statement comes out of a macro:
In such situation, code calling the macro is short-circuited which may not be apparent. |
Let me clarify - I understand the mechanics of why this is happening but would like to have some safety in place to make it easier to diagnose when this is done accidentally and in situations that are almost certainly incorrect. Based on other duplicates of this bug it seems that other people also find this behavior counter-intuitive and easy to trip over. |
On v1.0.2 I used a return statement from within a let block that got assigned to a variable. This didn't work as I expected, but it also didn't cause any errors. The surprising side effect was that the variable assigned to was never actually created.
The text was updated successfully, but these errors were encountered: