You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Deprecate experimental language import
- Make named tuple features conditional on -source >= 3.6 instead
- Make the NamedTuple object non-experimental.
- Move NamedTuple it to src-bootstrapped since it relies on clause
interleaving which is only standard in 3.6 as well.
- Drop the experimental.namedTuple import from tests
The elements of a tuple can now be named. Example:
7
+
Starting in Scala 3.6, the elements of a tuple can be named. Example:
8
8
```scala
9
9
typePerson= (name: String, age: Int)
10
10
valBob:Person= (name ="Bob", age =33)
@@ -94,6 +94,24 @@ Bob match
94
94
case (age = x, name = y) => ...
95
95
```
96
96
97
+
### Pattern Matching with Named Fields in General
98
+
99
+
We allow named patterns not just for named tuples but also for case classes. For instance:
100
+
```scala
101
+
city match
102
+
case c @City(name ="London") => println(c.population)
103
+
caseCity(name = n, zip =1026, population = pop) => println(pop)
104
+
```
105
+
106
+
Named constructor patterns are analogous to named tuple patterns. In both cases
107
+
108
+
- every name must match the name some field of the selector,
109
+
- names can come in any order,
110
+
- not all fields of the selector need to be matched.
111
+
112
+
Named patterns are compatible with extensible pattern matching simply because
113
+
`unapply` results can be named tuples.
114
+
97
115
### Expansion
98
116
99
117
Named tuples are in essence just a convenient syntax for regular tuples. In the internal representation, a named tuple type is represented at compile time as a pair of two tuples. One tuple contains the names as literal constant string types, the other contains the element types. The runtime representation of a named tuples consists of just the element values, whereas the names are forgotten. This is achieved by declaring `NamedTuple`
@@ -119,6 +137,47 @@ The translation of named tuples to instances of `NamedTuple` is fixed by the spe
119
137
- All tuple operations also work with named tuples "out of the box".
120
138
- Macro libraries can rely on this expansion.
121
139
140
+
### Computed Field Names
141
+
142
+
The `Selectable` trait now has a `Fields` type member that can be instantiated
143
+
to a named tuple.
144
+
145
+
```scala
146
+
traitSelectable:
147
+
typeFields<:NamedTuple.AnyNamedTuple
148
+
```
149
+
150
+
If `Fields` is instantiated in a subclass of `Selectable` to some named tuple type,
151
+
then the available fields and their types will be defined by that type. Assume `n: T`
152
+
is an element of the `Fields` type in some class `C` that implements `Selectable`,
153
+
that `c: C`, and that `n` is not otherwise legal as a name of a selection on `c`.
154
+
Then `c.n` is a legal selection, which expands to `c.selectDynamic("n").asInstanceOf[T]`.
155
+
156
+
It is the task of the implementation of `selectDynamic` in `C` to ensure that its
157
+
computed result conforms to the predicted type `T`
158
+
159
+
As an example, assume we have a query type `Q[T]` defined as follows:
The `NamedTuple` object contains a type definition
@@ -137,33 +196,36 @@ then `NamedTuple.From[City]` is the named tuple
137
196
(zip: Int, name: String, population: Int)
138
197
```
139
198
The same works for enum cases expanding to case classes, abstract types with case classes as upper bound, alias types expanding to case classes
140
-
and singleton types with case classes as underlying type.
199
+
and singleton types with case classes as underlying type (in terms of the implementation, the `classSymbol` of a type must be a case class).
141
200
142
201
`From` is also defined on named tuples. If `NT` is a named tuple type, then `From[NT] = NT`.
143
202
144
203
204
+
### Operations on Named Tuples
205
+
206
+
The operations on named tuples are defined in object [scala.NamedTuple](https://www.scala-lang.org/api/3.x/scala/NamedTuple$.html).
207
+
145
208
### Restrictions
146
209
147
-
The following restrictions apply to named tuple elements:
210
+
The following restrictions apply to named tuples and named pattern arguments:
148
211
149
-
1. Either all elements of a tuple are named or none are named. It is illegal to mix named and unnamed elements in a tuple. For instance, the following is in error:
212
+
1. Either all elements of a tuple or constructor pattern are named or none are named. It is illegal to mix named and unnamed elements in a tuple. For instance, the following is in error:
150
213
```scala
151
214
valillFormed1= ("Bob", age =33) // error
152
215
```
153
-
2. Each element name in a named tuple must be unique. For instance, the following is in error:
216
+
2. Each element name in a named tuple or constructor pattern must be unique. For instance, the following is in error:
154
217
```scala
155
218
valillFormed2= (name ="", age =0, name =true) // error
156
219
```
157
-
3. Named tuples can be matched with either named or regular patterns. But regular tuples and other selector types can only be matched with regular tuple patterns. For instance, the following is in error:
220
+
3. Named tuples and case classes can be matched with either named or regular patterns. But regular tuples and other selector types can only be matched with regular tuple patterns. For instance, the following is in error:
158
221
```scala
159
222
(tuple: Tuple) match
160
223
case (age = x) =>// error
161
224
```
162
-
4. Regular selector names `_1`, `_2`, ... are not allowed asnames in named tuples.
225
+
##SyntaxChanges
163
226
164
-
###Syntax
165
-
166
-
The syntax of Scala is extended asfollows to support named tuples:
227
+
The syntax of Scala is extended asfollows to support named tuples and
0 commit comments