我重构了这段代码:
remark.observeOn(mainThread())
.subscribe { remark ->
remark_box.visible = remark.isNotEmpty()
remark_tv.text = remark
}
.addTo(CompositeDisposable())使用这个扩展,但我迷失了什么是: Disposable where T : Observable<String>请谁能解释一下吗?
remark.bindTo(remark_tv)
.addTo(CompositeDisposable())和
fun <T> T.bindTo(textView: TextView): Disposable where T : Observable<String> {
return observeOn(mainThread())
.subscribe { remark ->
textView.text = remark
}
}发布于 2020-02-19 21:48:46
: Disposable表示您的函数的返回类型为Disposable。
where T : Observable<String>部件在generic类型参数T上指定upper bound。简单地说,这意味着T必须是Observable<String>的subtype。
使用generic types时,可以将单个upper bound指定为
// This function can only be called with types implementing Collection<Int>
fun <T: Collection<Int>> someFun(first: T, second: T)
// If you try to call it as following, it will not compile
someFun(listOfStrings, listOfStrings)但是,如果您需要指定多个upper bounds where clause,则必须使用作为
// This function can only be called with types implementing Iterable<Int>
// as well as Serializable
fun <T> someFun(first: T, second: T): Int where T: Iterable<Int>,T: Serializable
// This does not work, you can not comma separate the upper bounds
fun <T: Iterable<Int>, Serializable> someFun(first: T, second: T): Int正如documentation所说的
在尖括号内只能指定一个上限。如果同一类型参数需要多个上限,则需要单独的where子句。
https://stackoverflow.com/questions/60301617
复制相似问题