Skip to content

Static Property Variables #195

@AnthonyDGreen

Description

@AnthonyDGreen

Some set of people have asked us to enable fields scoped to a property. VB already has Static local variables, which as effectively fields scoped to their containing method. For lazily computed read-only variables this already enables one to write code where the backing store of a ReadOnly property is entirely local to that property. We could extend this to come up with a syntax for property scoped fields:

Property P As T
    Static F As T
    Get
        Return F
    End Get
    Set(value As T)
        F = value
    End Set
End Property

This works around a syntactic ambiguity with Dim since Dim could also be the start of a field declaration:

Property P As T
' Parser decided this was an auto-prop because next statement wasn't a Get or Set.
Dim F As T

' Parser starts to flip out right about here.
    Get
        Return F
    End Get
    Set(value As T)
        F = value
    End Set
End Property

We could still work around this without the special syntax with "statement" look-ahead, whereby the parser keeps accumulating field declarations until it hits either an accessor or something which can't be in a property. If it hits a property accessor it consumes all the field statements as its children. If it hits something else if closes itself and leaves all those field declarations "around" for the class to pick up as its children. This sounds a little messy but it's actually an extremely unlikely scenario to happen in practice. If, however, we elected to allow other modifiers like Private the scenario becomes more common, though still solvable. Either way, the multi-statement "look-ahead" isn't wasteful since those statements were going to be parsed as fields anyway. We don't need to throw away any work and re-parse.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions