1.kotlin和java一样支持类型参数:
class Box<T>(t: T) {
var value = t
}
你可以像下面这样创建实例,你需要提供类型参数:
val box: Box<Int> = Box<Int>(1)
假如类型参数是可以推断的,比如根据构造函数的参数等等,类型参数是可以忽略的,如下所示:
val box = Box(1) // 1 has type Int, so the compiler figures out that we are talking about Box<Int>
2.型变
Java 类型系统中最棘手的部分之一是通配符类型,如果不了解java通配符的同学可以先了解 java通配符的使用,而在kotlin中没有通配符,为了解决java中遇到的问题,提出了另外两个东西:声明处型变(declaration-site variance)与类型投影(type projections)
3.声明处型变
声明处形变out、in
out协变的类型参数示例:
open class Animal {
}
open class Suckler: Animal() {
}
class Dog: Suckler() {
}
class Source<out T> {
fun get():T? {
return null
}
//out 声明的类型参数不能作为参数传入,否则编译不通过
// fun add(t: T) {
//
// }
//只能将Suckler或其子类赋值给t
//好比我有一个只能往外取Suckler的箱子,你把这个箱子换成装Dog的箱子也是没问题的,因为你取出来的Dog可以当Suckler使用
fun match(source: Source<Dog>) {
var t: Source<Suckler> = source
// val dog: Dog = t.get()
}
//out声明的泛型只能将String或者子类赋值给t,下面编译不通过
// fun mismatch(tree: Source<Animal>) {
// var t: Source<Dog> = tree
// }
}
- out修饰的类型参数是生产者如Source<out T>,只允许从source中返回T(即生产),而不能添加(作为参数传入)
- 可以将子类赋值给父类
- Array<out Any>相当于java的Array<? extends Object>
in:逆变的类型参数示例:
class Sink<in T> {
fun add(t: T) {
}
//in修饰的类型参数不能返回,否则会编译不通过
// fun get() : T? = null
//只能将Suckler类型或其父类赋值给 t
//好比我有一个只能往里面放Suckler的箱子,你把它换成可以装Animal的箱子也是没问题的,因为Suckler和Dog都可以往里面放
fun match(sink: Sink<Animal>) {
var t: Sink<Suckler> = sink
t.add(Suckler())
t.add(Dog())
// t.add(Animal)
}
//编译不通过,报Type mismatch
// fun dismiss(router: Sink<Suckler>) {
// var t: Sink<Animal> = router
// }
}
- 和out相反in修饰的参数类型只能被消费(可以作为参数传入)而不可以被生产(返回)
- 可以将父类赋值给子类
- Array<in String> 相当于java的 Array<? super String>
4.使用处型变
考虑下面的问题:
class Array<T>(val size: Int) {
fun get(index: Int): T { ///* …… */ }
fun set(index: Int, value: T) { ///* …… */ }
}
fun copy(from: Array<Any>, to: Array<Any>) {
assert(from.size == to.size)
for (i in from.indices)
to[i] = from[i]
}
这个函数应该将项目从一个数组复制到另一个数组。让我们尝试在实践中应用它:
val ints: Array<Int> = arrayOf(1, 2, 3)
val any = Array<Any>(3) { "" }
copy(ints, any) // 错误:期望 (Array<Any>, Array<Any>)
我们可以在使用初形变解决这个问题:
fun copy(from: Array<out Any>, to: Array<Any>) {
// ......
}
类型投影:这里的 from
不仅仅是一个数组,而是一个受限制的(投影 的)数组。
或者使用 in
做类型投影:
fun fill(dest: Array<in String>, value: String) {
// ......
}
Array<in String>
对应于 Java 的 Array<? super String>
,也就是说,你可以传递一个 CharSequence
数组或一个 Object
数组给 fill()
函数。
5. 星投影
class Bar<in T, out U>() {
fun add(t: T) {
}
fun get(): U? {
return null
}
}
fun test0(bar: Bar<*, Suckler>) {
//添加不了,因为我不知道你的类型是什么
// bar.add(Animal())
}
fun test1(bar: Bar<Suckler, *>) {
bar.add(Suckler())
bar.add(Dog())
bar.get()
}
fun test2(bar: Bar<*, *>) {
//添加不了,因为我不知道你的类型是什么
// bar.add(Animal())
bar.get()
}
你对类型参数一无所知,但仍然希望以安全的方式使用它。 这里的安全方式是 定义泛型类型的这种投影,该泛型类型的每个具体实例化将是该投影的子类型。
星投影语法:
假设类型被声明 为 interface Function <in T, out U>
,我们可以想象以下星投影:
-
Function<*, String>
表示Function<in Nothing, String>
; -
Function<Int, *>
表示Function<Int, out Any?>
; -
Function<*, *>
表示Function<in Nothing, out Any?>
。
6.泛型函数
fun <T> singletonList(item: T): List<T> {
// ......
}
fun <T> T.basicToString() : String { // 扩展函数
// ......
}
val l = singletonList<Int>(1)
7.泛型约束
fun <T : Comparable<T>> sort(list: List<T>) {
// ......
}
冒号之后指定的类型是上界:只有Comparable<T>
的子类型可以替代 T 。
8.具体化的类型参数
内联函数支持具体化的类型参数。下面看看我们不使用具体化的类型参数的情况:
fun <T> TreeNode.findParentOfType(clazz: Class<T>): T? {
var p = parent
while (p != null && !clazz.isInstance(p)) {
p = p.parent
}
@Suppress("UNCHECKED_CAST")
return p as T?
}
treeNode.findParentOfType(MyTreeNode::class.java)
上面的代码使用了扩展函数的语法,如对扩展函数不熟悉,请查看扩展函数的语法。
让我们看看下面使用具体化的类型参数的例子:
inline fun <reified T> TreeNode.findParentOfType(): T? {
var p = parent
while (p != null && p !is T) {
p = p.parent
}
return p as T?
}
myTree.findParentOfType<MyTreeNodeType>()
我们使用 reified 修饰符来限定类型参数,现在可以在函数内部访问它了, 几乎就像是一个 普通的类一样。由于函数是内联的,不需要反射,正常的操作符如 !is 和 as 现在都能用了。如对内联函数不熟悉,请查看内联函数的语法。