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

docs: more scope lawyering (re #40238) #40394

Merged
merged 1 commit into from
Apr 8, 2021
Merged

Conversation

StefanKarpinski
Copy link
Sponsor Member

@StefanKarpinski StefanKarpinski commented Apr 7, 2021

Much more than this and we're writing a book about how lexical scope works in general rather than a chapter about how it works in Julia specifically. Closes #40238.

@mbauman
Copy link
Sponsor Member

mbauman commented Apr 7, 2021

I've long thought about completely re-imagining this entire chapter... and completely removing the word "variable" in favor of "name." I've found the latter vocabulary very helpful in teaching beginners Julia's behaviors, particularly when it comes to argument passing. I've found "variable" comes along with heavy baggage of a "box" mental model (that's often somewhat broken) and is rather ill-placed in Julia where we can't take the address of a binding or pass by reference to a name.

@StefanKarpinski
Copy link
Sponsor Member Author

StefanKarpinski commented Apr 8, 2021

Well, I fear that this text is now mind-numbingly complete. I have a hard time imagining that people can read this and understand what it means but at least it doesn't gloss over any details.

@StefanKarpinski
Copy link
Sponsor Member Author

StefanKarpinski commented Apr 8, 2021

I feel like a document that explains what scope is and how to think about it might be what most people need. They're not actually confused about Julia's scope rules so much as the concept of scope in general. The confusion about Julia's scope rules is just the local manifestation of that general confusion applied to the language that they happen to be currently trying to program in.

@heliosdrm
Copy link
Contributor

About the last new sentence ("which may surprise users coming from Python where assignment in an inner function creates a new local unless the variable is explictly declared to be non-local"): this might be removed from here to make it a bit shorter, and add a comment about it in the section of Noteworthy differences from Python

@StefanKarpinski
Copy link
Sponsor Member Author

I agree this isn't the most appropriate place to mention differences from Python (why only Python and not other languages?), but the reason I included that comment was that the first part of the sentence seemed poorly motivated otherwise: i.e. why is it worth noting that "this applies to variables assigned in inner functions"? It's only worth noting if some people might expect otherwise, and people would only expect otherwise if they have experience with languages where that's not the case, in which case it seems perverse to not mention at least one such language by name.

@StefanKarpinski
Copy link
Sponsor Member Author

Added a variant of sum_to_def with the loop body implemented as a closure to demonstrate that the local scope of an inner function behaves the same way as any other inner local scope.

@StefanKarpinski StefanKarpinski added the status:merge me PR is reviewed. Merge when all tests are passing label Apr 8, 2021
@StefanKarpinski StefanKarpinski merged commit b539198 into master Apr 8, 2021
@StefanKarpinski StefanKarpinski deleted the sk/scope-lawyering branch April 8, 2021 21:08
@StefanKarpinski StefanKarpinski added domain:docs This change adds or pertains to documentation and removed status:merge me PR is reviewed. Merge when all tests are passing labels Apr 8, 2021
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing documentation for scope rules.
3 participants