-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use ?
for wildcards
#6610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Use ?
for wildcards
#6610
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: doc-page | ||
title: Wildcard Arguments in Types | ||
--- | ||
|
||
The syntax of wildcard arguments in types has changed from `_` to `?`. Example: | ||
```scala | ||
List[?] | ||
Map[? <: AnyRef, ? >: Null] | ||
``` | ||
|
||
### Motivation | ||
|
||
We would like to use the underscore syntax `_` to stand for an anonymous type parameter, aligning it with its meaning in | ||
value parameter lists. So, just as `f(_)` is a shorthand for the lambda `x => f(x)`, in the future `C[_]` will be a shorthand | ||
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 | ||
parameter, `F[_]` means `F` is a type constructor whereas used as a type, `F[_]` means it is a wildcard (i.e. existential) type. | ||
In the future, `F[_]` will mean the same thing, no matter where it is used. | ||
|
||
We pick `?` as a replacement syntax for wildcard types, since it aligns with Java's syntax. | ||
|
||
### Migration Strategy | ||
|
||
The migration to the new scheme is complicated, in particular since the [kind projector](https://github.com/typelevel/kind-projector]) | ||
compiler plugin still uses the reverse convention, with `?` meaning parameter placeholder instead of wildcard. Fortunately, kind projector has added `*` as an alternative syntax for `?`. | ||
|
||
A step-by-step migration is made possible with the following measures: | ||
|
||
1. In Scala 3.0, both `_` and `?` are legal names for wildcards. | ||
2. In Scala 3.1, `_` is deprecated in favor of `?` as a name for a wildcard. A `-rewrite` option is | ||
available to rewrite one to the other. | ||
3. In Scala 3.2, the meaning of `_` changes from wildcard to placeholder for type parameter. | ||
4. The Scala 3.1 behavior is already available today under the `-strict` setting. | ||
|
||
To smooth the transition for codebases that use kind-projector, we adopt the following measures under the command line | ||
option `-Ykind-projector`: | ||
|
||
1. In Scala 3.0, `*` is available as a type parameter placeholder. | ||
2. In Scala 3.2, `*` is deprecated in favor of `_`. A `-rewrite` option is | ||
available to rewrite one to the other. | ||
3. In Scala 3.3, `*` is removed again, and all type parameter placeholders will be expressed with `_`. | ||
|
||
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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
object Test { | ||
val xs: List[_] = List(1, 2, 3) // error | ||
val ys: Map[_ <: AnyRef, _ >: Null] = Map() // error // error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
object Test { | ||
val xs: List[?] = List(1, 2, 3) | ||
val ys: Map[? <: AnyRef, ? >: Null] = Map() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be tpnme ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the idents coming form the token stream are always TermIdents.