Kotlin:如何调用扩展方法的超级实现
Kotlin: How to call super implementation of an extension method
在 Kotlin 中,我有如下代码:
open class A {
protected open fun B.doSomething() {}
}
class B {}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
print("now try to call super implementation. How?")
super.doSomething() // does not compile
super<A>.doSomething() // does not compile
super<B>.doSomething() // does not compile
}
}
而且我想知道,在这种情况下如何调用 doSomething() 的超类实现?或者根本不可能在 kotlin 中为扩展函数调用超级实现?
此问题已在 KT-11488 中报告。目前不可能。 Kotlin 规范说 super
-form 调用的候选成员是通过以下方式找到的:
For a callable f
with an explicit basic super-form receiver super
in a
classifier declaration with supertypes A1, A2, … , AN the following
sets are considered for non-emptiness:
- Non-extension member callables named
f
of type A1
;
- Non-extension member callables named
f
of type A2
;
- …;
- Non-extension member callables named
f
of type AN
.
If at least two of these sets are non-empty, this is a compile-time error. Otherwise, the non-empty set (if any) is analyzed as usual.
For a callable f
with an explicit extended super-form receiver super<A>
the following sets are analyzed (in the given order):
- Non-extension member callables named
f
of type A
.
如您所见,仅考虑“非扩展成员可调用文件”。完全不考虑扩展功能。
现在,您可以像这样解决(票证中也建议类似的方法):
open class A {
protected open fun B.doSomething() =
bDoSomething(this)
// make a non-extension function to put A's implementation in there
protected fun bDoSomething(receiver: B) {
print("A implementation")
}
}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
super.bDoSomething(this) // call the non-extension function
}
}
在 Kotlin 中,我有如下代码:
open class A {
protected open fun B.doSomething() {}
}
class B {}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
print("now try to call super implementation. How?")
super.doSomething() // does not compile
super<A>.doSomething() // does not compile
super<B>.doSomething() // does not compile
}
}
而且我想知道,在这种情况下如何调用 doSomething() 的超类实现?或者根本不可能在 kotlin 中为扩展函数调用超级实现?
此问题已在 KT-11488 中报告。目前不可能。 Kotlin 规范说 super
-form 调用的候选成员是通过以下方式找到的:
For a callable
f
with an explicit basic super-form receiversuper
in a classifier declaration with supertypes A1, A2, … , AN the following sets are considered for non-emptiness:
- Non-extension member callables named
f
of typeA1
;- Non-extension member callables named
f
of typeA2
;- …;
- Non-extension member callables named
f
of typeAN
.If at least two of these sets are non-empty, this is a compile-time error. Otherwise, the non-empty set (if any) is analyzed as usual.
For a callable
f
with an explicit extended super-form receiversuper<A>
the following sets are analyzed (in the given order):
- Non-extension member callables named
f
of typeA
.
如您所见,仅考虑“非扩展成员可调用文件”。完全不考虑扩展功能。
现在,您可以像这样解决(票证中也建议类似的方法):
open class A {
protected open fun B.doSomething() =
bDoSomething(this)
// make a non-extension function to put A's implementation in there
protected fun bDoSomething(receiver: B) {
print("A implementation")
}
}
class C : A() {
override fun B.doSomething() {
print("first, do some C-specific logic")
super.bDoSomething(this) // call the non-extension function
}
}