// stdlib에 있는 대표적인 제네릭 함수인 filter
inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
val destination = ArrayList<T>
for (element in this) {
if (predicate(element)) {
destination.add(element)
}
}
return destination
}
List<String>
, Set<User>
// 특정 타입의 스브타입만 허용
fun <T : Comparable<T>> Iterable<T>.sorted(): List<T> {
/*...*/
}
Any
로 제한하면 된다.inline fun <T, R : Any> Iterable<T>.mapNotNull(transform: (T) -> R?): List<R> {
return mapNotNullTo(ArrayList<R>(), transform)
}
fun <T : Animal> pet(animal: T) where T: GoodTempered {
/*...*/
}
// 또는
fun <T> pet(animal: T) where T: Animal, T: GoodTempered {
/*...*/
}