kotlin-extension

How to check “instanceof ” class in kotlin?

こ雲淡風輕ζ 提交于 2019-12-01 02:02:33
In kotlin class, I have method parameter as object (See kotlin doc here ) for class type T . As object I am passing different classes when I am calling method. In Java we can able to compare class using instanceof of object which class it is. So I want to check and compare at runtime which Class it is? How can I check instanceof class in kotlin? nhaarman Use is . if (myInstance is String) { ... } or the reverse !is if (myInstance !is String) { ... } Combining when and is : when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) } copied from official

Kotlin extension clash

拈花ヽ惹草 提交于 2019-12-01 00:30:22
问题 If I have a jar, on the classpath, where I've created an extension function on say the String class for argument's sake and I have another jar with the same extension function on String, how will Kotlin resolve the two? I presume if both functions are defined in the same packages then there will be a clash? But if different packages, how I can distinguish the two extensions? 回答1: Indeed, if they're in the same package, it won't compile. For the other scenario, let's say you have two files

Cannot resolve string supplied to vararg parameter in extension function

这一生的挚爱 提交于 2019-11-30 19:36:49
strings.xml <string name="my_string">Showing your number: %1$s</string> ActivityExt.kt fun Activity.showToast(textResId: Int, vararg formatArgs: String) { val text = getString(textResId, formatArgs) Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } MainActivity.kt val number = 11 showToast(R.string.my_string, number.toString()) Toast with following text is showing: Showing your number: [Ljava.lang.String;@2cfa3b] Why this happens? Use the spread operator: fun Activity.showToast(textResId: Int, vararg formatArgs: String) { val text = getString(textResId, *formatArgs) Toast.makeText(this,

Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, element: E): Unit defined in java.util.ArrayList'

不打扰是莪最后的温柔 提交于 2019-11-30 11:14:21
I have this snippets: class RecyclerViewAdapter internal constructor( val clazz: Class<out RecyclerViewViewHolder>, val layout: Int, var dataList: MutableList<*>) ... ... ... fun RecyclerView.getDataList() : ArrayList<*> { return (adapter as RecyclerViewAdapter).dataList as ArrayList<*> } ... ... ... then I use that on this: recyclerView.getDataList().add(Person("Lem Adane", "41 years old", 0)) but I get this error: Error:(19, 31) Out-projected type 'ArrayList<*>' prohibits the use of 'public open fun add(index: Int, element: E): Unit defined in java.util.ArrayList' Kotlin star-projections are

Should we avoid naming a function same as an existing class in Kotlin? Why?

此生再无相见时 提交于 2019-11-30 08:59:28
问题 Kotlin allows to name a function same as an existing class, e.g. HashSet with initializer function could be implemented like this: fun <T> HashSet(n : Int, fn: (Int) -> T) = HashSet<T>(n).apply { repeat(n) { add(fn(it)) } } When used, it looks like a normal HashSet constructor: var real = HashSet<String>() var fake = HashSet(5) { "Element $it" } Should this be avoided or encouraged and why? 回答1: UPD In the updated coding conventions, there's a section on this topic: Factory functions If you

How to iterate over hashmap in Kotlin?

女生的网名这么多〃 提交于 2019-11-30 07:49:02
How to iterate over HashMap in Kotlin ? typealias HashMap<K, V> = HashMap<K, V> (source) Alexander Romanov It's not that difficult: for ((key, value) in map) { println("$key = $value") } OR ( Updated in accordance with @RuckusT-Boom's and @KenZira's information.) map.forEach { (key, value) -> println("$key = $value") } Ken Zira For the above answer, be careful with Android below N ! map.forEach { key, value -> println("$key = $value") } reference to Java 8 api which leads to: Rejecting re-init on previously-failed class java.lang.Class<T> map.forEach { (key, value) -> println("$key = $value")

Cannot resolve string supplied to vararg parameter in extension function

折月煮酒 提交于 2019-11-30 04:22:17
问题 strings.xml <string name="my_string">Showing your number: %1$s</string> ActivityExt.kt fun Activity.showToast(textResId: Int, vararg formatArgs: String) { val text = getString(textResId, formatArgs) Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } MainActivity.kt val number = 11 showToast(R.string.my_string, number.toString()) Toast with following text is showing: Showing your number: [Ljava.lang.String;@2cfa3b] Why this happens? 回答1: Use the spread operator: fun Activity.showToast

Should we avoid naming a function same as an existing class in Kotlin? Why?

大兔子大兔子 提交于 2019-11-29 10:11:16
Kotlin allows to name a function same as an existing class, e.g. HashSet with initializer function could be implemented like this: fun <T> HashSet(n : Int, fn: (Int) -> T) = HashSet<T>(n).apply { repeat(n) { add(fn(it)) } } When used, it looks like a normal HashSet constructor: var real = HashSet<String>() var fake = HashSet(5) { "Element $it" } Should this be avoided or encouraged and why? UPD In the updated coding conventions, there's a section on this topic : Factory functions If you declare a factory function for a class, avoid giving it the same name as the class itself. Prefer using a

How to extend a dataclass with toString

 ̄綄美尐妖づ 提交于 2019-11-29 09:04:17
I have created a dataclass data class Something ( val a : String, val b : Object, val c : String ) as later in my program I need the string representation of this dataclass I tried to extend the toString method. override fun Something.toString() : String = a + b.result() + c The problem here is, it does not allow extending(overriding) the toString funtion, as it is not applicable to top level functions. How to properly override/extend the toString method of a custom dataclass? In Kotlin, extension functions cannot override member functions, moreover, they are resolved statically . It implies

How to iterate over hashmap in Kotlin?

馋奶兔 提交于 2019-11-29 04:23:43
问题 How to iterate over HashMap in Kotlin ? typealias HashMap<K, V> = HashMap<K, V> (source) 回答1: It's not that difficult: for ((key, value) in map) { println("$key = $value") } OR ( Updated in accordance with @RuckusT-Boom's and @KenZira's information.) map.forEach { (key, value) -> println("$key = $value") } 回答2: For the above answer, be careful with Android below N ! map.forEach { key, value -> println("$key = $value") } reference to Java 8 api which leads to: Rejecting re-init on previously