Document grammar of field and associated constant decls (#4980)

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
This commit is contained in:
Geoff Romer
2025-02-20 01:28:01 +00:00
committed by GitHub
co-authored by josh11b
parent 5b30888dd8
commit 09b06b4234
2 changed files with 34 additions and 7 deletions
+25 -7
View File
@@ -30,6 +30,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Assignment and initialization](#assignment-and-initialization)
- [Operations performed field-wise](#operations-performed-field-wise)
- [Nominal class types](#nominal-class-types)
- [Fields](#fields)
- [Forward declaration](#forward-declaration)
- [`Self`](#self)
- [Construction](#construction)
@@ -688,9 +689,13 @@ The declarations for nominal class types will have:
- a sequence of declarations
- `}`, a close curly brace
Declarations should generally match declarations that can be declared in other
contexts, for example variable declarations with `var` will define
[instance variables](https://en.wikipedia.org/wiki/Instance_variable):
Declarations within a class should generally have the same syntax as
declarations that occur in other contexts. For example, member functions are
introduced with `fn`.
### Fields
Fields of a nominal class type are declared with `var`:
```
class TextLabel {
@@ -701,10 +706,23 @@ class TextLabel {
}
```
The main difference here is that `"default"` is a default instead of an
initializer, and will be ignored if another value is supplied for that field
when constructing a value. Defaults must be constants whose value can be
determined at compile time.
Notice that this is subtly different from the meaning of `var` in other
contexts: it declares an
[instance variable](https://en.wikipedia.org/wiki/Instance_variable)), not just
a variable in the class's scope.
> **Open question:** Is there a way to declare a variable in a class's scope?
In a field declaration, an initializer (such as `= "default"` above) specifies
the default value of the field, and will be ignored if another value is supplied
for that field when constructing an instance of the class. Defaults must be
constants whose value can be determined at compile time.
The pattern in a field declaration must be a run-time binding pattern, so the
full syntax is:
_field-declaration_ ::= `var` _identifier_ `:` _expression_ [ `=` _expression_
] `;`
### Forward declaration
+9
View File
@@ -2004,6 +2004,15 @@ interface NSpacePoint {
}
```
The pattern of an associated constant declaration must be a symbolic binding
pattern, and unlike other `let` declarations, an associated constant declaration
cannot have an initializer unless it's
[preceded by `default`](#interface-defaults):
_associated-constant-decl_ ::= `let` _identifier_ `:!` _expression_ `;`
_associated-constant-decl_ ::= `default` `let` _identifier_ `:!` _expression_ =
_expression_ `;`
An implementation of an interface specifies values for associated constants with
a [`where` clause](#where-constraints). For example, implementations of
`NSpacePoint` for different types might have different values for `N`: