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
10
votes
3 answers

extension function in a kotlin using super

How to call extension function of the base class in a derived class using the super keyword? I tried to call using super but it doesn't work. open class abc { open fun aa() { println("function in abc") } } fun abc.sum() { …
hardik gosai
  • 328
  • 3
  • 17
10
votes
4 answers

Create New Instance of Kotlin Object

I have an object QuickSort that I am attempting to create 2 instances of. When I try to create 2 separate instances I can see that it is only using one instance because I have a count in the QuickSort class that is inaccurate. Kotlin does not use…
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
10
votes
3 answers

Are extension methods and extension properties are bad practice?

So if extension methods and extension properties are really static methods and properties. And static methods and properties and methods are not thread safe and therefore should be avoided then extension methods and extension properties are bad. We…
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72
9
votes
1 answer

Extension function not found when run tests

I have this extension: src/main/kotlin/com/myproject/api/extensions.kt fun String.asJson() : JsonObject { return JsonObject.readFrom(this) } When I run my application, it works fine. But, when I run a test case that uses that extension…
Héctor
  • 24,444
  • 35
  • 132
  • 243
9
votes
1 answer

Experimental features in Android extension is good for Production release

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…
9
votes
3 answers

How to create object of a reified type in Kotlin

I was trying to create an extension function to create object of view holder for recycler view adapter inline fun ViewGroup.createViewHolder(@LayoutRes res: Int): T { val inflater =…
9
votes
1 answer

Kotlin function required Nothing but defined as a different type

I've defined a class like so abstract class MvpViewHolder

(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter { protected var presenter: P? = null fun bindPresenter(presenter: P): Unit { …

Rafa
  • 3,219
  • 4
  • 38
  • 70
9
votes
6 answers

Kotlin member and extension at the same time

In an attempt to understand more about Kotlin and play around with it, I'm developing a sample Android app where I can try different things. However, even after searching on the topic for a while, I haven't been able to find a proper answer for the…
NSimon
  • 5,212
  • 2
  • 22
  • 36
9
votes
2 answers

Kotlin extension clash

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…
Big Kahuna
  • 545
  • 4
  • 12
9
votes
2 answers

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

Kotlin allows to name a function same as an existing class, e.g. HashSet with initializer function could be implemented like this: fun HashSet(n : Int, fn: (Int) -> T) = HashSet(n).apply { repeat(n) { add(fn(it)) } } When…
Vilmantas Baranauskas
  • 6,596
  • 3
  • 38
  • 50
8
votes
2 answers

Kotlin extension function - compiler cannot infer that nullable is not null

Let's say I have a simple class Foo with a nullable String? data class Foo( val bar: String? ) and I create a simple function capitalize fun captitalize(foo: Foo) = when { foo.bar != null -> runCatching { foo.bar.capitalize() } else ->…
Bohemen90
  • 131
  • 3
8
votes
1 answer

kotlin GlobalScope, runBlocking is not available in kotlin.coroutines.*

I have multi module kotlin gradle project in github here. One of my sub project introducing-coroutines with build file build.gradle.kts file is here The contents of build.gradle.kts is - import org.jetbrains.kotlin.gradle.dsl.Coroutines …
user51
  • 8,843
  • 21
  • 79
  • 158
8
votes
0 answers

Using @Parcelize gives Attempt to invoke interface method 'int java.util.Collection.size()' on a null object reference at writeToParcel(Client.kt:0)

The Code for my Parcelable Model class is below, @Parcelize data class Client( @SerializedName("id") var id: Int = 0, @SerializedName("accountNo") var accountNo: String? = null, @SerializedName("status") private val…
8
votes
3 answers

Kotlin class NoClassDefFoundError crash

I have an existing Android Project that uses the following libs: AutoValue Dagger2 RxJava Retrolambda I am trying to add Kotlin Support so that I can slowly migrate the project over to Kotlin. Here is what I have done. Added Kotlin dependency.…
bond
  • 11,236
  • 7
  • 48
  • 62
7
votes
2 answers

Kotlin extension function with generic types dependant on the receiver type parameter (without specifying it explicitly)

Sometimes I want to have an extension function on a generic type, whose parameter or return value is a subtype of the given type parameter, but without having to specify that actual type parameter. (maybe, if it isn't clear, the code will clarify it…
Roland
  • 22,259
  • 4
  • 57
  • 84