-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@scala.annotation.preview
annotation and -preview
flag.
- Loading branch information
1 parent
7441791
commit cb46a43
Showing
20 changed files
with
287 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,38 @@ | ||
--- | ||
layout: doc-page | ||
title: "Preview Definitions" | ||
nightlyOf: https://docs.scala-lang.org/scala3/reference/other-new-features/preview-defs.html | ||
--- | ||
|
||
The [`@preview`](https://scala-lang.org/api/3.x/scala/annotation/preview.html) annotation allows the definition of an API that is not guaranteed backward binary, but might become stable in next minor version of the compiler. | ||
|
||
New Scala language features or standard library APIs initially introduced as experimental can become a preview features when they have become fully implemented and acceppted by the [SIP](https://docs.scala-lang.org/sips/) before they're accepted as standard features. | ||
Such definitions can be used by early adopters that can accept possibility of binary compatibility breakage, for example these can be used for project internal tools and applications, but are discouraged to be used by libraries. | ||
|
||
The [`@preview`](https://scala-lang.org/api/3.x/scala/annotation/preview.html) definitions follows similar rules as the [`@experimental`](https://scala-lang.org/api/3.x/scala/annotation/experimental.html) - to enable access to preview feature or API in given compilation unit Scala compiler requires either: | ||
|
||
- explicit `-preview` flag passed to the compiler, | ||
- top level import for explicit `scala.language.preview.<feature>`, | ||
- annotating defintion that referes to preview feature with `@preview` | ||
|
||
The biggest difference of preview features when compared with experimental features is their non-viral behaviour. | ||
Any defintion that was compiles in the preview scope (using `-preview` flag or `scala.language.preview` top-level import) is not annotated as `@preview` defintion itself. It behaviour allows to use preview features transitively in other compilation units without enabled preview mode. | ||
|
||
```scala | ||
//> using options -preview | ||
import scala.annotation.preview | ||
|
||
@preview def previewFeature: Unit = () | ||
|
||
// Can be used in non-preview scope | ||
def usePreviewFeature = previewFeature | ||
``` | ||
|
||
```scala | ||
def usePreviewFeatureTransitively = usePreviewFeature | ||
def usePreviewFeatureDirectly = previewFeature // error - refering to preview definition outside preview scope | ||
def useWrappedPreviewFeature = wrappedPreviewFeature // error - refering to preview definition outside preview scope | ||
|
||
@scala.annotation.preview | ||
def wrappedPreviewFeature = previewFeature | ||
``` |
This file contains 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,9 @@ | ||
package scala.annotation | ||
|
||
/** An annotation that can be used to mark a definition as preview. | ||
* | ||
* @see [[https://dotty.epfl.ch/docs/reference/other-new-features/preview-defs]] | ||
* @syntax markdown | ||
*/ | ||
final class preview(message: String) extends StaticAnnotation: | ||
def this() = this("") |
This file contains 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 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,27 @@ | ||
-- Error: tests/neg/preview-message.scala:15:2 ------------------------------------------------------------------------- | ||
15 | f1() // error | ||
| ^^ | ||
| method f1 is marked @preview | ||
| | ||
| Preview definition may only be used under preview mode: | ||
| 1. in a definition marked as @preview, or | ||
| 2. a preview feature is imported at the package level, or | ||
| 3. compiling with the -preview compiler flag. | ||
-- Error: tests/neg/preview-message.scala:16:2 ------------------------------------------------------------------------- | ||
16 | f2() // error | ||
| ^^ | ||
| method f2 is marked @preview | ||
| | ||
| Preview definition may only be used under preview mode: | ||
| 1. in a definition marked as @preview, or | ||
| 2. a preview feature is imported at the package level, or | ||
| 3. compiling with the -preview compiler flag. | ||
-- Error: tests/neg/preview-message.scala:17:2 ------------------------------------------------------------------------- | ||
17 | f3() // error | ||
| ^^ | ||
| method f3 is marked @preview: not yet stable | ||
| | ||
| Preview definition may only be used under preview mode: | ||
| 1. in a definition marked as @preview, or | ||
| 2. a preview feature is imported at the package level, or | ||
| 3. compiling with the -preview compiler flag. |
This file contains 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,17 @@ | ||
|
||
|
||
import scala.annotation.preview | ||
|
||
@preview | ||
def f1() = ??? | ||
|
||
@preview() | ||
def f2() = ??? | ||
|
||
@preview("not yet stable") | ||
def f3() = ??? | ||
|
||
def g() = | ||
f1() // error | ||
f2() // error | ||
f3() // error |
This file contains 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,6 @@ | ||
//> using options -preview | ||
import scala.annotation.preview | ||
|
||
@preview def previewFeature = 42 | ||
|
||
def usePreviewFeature = previewFeature |
Oops, something went wrong.