Skip to content

Commit

Permalink
OptIn: KT-48890 related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mglukhikh authored and elizarov committed Jun 27, 2022
1 parent f89a3e2 commit 05b1279
Showing 1 changed file with 1 addition and 29 deletions.
30 changes: 1 addition & 29 deletions proposals/opt-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,8 @@ fun foo(s: String) {}
## OptIn markers and overridden declarations

As a general rule, marker annotation should present either at base and overridden declarations together,
or at none of them.
or at an overridden declaration only, or at none of them.
Using marker on base declaration only provokes error or warning on overridden declaration, depending on marker `RequiresOptIn.level`.
Using marker on overridden declaration only is normally forbidden (see `bar` example below)
because compiler usually cannot guarantee that exactly overridden function is called (see `base.bar` call).
The exception to this rule is the situation when base class has the same marker,
in this case it's allowed to have the same marker on overridden declaration without marker on base declaration.
The reason of this exception is marker contagiousness in lexical scopes (see chapter about it below).

```kotlin
open class Base {
Expand All @@ -213,8 +208,6 @@ open class Base {
@ShinyNewAPI
open fun foooo() {}

open fun bar() {}

@ShinyNewAPI
open fun baz() {}
}
Expand All @@ -226,30 +219,9 @@ class Derived {
@OptIn(ShinyNewAPI::class)
override fun foooo() {} // OK!

@ShinyNewAPI
override fun bar() {} // ERROR! Base declaration isn't annotated

// Overriding experimental declaration
override fun baz() {} // ERROR/WARNING (depending on ShinyNewAPI level)
}

fun use(base: Base) {
base.bar() // Is it experimental or not?
}
```

Exception example
```kotlin
@ShinyNewAPI
open class Base {
// No marker here
open fun foo() {}
}
@OptIn(ShinyNewAPI::class)
class Derived : Base() {
@ShinyNewAPI // Ok
override fun foo() {}
}
```

## OptIn marker contagiousness (type usages)
Expand Down

0 comments on commit 05b1279

Please sign in to comment.