-
Android的KTX实现了Kotlin中的reified type parameters,这样一来,就可以不用再传递class的参数给我们的函数,比如:
// the old way var alarmManager = context.getSystemService(AlarmManager::class.java) // the reified way var alarmManager: AlarmManager = context.systemService() // or var alarmManager = context.systemService<AlarmManager>() // the magic from Android KTX... with "real" type T inline fun <reified T> Context.systemService() = getSystemService(T::class.java) //previously 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? } // call treeNode.findParentOfType(MyTreeNode::class.java) // actually we want pass a type to this funciton, like: treeNode.findParentOfType<MyTreeNode>() // enable this, try use reified modifier inline fun <reified T> TreeNode.findParentOfType(): T? { var p = parent while (p != null && p !is T) { p = p.parent } return p as T? }
Inline Functions and Reified Type Parameters - Kotlin Programming Languagekotlinlang.org
-
通过关键字“by”可以将你的工作委托给另外一个class,在前面系列的lazy中其实已经提到过了。
class MyAnimatingView: View(/*...*/) { // delegated property. Uses the getter and setter defined in InvalidateDelegeate var foregroundX by InvalidateDelegate(0f) } // A View Delegate which invalidates View.postInvalidateOnAnimation when set. class InvalidateDelegate<T: Any>(var value: T) { operator fun getValue(thisRef: View, property: KProperty<*>) = value operator fun setValue(thisRef: View, property: KProperty<*>, value: T) { this.value = value this.Ref.postInvalidateOnAnimation() } }
Delegated Properties - Kotlin Programming Languagekotlinlang.org
-
No more Util classes。可以通过“extension functions”实现。在你想要添加方法的前面放上class的名字即可。比如:
// Exten String with toUri inline fun String.toUri(): Uri = Uri.parse(this) // And call it on any String! val myUri = "www.developer.android.com".toUri()
android/android-ktxandroid-ktx - A set of Kotlin extensions for Android app development.github.com
-
在Android里面如果我们要实现一个从Drawable转换到Bitmap,就会需要一系列的步骤。但Android KTX提供了一些方法,让你的代码更加的简洁。(可以查看graphics package)
// get a drawable from resources val myDrawable = ContextCompat.getDrawable(context, R.drawable.icon) //convert the drawable to a bitmap val bitmap = myDrawable.toBitmap()
android/android-ktxandroid-ktx - A set of Kotlin extensions for Android app development.github.com
-
Sequence和Iterator很像,但Sequence其实perform lazily当你需要的时候,比如调的它的时候,但是Iterator更像是马上执行返回另外一个Iterable,如:
// Sequence<T>.map { ... } val seq = sequenceOf(1, 2) val seqMapped: Sequence<Int> = seq.map { print("$it "); it * it } // intermediate print("before sum ") val sum = seqMapped.sum() // terminal // prints: // before sum 1 2
// Iterable<T>.map { ... } val lst = listOf(1, 2) val lstMapped: List<Int> = lst.map { print("$it "); it * it } print("before sum ") val sum = lstMapped.sum() // prints: // 1 2 before sum
val sequence = List(50) {it * 5}.asSequence sequence.map {it * 2} // lazy(iterate 1 element at a time) .filter {it % 3 == 0} // lazy(iterate 1 element at a time) .map {it + 1} // lazy(iterate 1 element at a time) .toList {} // eager (iterate all elemnts)
kotlin.sequences - Kotlin Programming Languagekotlinlang.org
31DaysOfKotlin-series 4
于 2018-04-18 20:02:52 首次发布