extension-function

Kotlin Extension Functions Databinding

我们两清 提交于 2020-01-12 06:31:50
问题 Is there any possibility to use extension function with a databinding? XML: <data> <import type="my.package.domain.country.model.City.streetName" /> <variable name="city" type="my.package.domain.country.model.City" /> </data> <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{city.street.streetName()}" /> my.package.domain.country.model.city data class City( val id: String, val street: Street ) fun City.streetName():

Can extension functions be called in a “static” way?

隐身守侯 提交于 2020-01-01 11:25:38
问题 Is it possible to create an extension function and call it as if it were static ? For Example... fun System.sayByeAndExit() { println("Goodbye!") System.exit() } fun main(args: Array<String>) { System.sayByeAndExit() // I'd like to be able to call this } I know that the code sample doesn't work... I understand that kotlin's extension functions are resolved statically , as mentioned in the Kotlin Reference (Extension Functions) , but this does not mean they can be called as if they were static

How can I inline the receiver parameter of an extension function in Kotlin?

删除回忆录丶 提交于 2019-12-22 08:29:26
问题 Inspired by this question, I was thinking about how could one inline the receiver parameter of an extension function? In theory, like this: inline fun <T> not(crossinline predicate : (T) -> Boolean) = { e : T -> !predicate(e) } Just that predicate becomes our receiver function: operator inline fun <T> ((T) -> Boolean).not() = { e : T -> !this(e) } Now, for the above code, I'd expect the compiler to complain that it needs crossinline ; however, I get the following warning: Warning: Expected

overloading + and += operators for “Number Classes”

喜夏-厌秋 提交于 2019-12-20 20:16:12
问题 I want to create extension functions for classes that encapsulate simple Number s. For example DoubleProperty . I encountered the problem, that I can't overload the + and the += operator at the same time. I wan't to create a behaviour, that passes following tests: class DoublePropertyTest { lateinit var doubleProperty: DoubleProperty @Before fun initialize() { doubleProperty = SimpleDoubleProperty(0.1) } @Test fun plus() { val someProperty = doubleProperty + 1.5 assertEquals(someProperty

Is there a way to show all the extension functions of a given Kotlin class in Intellij IDE?

微笑、不失礼 提交于 2019-12-13 03:22:58
问题 The only way I've found so far is to create an instance of the given class and then using the autocomplete to see all possible functions. Obviously, this way is cumbersome and takes too much time. Is there a neater way to see all possible functions? 回答1: If you select the class and right-click and select Find Usages then select Group by type , it will show you usages where the class is Extension receiver type (make sure Group by test/production is unticked if you want to see all the possible

Testing extension functions inside classes

倾然丶 夕夏残阳落幕 提交于 2019-12-08 19:55:35
问题 If we want to test an extension function on a type, we can create an instance of this type, call the function and check the returned value. But what about testing extension functions defined inside classes? abstract class AbstractClass<T> { fun doStuff(): T = "Hello".foo() abstract fun String.foo(): T } class SubClass1: AbstractClass<Int>() { override fun String.foo(): Int = 1 } class SubClass2: AbstractClass<Boolean>() { override fun String.foo(): Boolean = true } How do we test the logic of

Can extension functions be called in a “static” way?

喜你入骨 提交于 2019-12-04 07:45:24
Is it possible to create an extension function and call it as if it were static ? For Example... fun System.sayByeAndExit() { println("Goodbye!") System.exit() } fun main(args: Array<String>) { System.sayByeAndExit() // I'd like to be able to call this } I know that the code sample doesn't work... I understand that kotlin's extension functions are resolved statically , as mentioned in the Kotlin Reference (Extension Functions) , but this does not mean they can be called as if they were static functions within a class (in a Java sense). I also understand that this code will not work because

Kotlin Extension Functions Databinding

左心房为你撑大大i 提交于 2019-12-03 09:55:00
Is there any possibility to use extension function with a databinding? XML: <data> <import type="my.package.domain.country.model.City.streetName" /> <variable name="city" type="my.package.domain.country.model.City" /> </data> <TextView android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{city.street.streetName()}" /> my.package.domain.country.model.city data class City( val id: String, val street: Street ) fun City.streetName(): String = street.houseNumber Error [kapt] An exception occurred: android.databinding.tool.util

overloading + and += operators for “Number Classes”

假装没事ソ 提交于 2019-12-03 06:28:59
I want to create extension functions for classes that encapsulate simple Number s. For example DoubleProperty . I encountered the problem, that I can't overload the + and the += operator at the same time. I wan't to create a behaviour, that passes following tests: class DoublePropertyTest { lateinit var doubleProperty: DoubleProperty @Before fun initialize() { doubleProperty = SimpleDoubleProperty(0.1) } @Test fun plus() { val someProperty = doubleProperty + 1.5 assertEquals(someProperty.value, 1.6, 0.001) } @Test fun plusAssign() { val someProperty = doubleProperty doubleProperty += 1.5 /

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

▼魔方 西西 提交于 2019-11-28 18:22:50
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. 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 { println(it) } // Output "hello!" You can use let for null safety check: val myVar = if (Random().nextBoolean