kotlin-extension

Kotlin inlined extension property

无人久伴 提交于 2019-12-22 04:37:07
问题 I know inline keyword means to avoid the call overhead calling a funtion. But I can't figure out what inline a extension property work for? Let say we have two extension property named foo and another with is inlined named bar val Any.foo : Long get() = Date().time inline val Any.bar : Long get() = Date().time Executing any of them, we gent the expected output, the current time. The bytecode for this file is this below: public final class InlinedExtensionPropertyKt { public final static

Experimental features in Android extension is good for Production release

可紊 提交于 2019-12-20 01:41:54
问题 I'm using the @Parcelize feature in android development using Kotlin language. To use them I have done the below modifications in build.gradle file. apply plugin: 'kotlin-android-extensions' then androidExtensions { experimental = true } I'm successful in using the above feature. But is it recommended to use them for production release or only for development purpose alone? 回答1: This isn't just an Android-thing though, it's Kotlin in general. Kotlin has quite a few experimental features, such

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

感情迁移 提交于 2019-12-18 14:06:43
问题 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

Deep copy of list with objects in Kotlin

允我心安 提交于 2019-12-18 07:45:03
问题 I am new to kotlin and I am trying to make a copy of a list of objects.The problem I am having is that when I change items in the new copy, the old list gets changed as well. This is the object: class ClassA(var title: String?, var list: ArrayList<ClassB>, var selected: Boolean) class ClassB(val id: Int, val name: String) I tried doing this, but it doesn't work: val oldList:ArrayList<ClassA> val newList :ArrayList<ClassA> = ArrayList() newList.addAll(oldList) 回答1: That's bacause you are

How to extend a dataclass with toString

吃可爱长大的小学妹 提交于 2019-12-18 05:27:06
问题 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? 回答1: In Kotlin,

How can one add static methods to Java classes in Kotlin

六月ゝ 毕业季﹏ 提交于 2019-12-17 16:24:34
问题 Is it possible to add a new static method to the java.lang.Math class in Kotlin ? Usually, such things are possible in Kotlin thanks to Kotlin Extensions. I already tried doing the following in a file I made called Extensions.kt : fun Math.Companion.clamp(value:Double,minValue:Double,maxValue:Double):Double { return Math.max(Math.min(value,maxValue),minValue) } but Math.Companion could not be resolved... 回答1: As of Kotlin 1.3, this is not possible. However, it's being considered for a future

Kotlin Android debounce

懵懂的女人 提交于 2019-12-12 07:47:33
问题 Is there any fancy way to implement debounce logic with Kotlin Android? I'm not using Rx in project. There is a way in Java, but it is too big as for me here. 回答1: You can use kotlin coroutines to achieve that. Here is an example. Be aware that coroutines are experimental at kotlin 1.1+ and it may be changed in upcoming kotlin versions. UPDATE Since Kotlin 1.3 release, coroutines are now stable. 回答2: Thanks to https://medium.com/@pro100svitlo/edittext-debounce-with-kotlin-coroutines

how to get the coefficients out of the polynomal expression?

天涯浪子 提交于 2019-12-11 15:29:43
问题 At the input I get a polynomial as a string, I want to get its coefficients in variables, but i have no idea gow do this. example: 7x^4+3x^3-6x^2+x-8 .Maximum degree is not known, coefficients are integers. I will be very grateful for any help. 回答1: Split by plus and minus (e.g. with re.split), preserving the signs in the results. Then for each substring, split by "x" to get the leading coefficient (+1 and -1 are special cases), and take note of missing powers of x (i.e. coefficient 0). 来源:

Kotlin get type of generic class without instance

蹲街弑〆低调 提交于 2019-12-10 17:14:09
问题 Hey want go get Type of T , but I can not get it from instance, I have to get it from class parameter, how to do it? abstract class ViewModelFragment<T : ViewModel>{ protected lateinit var mViewModel: T override fun onViewCreated(view: View, savedInstanceState: Bundle?) { mViewModel = ViewModelProviders .of(scope) .get(getGenericTClass()) // .get(mViewModel.javaClass) // not working either } inline fun<reified R> getGenericTClass() = R::class.java } Right now compiler complains Cannot use 'T'

Use Kotlin extension in android java class

浪子不回头ぞ 提交于 2019-12-10 02:50:17
问题 Is it possible to use a kotlin extension in a android java class? Example: fun String.getSomething(): String { return "something" } then in Java use it like: String someString = "blabla"; someString.getSomething(); is this possible? 回答1: You can mix kotlin and java ( use/call kotlin classes in java classes ) But what you want here is use a kotlin feature in java - this is not possible 回答2: Kotlin's extension functions are compiled to JVM methods taking the receiver as the first parameter. If