Is there a way to have a union of `protected` and ...
# compiler
e
Is there a way to have a union of
protected
and
internal
visibility? Pretty sure the answer is no, but just checking. Are there any proposals for adding something like that? Use case is where I want to invoke an abstract function from a class in the same module, and have subclasses implement them.
a
the current closest equivalent is a custom OptIn annotation https://kotlinlang.org/docs/opt-in-requirements.html#require-opt-in-for-api then at least any usage needs to be explicit hmmm actually, maybe that’s not suitable for your use case
e
Copy code
abstract class Foo {
    abstract protected fun foo()

    companion object {
        internal fun Foo.foo() = foo()
    }
}
Copy code
import Foo.Companion.foo
super ugly, but assuming this is what you mean by "union"… this will allow
internal
, non-derived callers to use the extension
Foo.foo
, while derived callers use can use the
protected foo
.
e
mind blown
y
You could even do:
Copy code
abstract class Foo {
  abstract protected fun foo()
  final internal fun foo(unit: Unit = Unit) = this.foo()
}
(Haven't tested it out but I think it should work) it's slightly neater in that no import is required at the call site, but the unit parameter (which is needed for disambiguation) is annoying.