Skip to content

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 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class ScalaSettings extends Settings.SettingGroup {
val YshowSuppressedErrors: Setting[Boolean] = BooleanSetting("-Yshow-suppressed-errors", "Also show follow-on errors and warnings that are normally suppressed.")
val YdetailedStats: Setting[Boolean] = BooleanSetting("-Ydetailed-stats", "show detailed internal compiler stats (needs Stats.enabled to be set to true).")
val Yheartbeat: Setting[Boolean] = BooleanSetting("-Yheartbeat", "show heartbeat stack trace of compiler operations (needs Stats.enabled to be set to true).")
val YkindProjector: Setting[Boolean] = BooleanSetting("-Ykind-projector", "allow `*` as wildcard to be compatible with kind projector")
val YprintPos: Setting[Boolean] = BooleanSetting("-Yprint-pos", "show tree positions.")
val YprintPosSyms: Setting[Boolean] = BooleanSetting("-Yprint-pos-syms", "show symbol definitions positions.")
val YnoDeepSubtypes: Setting[Boolean] = BooleanSetting("-Yno-deep-subtypes", "throw an exception on deep subtyping call stacks.")
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ object StdNames {
val _21 : N = "_21"
val _22 : N = "_22"

val * : N = "*"
val ? : N = "?"
val ??? : N = "???"

val genericWrapArray: N = "genericWrapArray"
Expand Down
12 changes: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,21 @@ object Parsers {
SingletonTypeTree(literal(negOffset = start))
}
else if (in.token == USCORE) {
if (ctx.settings.strict.value) {
deprecationWarning(em"`_` is deprecated for wildcard arguments of types: use `?` instead")
patch(source, Span(in.offset, in.offset + 1), "?")
}
val start = in.skipToken()
typeBounds().withSpan(Span(start, in.lastOffset, start))
}
else if (isIdent(nme.?)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be tpnme ?

Copy link
Contributor Author

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.

val start = in.skipToken()
typeBounds().withSpan(Span(start, in.lastOffset, start))
}
else if (isIdent(nme.*) && ctx.settings.YkindProjector.value) {
syntaxError("`*` placeholders are not implemented yet")
typeIdent()
}
else if (isSplice)
splice(isType = true)
else path(thisOK = false, handleSingletonType) match {
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ class CompilationTests extends ParallelTesting {
defaultOptions),
compileFile("tests/neg-custom-args/i6300.scala", allowDeepSubtypes),
compileFile("tests/neg-custom-args/infix.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings")),
compileFile("tests/neg-custom-args/missing-alpha.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings"))
compileFile("tests/neg-custom-args/missing-alpha.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings")),
compileFile("tests/neg-custom-args/wildcards.scala", defaultOptions.and("-strict", "-deprecation", "-Xfatal-warnings"))
).checkExpectedErrors()
}

Expand Down
43 changes: 43 additions & 0 deletions docs/docs/reference/changed-features/wildcards.md
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`.
2 changes: 2 additions & 0 deletions docs/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ sidebar:
url: docs/reference/changed-features/structural-types.html
- title: Operators
url: docs/reference/changed-features/operators.html
- title: Wildcard Types
url: docs/reference/changed-features/wildcards.html
- title: Type Checking
url: docs/reference/changed-features/type-checking.html
- title: Type Inference
Expand Down
4 changes: 4 additions & 0 deletions tests/neg-custom-args/wildcards.scala
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
}
4 changes: 4 additions & 0 deletions tests/pos/wildcards.scala
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()
}