Commit 12764e2
committed
Make type of type hole available quoted patterns
In general, instances of `scala.quoted.Type` are and should not be used explicitly in quotes.
```diff
+ def f[T](x: T)(using Type[T])(using QuoteContext) =
+ '{ val y: T = $x; ... } // good code
- def f[T](x: T)(using t: Type[T])(using QuoteContext) =
- '{ val y: t.T = $x; ... } // bad code
- def f[T](x: T)(using t: Type[T])(using QuoteContext) =
- '{ val y: $t = $x; ... } // bad code (syntax will be removed)
```
The only exception is on types extracted using quoted patterns as these hide tier type inside an instance of `Type[T]` which must be accessed using it's path.
```diff
??? match
case '{ $x: $t } =>
- '{ val y: t.T = $x; ... } // bad code
- '{ val y: $t = $x; ... } // bad code (syntax will be removed)
```
Here `t` is provided as a `given` as if we refer to its type we must be able to summon it.
The change is to provide the name of the type that is extracted rather than the `given Type[T]`.
`Type[T]` can be summoned were needed as in all other uses of abstract types in quotes.
```diff
??? match
case '{ $x: $T } =>
+ '{ val y: T = $x; ... } // good code
- val t: Type[T] = summon[Type[T]]
- '{ val y: t.T = $x; ... } // bad code
- '{ val y: $t = $x; ... } // bad code (syntax will be removed)
```
The pattern will make `T` and a `given Type[T]` available on the right-hand side of the pattern.
The `T` can be used the same way it could be used in the implementation of a method with signature:
```scala
def f[T](x: T)(using Type[T])(using QuoteContext) =
'{ val y: T = $x; ... }
```
```scala
case '{ $x: $T } =>
'{ val y: T = $x; ... }
```
It is also possible to use lower cases for the type splices `case '{ $x: $t } => '{ val y: t = $x; ... }`.
This is similar to the use of type variables in normal patterns `case Some[t](x) =>`.
It is preferable to use upper cases as it makes it clearer that this is a type and renders better on the uses in the right-hand side of the pattern.1 parent 3107214 commit 12764e2
File tree
24 files changed
+97
-93
lines changed- community-build/community-projects
- compiler/src/dotty/tools/dotc
- core
- typer
- tests
- neg-macros
- pos-macros
- tasty-constant-type
- run-macros
- flops-rewrite-3
- flops-rewrite
- i7987
- i8007
- quote-matcher-symantics-3
- quote-matcher-type-bind
- quote-type-matcher-2
- refined-selectable-macro
- string-context-implicits
- run-staging
24 files changed
+97
-93
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
| 323 | + | |
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
Lines changed: 13 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
159 | | - | |
160 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
161 | 163 | | |
162 | 164 | | |
163 | | - | |
| 165 | + | |
164 | 166 | | |
165 | 167 | | |
166 | 168 | | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
171 | | - | |
| 173 | + | |
172 | 174 | | |
173 | 175 | | |
174 | 176 | | |
| |||
214 | 216 | | |
215 | 217 | | |
216 | 218 | | |
217 | | - | |
| 219 | + | |
218 | 220 | | |
219 | 221 | | |
220 | 222 | | |
| |||
263 | 265 | | |
264 | 266 | | |
265 | 267 | | |
266 | | - | |
| 268 | + | |
| 269 | + | |
267 | 270 | | |
268 | 271 | | |
269 | 272 | | |
270 | 273 | | |
271 | 274 | | |
272 | | - | |
| 275 | + | |
273 | 276 | | |
274 | 277 | | |
275 | 278 | | |
| |||
305 | 308 | | |
306 | 309 | | |
307 | 310 | | |
308 | | - | |
| 311 | + | |
309 | 312 | | |
310 | 313 | | |
311 | 314 | | |
312 | 315 | | |
313 | | - | |
314 | | - | |
| 316 | + | |
315 | 317 | | |
316 | 318 | | |
317 | 319 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
0 commit comments