-
Notifications
You must be signed in to change notification settings - Fork 418
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
use statement replaces all symbols in current scope? #12744
Comments
I believe this is as intended, though I agree that there's a learning curve here. While there are aspects of procedures that respect time (e.g., the order in which their statements are executed), there are aspects that are independent of time, specifically declarations. For example, given this code (Try it online): module M {
var x = 42;
proc foo() {
writeln(x);
var x = "hello";
writeln(x);
}
} it may appear (based on C-like languages, e.g.) that foo() should write out 42 and then "hello" but because Chapel treats declarations as applying to the whole scope (even in procedures), the original writeln(x) is also referring to the declaration of x and so is illegal (since the local scope's x hasn't been declared and initialized yet), and the compiler flags it as such. Similarly, procedures nested within procedures can be called whether they appear before or after the call to the procedure (try it online): proc foo() {
proc bar() { writeln("In bar"); }
bar(); // OK
}
proc baz() {
bar(); // also OK
proc bar() { writeln("In bar"); }
} I believe Though there's admittedly a learning curve here, I have to say I really like this consistency in Chapel, both from a user's and an implementer's point of view (i.e., unlike some choices we've made, I haven't spent a lot of time second-guessing it). I suspect there is more we could do though to help users get surprised by this, for example, in the way of compiler warnings, overload sets (#12635), and/or adding more of a Python-like use-nothing-by-default |
Related #6872 #11262 #12734 #10021 #9985
Summary of Problem
The following code behaves weirdly, but is correct based on the defined Language Specification.
From the Language Specification, 11.11:
From Brad's comment in #9985:
I understand the principle of Brad's comment, but this code is weird because the use statement is able to replace symbols that chronologically occurred before the use statement in the procedure. If the enclosing scope of the use statement was a module definition, class definition, or some other scope where declaration order didn't matter, then this would be less of an issue, but inside a scope that exhibits a notion of time, it doesn't feel correct even if the behavior is.
Possible solutions:
Edit: I don't know if 3 is possible if
use
ing a module and then doing a bunch of dynamic casts that could depend on said module. The interplay of compile-time symbol visibility vs. runtime dynamic casting might be the breaking point?The text was updated successfully, but these errors were encountered: