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
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.
0 commit comments