Questions tagged [kotlin-extension]

Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.

333 questions
242
votes
8 answers

How to check "instanceof " class in kotlin?

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…
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
180
votes
5 answers

How to iterate over hashmap in Kotlin?

How to iterate over HashMap in Kotlin? typealias HashMap = HashMap (source)
Nomi
  • 1,981
  • 2
  • 8
  • 10
100
votes
7 answers

Accidental override: The following declarations have the same JVM signature

I'm getting error in Kotlin in this part: class GitHubRepoAdapter( private val context: Context, private val values: List ) : ArrayAdapter( context, R.layout.list_item, values ) private val context:…
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85
97
votes
2 answers

throws Exception in a method with Kotlin

I'm trying to convert this Java code to Kotlin: public class HeaderInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { return null; } } The problem is, when I implement the methods, I…
cesards
  • 15,882
  • 11
  • 70
  • 65
78
votes
2 answers

How to extend a data class with toString

I have created a data class data class Something ( val a : String, val b : Object, val c : String ) as later in my program, I need the string representation of this data class I tried to extend the toString method. override fun…
user4233758
66
votes
13 answers

Kotlin Android debounce

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.
57
votes
4 answers

How can one add static methods to Java classes in Kotlin

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…
Eric
  • 16,397
  • 8
  • 68
  • 76
42
votes
5 answers

Kotlin extension function access Java private field

I'd like to access Java's private field when using Kotlin extension function. Suppose I have a Java class ABC. ABC has only one private field mPrivateField. I'd like to write an extension function in Kotlin which uses that field for whatever…
40
votes
3 answers

Deep copy of list with objects in Kotlin

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:…
Oya
  • 833
  • 1
  • 8
  • 21
40
votes
4 answers

Kotlin Extension Functions suddenly require api level 24

I just noticed this lint error: Call requires API Level 24 (current min is 19) java.util.map#foreach when I use the extension function forEach on a MutableMap in Kotlin. This didn't happen when I wrote the line, but its there now. And I'm not…
Sebastian Rüttger
  • 765
  • 3
  • 10
  • 16
36
votes
1 answer

How to organize Kotlin extension methods

Let's say I have a few extension methods for "MyClass". My question is, what's the best practice to organize/store these methods? Should they be simply put into a "MyClassExtensions" Kotlin file? I have tried to encapsulate these methods within a…
pjozsef
  • 815
  • 8
  • 14
35
votes
4 answers

Kotlin List tail function

I am trying to find a tail function in List but I couldn't find any. I ended up doing this. fun List.tail() = this.takeLast(this.size -1) Is there a better way to do this?
Abdelrhman Talat
  • 1,205
  • 2
  • 13
  • 25
30
votes
6 answers

Split space from string not working in Kotlin

Anyone wonder this ? Splitting SPACE (" ") in kotlin is not working, i tried with different regex codes but is not working at all. Tried with this : value.split("\\s")[0]; value.split("\\s+")[0]; value.split("\\s++")[0]; Then i came up with…
Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
25
votes
1 answer

Extension functions on annotated types

Is it possible to define a Kotlin extension function on a annotated type like this? @ColorInt fun @ColorInt Int.darken(): Int { return ColorUtils.blendARGB(this, Color.BLACK, 0.2f) } Alternative form: @ColorInt fun (@ColorInt Int).darken(): Int…
25
votes
2 answers

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

I have this snippets: class RecyclerViewAdapter internal constructor( val clazz: Class, val layout: Int, var dataList: MutableList<*>) ... ... ... fun RecyclerView.getDataList() : ArrayList<*> { return…
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72
1
2 3
22 23