-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add @scala.annotation.internal.preview
annotation and -preview
flag.
#22317
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b05c8a7
Add `@scala.annotation.preview` annotation and `-preview` flag.
WojciechMazur 30a4f60
Adjust MiMa filters
WojciechMazur 0ab56bd
Make preview `private[scala]`, enable all preview features globally u…
WojciechMazur fd67fc1
Fix typos
WojciechMazur 18ac691
Fix typos and address review sugesions
WojciechMazur 12526fb
Fix more typos
WojciechMazur 7356a2c
Merge branch 'main' into feature/preview
WojciechMazur 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
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,34 @@ | ||
--- | ||
layout: doc-page | ||
title: "Preview Definitions" | ||
nightlyOf: https://docs.scala-lang.org/scala3/reference/other-new-features/preview-defs.html | ||
--- | ||
|
||
New Scala language features or standard library APIs are initially introduced as experimental, but once they become fully implemented and acceppted by the [SIP](https://docs.scala-lang.org/sips/) these can become a preview features. | ||
Preview language features and APIs are guaranteed to be standarized in some next Scala minor release, but allow compiler team to introduce small, possibly binary incompatible, changes based on the community feedback. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
These can be used by early adopters who can accept possibility of binary compatibility breakage. As an example these can be used for project internal tools and applications, but are discouraged to be used by libraries. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Users can enable access to preview features and definitions by compiling with `-preview` flag. The flag would enable all preview features and definitions. There is no way for enabling only a subset of preview features. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The biggest difference of preview features when compared with experimental features is their non-viral behaviour. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Any defintion compiled in the preview mode (using `-preview` flag) is not marked as preview defintion itself. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This behaviour allows to use preview features transitively in other compilation units without explicitlly enabled preview mode, as long as it does not directly reference APIs or features marked as preview. | ||
|
||
The [`@preview`](https://scala-lang.org/api/3.x/scala/annotation/internal/preview.html) annotations are used to mark Scala 3 standard library APIs currently available under enabled preview mode. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The definitions follows similar rules as the [`@experimental`](https://scala-lang.org/api/3.x/scala/annotation/experimental.html) when it comes to accessing, subtyping, overriding or overloading definitions marked with this annotation - all of these can only be performed in compilation unit that enables preview mode. | ||
WojciechMazur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```scala | ||
//> using options -preview | ||
package scala.stdlib | ||
import scala.annotation.internal.preview | ||
|
||
@preview def previewFeature: Unit = () | ||
|
||
// Can be used in non-preview scope | ||
def usePreviewFeature = previewFeature | ||
``` | ||
|
||
```scala | ||
def usePreviewFeatureTransitively = scala.stdlib.usePreviewFeature | ||
def usePreviewFeatureDirectly = scala.stdlib.previewFeature // error - refering to preview definition outside preview scope | ||
``` |
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,11 @@ | ||
package scala.annotation | ||
package internal | ||
|
||
|
||
/** 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 | ||
*/ | ||
private[scala] final class preview(message: String) extends StaticAnnotation: | ||
def this() = this("") |
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,18 @@ | ||
-- Error: tests/neg/preview-message.scala:15:2 ------------------------------------------------------------------------- | ||
15 | f1() // error | ||
| ^^ | ||
| method f1 is marked @preview | ||
| | ||
| Preview definition may only be used when 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 when 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 when compiling with the `-preview` compiler flag |
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,17 @@ | ||
package scala // @preview is private[scala] | ||
|
||
import scala.annotation.internal.preview | ||
|
||
@preview | ||
def f1() = ??? | ||
|
||
@preview() | ||
def f2() = ??? | ||
|
||
@preview("not yet stable") | ||
def f3() = ??? | ||
|
||
def g() = | ||
f1() // error | ||
f2() // error | ||
f3() // 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,7 @@ | ||
//> using options -preview | ||
package scala // @preview is private[scala] | ||
import scala.annotation.internal.preview | ||
|
||
@preview def previewFeature = 42 | ||
|
||
def usePreviewFeature = previewFeature |
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,2 @@ | ||
def usePreviewFeatureTransitively = scala.usePreviewFeature | ||
def usePreviewFeatureDirectly = scala.previewFeature // 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,13 @@ | ||
package scala // @preview is private[scala] | ||
|
||
import scala.annotation.internal.preview | ||
|
||
trait A: | ||
def f: Int | ||
def g: Int = 3 | ||
trait B extends A: | ||
@preview | ||
def f: Int = 4 // error | ||
|
||
@preview | ||
override def g: Int = 5 // 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,41 @@ | ||
package scala // @preview is private[scala] | ||
|
||
import scala.annotation.internal.preview | ||
|
||
@preview | ||
class A: | ||
def f() = 1 | ||
|
||
@preview | ||
class B extends A: | ||
override def f() = 2 | ||
|
||
class C: | ||
@preview | ||
def f() = 1 | ||
|
||
class D extends C: | ||
override def f() = 2 | ||
|
||
trait A2: | ||
@preview | ||
def f(): Int | ||
|
||
trait B2: | ||
def f(): Int | ||
|
||
class C2 extends A2, B2: | ||
def f(): Int = 1 | ||
|
||
def test: Unit = | ||
val a: A = ??? // error | ||
val b: B = ??? // error | ||
val c: C = ??? | ||
val d: D = ??? | ||
val c2: C2 = ??? | ||
a.f() // error | ||
b.f() // error | ||
c.f() // error | ||
d.f() // ok because D.f is a stable API | ||
c2.f() // ok because B2.f is a stable API | ||
() |
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,18 @@ | ||
//> using options -preview | ||
package scala // @preview is private[scala] | ||
import scala.annotation.internal.preview | ||
|
||
@preview def previewDef: Int = 42 | ||
|
||
class Foo: | ||
def foo: Int = previewDef | ||
|
||
class Bar: | ||
def bar: Int = previewDef | ||
object Bar: | ||
def bar: Int = previewDef | ||
|
||
object Baz: | ||
def bar: Int = previewDef | ||
|
||
def toplevelMethod: Int = previewDef |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.