【发布时间】:2020-03-15 12:40:43
【问题描述】:
我正在尝试找出声明一个具有类型限制的委托类的语法。例如,这是有效的语法:
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> by innerList
但现在我想强制 T 扩展 ClassFoo 并实现 InterfaceBar。 where 关键字应该放在哪里?
以下是我尝试过的一些无法编译的东西:
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> by innerList where T : ClassFoo, T : InterfaceBar
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) where T : ClassFoo, T : InterfaceBar : MutableList<T> by innerList
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> where T : ClassFoo, T : InterfaceBar by innerList
这些限制与委派不兼容吗?换句话说,我应该只扩展我的Cell 类并在派生类上设置边界吗?
【问题讨论】:
标签: templates generics kotlin typename