|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: Wildcard Arguments in Types |
| 4 | +--- |
| 5 | + |
| 6 | +The syntax of wildcard arguments in types has changed from `_` to `?`. Example: |
| 7 | +```scala |
| 8 | +List[?] |
| 9 | +Map[? <: AnyRef, ? >: Null] |
| 10 | +``` |
| 11 | + |
| 12 | +### Motivation |
| 13 | + |
| 14 | +We would like to use the underscore syntax `_` to stand for an anonymous type parameter, aligning it with its meaning in |
| 15 | +value parameter lists. So, just as `f(_)` is a shorthand for the lambda `x => f(x)`, in the future `C[_]` will be a shorthand |
| 16 | +for the type lambda `[X] =>> C[X]`. This makes higher-kinded types easier to use. It also removes the wart that, used as a type |
| 17 | +parameter, `F[_]` means `F` is a type constructor whereas used as a type, `F[_]` means it is a wildcard (i.e. existential) type. |
| 18 | +In the future, `F[_]` will mean the same thing, no matter where it is used. |
| 19 | + |
| 20 | +We pick `?` as a replacement syntax for wildcard types, since it aligns with Java's syntax. |
| 21 | + |
| 22 | +### Migration Strategy |
| 23 | + |
| 24 | +The migration to the new scheme is complicated, in particular since the [kind projector](https://github.com/typelevel/kind-projector]) |
| 25 | +compiler plugin still uses the reverse convention, with `?` meaning parameter placeholder instead of wildcard. Fortunately, kind projector has added `*` as an alternative syntax for `?`. |
| 26 | + |
| 27 | +A step-by-step migration is made possible with the following measures: |
| 28 | + |
| 29 | + 1. In Scala 3.0, both `_` and `?` are legal names for wildcards. |
| 30 | + 2. In Scala 3.1, `_` is deprecated in favor of `?` as a name for a wildcard. A `-rewrite` option is |
| 31 | + available to rewrite one to the other. |
| 32 | + 3. In Scala 3.2, the meaning of `_` changes from wildcard to placeholder for type parameter. |
| 33 | + 4. The Scala 3.1 behavior is already available today under the `-strict` setting. |
| 34 | + |
| 35 | +To smooth the transition for codebases that use kind-projector, we adopt the following measures under the command line |
| 36 | +option `-Ykind-projector`: |
| 37 | + |
| 38 | + 1. In Scala 3.0, `*` is available as a type parameter placeholder. |
| 39 | + 2. In Scala 3.2, `*` is deprecated in favor of `_`. A `-rewrite` option is |
| 40 | + available to rewrite one to the other. |
| 41 | + 3. In Scala 3.3, `*` is removed again, and all type parameter placeholders will be expressed with `_`. |
| 42 | + |
| 43 | +These rules make it possible to cross build between Scala 2 using the kind projector plugin and Scala 3.0 - 3.2 using option `-Ykind-projector`. |
0 commit comments