Skip to content

Commit 59d1209

Browse files
authored
Merge pull request #369 from ejller/nekrasova/subclassOptInRequired
Update proposals/subclass-opt-in-required.md
2 parents 9ae3362 + 12a44f2 commit 59d1209

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

proposals/subclass-opt-in-required.md

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,64 @@ Other alternatives are:
107107

108108
`SubclassOptInRequired` was chosen as the most appropriate and likely the most familiar for developers
109109
to grasp from at first glance.
110+
111+
### SubclassOptInRequired marker contagiousness (lexical scopes)
112+
113+
`SubclassOptInRequired` is not propagated to inner and nested classes. Opt-in is required only when inheriting from a class on which the `SubclassOptInRequired` annotation has been explicitly specified.
114+
115+
```kotlin
116+
@RequiresOptIn
117+
annotation class API
118+
119+
@SubclassOptInRequired(API::class)
120+
open class A {
121+
open class B
122+
}
123+
124+
class C1: A() // opt-in required
125+
class C2 : A.B() // no opt-in required
126+
```
127+
128+
### Interaction with Java code
129+
130+
Since the Kotlin compiler can't report errors or warnings in Java code, adding the opt-in is not required for the Java classes or interfaces.
131+
132+
```kotlin
133+
// a.kt
134+
@RequiresOptIn
135+
annotation class API
136+
137+
@SubclassOptInRequired(API::class)
138+
open class KotlinCl
139+
140+
// b.java
141+
public class Foo extends KotlinCl {} // no opt-in required
142+
143+
```
144+
Also, Java code suppresses the propagation of opt-in requirements. Therefore, if a class in Kotlin inherits from the Java class `Foo`, opt-in is not required for the inheritance.
145+
146+
```kotlin
147+
// c.kt
148+
class Bar: Foo() //no opt-in required
149+
```
150+
151+
To propagate experimentation through Java code, it is required to explicitly use the `SubclassOptInRequired` annotation in Java code.
152+
153+
```kotlin
154+
// a.kt
155+
@RequiresOptIn
156+
annotation class API
157+
158+
@SubclassOptInRequired(API::class)
159+
open class KotlinCl
160+
161+
// b.java
162+
@SubclassOptInRequired(API::class)
163+
public class Foo extends KotlinCl {}
164+
165+
// c.kt
166+
class Bar: Foo() // opt-in required
167+
```
110168

111169
### Restrictions and limitations
112170

@@ -128,6 +186,6 @@ opting-in into extension and opting-in into overall uses.
128186

129187
### Status and timeline
130188

131-
The feature is available since Kotlin 1.8.0 as experimental (it itself requires an opt-in
189+
The feature is available since Kotlin 2.0.0 as experimental (it itself requires an opt-in
132190
into `ExperimentalSubclassOptIn`)
133-
and is expected to be promoted to stable in Kotlin 1.9.0.
191+
and is expected to be promoted to stable in Kotlin 2.1.0.

0 commit comments

Comments
 (0)