templates - Kotlin:具有通用约束的委托
问题描述
我试图找出语法来声明一个具有类型限制的委托类。例如,这是有效的语法:
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> by innerList
但现在我想强制执行T
extendsClassFoo
和 implements 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
类并在派生类上设置界限吗?
解决方案
第一个版本是正确的,但你需要where
换一个新行(可能是一个错误)
open class Cell<T>(val innerList: MutableList<T> = mutableListOf()) : MutableList<T> by innerList
where T : ClassFoo, T : InterfaceBar
推荐阅读
- ssh - Plink 命令在哪里查找私钥?
- pytest - pytest/mixer Django - 夹具从另一个模型继承时忽略字段值
- java - 如何在 EditText 中添加数字分隔符
- java - 如何将两个或多个字符串数组列表的排列组合创建为一个组合列表
- laravel - 在 Laravel 5.7 中修改 getRouteKeyName
- javascript - Phaser3 + ES6 类:如何保持范围从创建到更新
- html - 如何在 HTML 中以列方式组合两个框架并使其成为一个可滚动页面?
- vuejs2 - 封装使用类语法的 vue 组件
- laravel - laravel vuejs/axios put request Formdata 为空
- python - 如何找到最优化的 if 语句序列?