Skip to content

Revising the Definition of “Variable Reference” #270

Closed
@RexJaeschke

Description

@RexJaeschke

Introduction

Recently, I’ve been working on the specs for a family of additions in V7, and that has caused me to review the way we’re spec’ing some things currently, with regard to argument passing.

For the purpose of this discussion, let’s assume the following working definitions (for terms that we are not currently using in the spec):

  • A ByVal operation results in the creation of a value.
  • A ByRef operation results in the creation of an alias for a variable.

As V7 (see later below) adds a bunch of new ByRef scenarios, I’ve been looking for a unified approach to describe such operations, with the intent to retrofit it back to the existing spec as well. One of my goals was to not overload any further the word reference (which is already used in unrelated context such as reference type, reference variable, as well as an English synonym for designate).

I thought about—and abandoned—introducing terms like ByVal and ByRef. However, ByVal scenarios are quite simple, easily understandable, and don’t need any extra spec’ing for V7. It’s ByRef that I think needs some work. (BTW, I’m also trying to avoid using the term alias in a ByRef context, using denoting or designating instead.)

As best as I can tell, we already have a ByRef-like term, variable reference, with corresponding grammar (see below), so why not use that in both new text and in more existing places, rather than describing the related behavior in multiple places?

The Current Spec for Variable References

Here’s what we have today:

10.5 Variable references
A variable_reference is an expression that is classified as a variable. A variable_reference denotes a storage location that can be accessed both to fetch the current value and to store a new value.

variable_reference
    : expression
    ;

Note: In C and C++, a variable_reference is known as an lvalue. end note

Shortcomings in the Existing Definition

Before I talk about “improving” this definition for V7, let’s consider if it actually suffices for what we have in V5. The definition says that a variable reference is read/write. But that isn’t always the case. For example:

  • Surely a readonly field is a variable reference, but it can be written to by a constructor, so in some contexts it’s read/write and in others, read-only.
  • When a read/write variable is passed as an out parameter, it is read-only until it is definitely assigned, after which it is read/write.

As to the note, it’s not quite correct. When the standards committee added the const qualifier to C in C89, they had to break lvalues into two categories: modifiable lvalues and non-modifiable lvalues. As such, the use of lvalue in the note really means modifiable lvalue.

As this is the only place in the spec that we use lvalue, I suggest that rather than correct the note, we delete it all together.

What’s Coming in V7

Here are the ByRef scenarios added by V7

  • Input parameters (which are passed ByRef)
  • Methods/delegates that return ByRef
  • ByRef local variables
  • Conditional expressions having a ByRef result
  • ByRef assignment

Add to that the ability to use ref readonly in some of these places, and we have some new read-only variable references. Also, an input parameter is implicitly a read-only ByRef.

Proposed Spec for Variable References

A variable_reference is an expression that is classified as a variable. A variable_reference denotes a storage location that, depending on the context in which the variable_reference is used, may be readable, read-write, or writeable. In the writable case, the location may become readable as well after the first write to it.

variable_reference
    : expression
    ;

Multiple variable_references may denote the same location.

[[[For V7, this would be extended to allow a read-only variable reference to denote a read/write location, so some variable references would have write access to it while others wouldn’t.]]]

What Else in V6 is Impacted by the Proposed Change?

Some current uses of variable reference will need, or benefit from, minor tweaking. For example,

15.6.2.3 Reference parameters

Change

A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference parameter does not create a new storage location. Instead, a reference parameter represents the same storage location as the variable given as the argument in the method invocation.

to

A parameter declared with a ref modifier is a reference parameter. Unlike a value parameter, a reference parameter does not create a new storage location. Instead, a reference parameter is a variable_reference that denotes the variable given as the argument in the method invocation.

And change

When a formal parameter is a reference parameter, the corresponding argument in a method invocation shall consist of the keyword ref followed by a variable_reference

to

When a formal parameter is a reference parameter, the corresponding argument in a method invocation shall consist of the keyword ref followed by a read/write variable_reference

Currently, the term variable reference and the grammar rule variable_reference are not widely used, so the tweaks will be few.

Impact on V7 ref-Related Work

Going this route will allow me to use read/write variable reference or read-only variable reference, as appropriate, without adding other terms.

Metadata

Metadata

Assignees

Labels

meeting: discussThis issue should be discussed at the next TC49-TG2 meeting

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions