-
Notifications
You must be signed in to change notification settings - Fork 91
Variables and Variable References #324
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
Comments
Use of “lvalue”Proposal 2: As this is the only place in the spec that we use lvalue, rather than correcting the note (because it ignores the C89-invented distinction between modifiable and non-modifiable lvalues), we should delete it. |
Classified as a variableIn the current V6 draft spec, we use the phrase “is [not] classified as a variable” numerous times, all in the context of an expression. Some are declarative statements; others are qualifiers of the form “If is classified as a variable …” Proposal 3 (initial): Replace all these occurrences with “is [not] a variable_reference.” After all, that’s exactly what a variable reference is! |
Variable10 Variables|10.1 General says
Operators needing write accessProposal 4 (initial): Rewrite 12.7.10 Postfix increment and decrement operators
as
Likewise, for 12.8.6 Prefix increment and decrement operators, and 12.18 Assignment operators|12.18.1 General. |
My experimentIgnoring the While 12.7.10 Postfix increment and decrement operators, 12.8.6 Prefix increment and decrement operators, 12.18.2 Simple assignment, and 12.18.3 Compound assignment, explicitly require property and indexer operands to have a setter, there is no stated requirement that a variable/variable_reference operand be writable! So, that needs to be addressed. Variable reference (revisited)I don’t see any way that the spec requires a variable to be physically write-protected. (Afterall, a readonly field can be written to by a constructor!) However, the spec can allow us to add write-protection when accessing a variable, via a variable_reference. When C89 (the first C standard) borrowed the type qualifier Proposal 5: Change 10.5 Variable references from
to
|
Proposal 3 (final): Re the use of “classified as a variable,” all these occurrences should be replaced with “is a [modifiable|non-modifiable] variable_reference, as appropriate.” |
Operators needing write access (revisited)Proposal 4 (final): 12.7.10 Postfix increment and decrement operators
12.8.6 Prefix increment and decrement operators
12.18 Assignment operators|12.18.1 General
|
Readonly Fields15.5 Fields|15.5.1 General says
Proposal 6: Change this to
|
Other places that may need editsProposal 7: Change 10 Variables|10.1 General Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed through assignment or through use of the ++ and -- operators. To Variables represent storage locations. Every variable has a type that determines what values can be stored in the variable. C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type. The value of a variable can be changed via a modifiable variable_reference to that variable, through assignment or through use of the ++ and -- operators. |
Proposal 8: Change 10.2 Variable categories|10.2.1 General Add here something like “Except for fields marked |
Still to comeI have not done an exhaustive search for all places that would be impacted by these changes, but can do that if/when we agree on these approaches. Once we have the two flavors of variable_reference defined and use them correctly throughout the exiting spec, I can go back to the V7 feature specs and use the new terminology, as appropriate for the new |
Regarding:
I follow the motivation but I'm not sure this works/reads correctly. Consider from §12.7.8 This access:
Here we have Both being "is classified as" (as now) or "is a" is at least more consistent. |
The term "variable reference" is used here, in others the grammar rule variable_reference is used. I suggest the former should be a defined term (and then its read-only/writable variants) and should probably be used in many of the cases variable_reference is used below as the text is about things which are semantically variable references not the grammar rule. |
Would it be possible to show a v7 example of how these changes help? |
Nigel, off the top of my head, here's an example involving 12.15 Conditional operatorconditional_expression
: null_coalescing_expression
| null_coalescing_expression '?' 'ref'? expression ':' 'ref'? expression
;
... If
|
On the call of Jul 2, I took an action item to draft a PR. However, after considerable deliberation, I think I’ll withdraw the issue instead. Here’s my rationale: A. Of late, my main thrust has been to deal with the supposed hole of not prohibiting attempts to modify a readonly field. Certainly, this is not prohibited directly by words in the sections covering ++, --, and assignment (all of which do, however, directly require both get and set accessors for properties and indexers).
But this only about non-readonly fields.
But this only talks about direct assignments, which I’m not sure includes ++/--, although one could deduce that it is intended to include them. In any event, I don’t plan on offering any word improvements here, but I do acknowledge that my initial assumption of the “problem” was wrong. B. I’ve spec’d most of the ref-related features for V7 without using terms like modifiable variable reference and non-modifiable variable reference. C. Looking at 12.8.6 Prefix increment and decrement operators
Using my previous thinking, it was a small step to adjusting this to say quite explicitly
Where a modifiable property access and a modifiable indexer access referred to those having setters as well as getters. And the next step would be to consider the idea that a property or indexer access really designates a (pseudo) variable, so why not include them in the definition of variable reference? This would result in
The one problem with that is that the address-of operator cannot be applied to a property or indexer access, but it can be to a variable. But I think that would be resolvable by a constraint on &. Bottom line, while I still think my proposal to split variable references into modifiable and non-modifiable groups has merit, at this stage of the spec’s life that seems like mostly an “issue of elegance” rather than something that solves a real problem for the V7 additions. As such, I suggest closing this issue. Then when we get to reviewing the V7 ref-like features, we can revisit this topic, if necessary. |
On the June 30 call, we agreed to suspend discussion on this for now, and possibly reconsider when we get to V7. Changed the milestone to v7. |
See #927 (comment) as relevant to some of the discussion. |
This Issue replaces Issue #270, ‘Revising the Definition of “Variable Reference”.’
I’ve put each proposal in a separate Comment, so you can record a thumb-up/down for each.
Defining two kinds of variable references
The key to my proposals below is to have two flavors of variable reference: modifiable and non-modifiable, so we can adequately spec the following V7 features:
Variable reference
10.5 Variable references says
Proposal 1: We should use the term “variable reference” (rather than “variable”) in all contexts involving an expression that denotes a variable.
The text was updated successfully, but these errors were encountered: