extension-function

difference between kotlin also, apply, let, use, takeIf and takeUnless in Kotlin

ぃ、小莉子 提交于 2019-11-27 11:08:23
问题 I read many Kotlin documents about these items. But I can't understand so clearly. What is the use of Kotlin let , also , takeIf and takeUnless in detail? I need an example of each item. Please don't post the Kotlin documentation. I need a real-time example and use cases of these items. 回答1: let public inline fun <T, R> T.let(block: (T) -> R): R = block(this) Take the receiver and pass it to a function passed as a parameter. Return the result of the function. val myVar = "hello!" myVar.let {

Accessing Kotlin extension functions from Java

家住魔仙堡 提交于 2019-11-27 06:42:37
Is it possible to access extension functions from Java code? I defined the extension function in a Kotlin file. package com.test.extensions import com.test.model.MyModel /** * */ public fun MyModel.bar(): Int { return this.name.length() } Where MyModel is a (generated) java class. Now, I wanted to access it in my normal java code: MyModel model = new MyModel(); model.bar(); However, that doesn't work. The IDE won't recognize the bar() method and compilation fails. What does work is using with a static function from kotlin: public fun bar(): Int { return 2*2 } by using import com.test

Accessing Kotlin extension functions from Java

守給你的承諾、 提交于 2019-11-26 10:28:44
问题 Is it possible to access extension functions from Java code? I defined the extension function in a Kotlin file. package com.test.extensions import com.test.model.MyModel /** * */ public fun MyModel.bar(): Int { return this.name.length() } Where MyModel is a (generated) java class. Now, I wanted to access it in my normal java code: MyModel model = new MyModel(); model.bar(); However, that doesn\'t work. The IDE won\'t recognize the bar() method and compilation fails. What does work is using