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.
Questions tagged [kotlin-extension]
333 questions
5
votes
2 answers
Why extension function is not visible in another module?
My Android project has two modules:
app
common
In settings.gradle:
rootProject.name='My project'
include ':app'
include ':common'
In my build.gradle:
implementation project(':common')
In common package I has StringUtil.kt with the next extension…

Alexei
- 14,350
- 37
- 121
- 240
5
votes
2 answers
How to cancel a running LiveData Coroutine Block
By using LiveData's latest version "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03", I have developed a code for a feature called "Search Products" in the ViewModel using LiveData's new building block (LiveData + Coroutine) that performs a…

Yasir Ali
- 1,785
- 1
- 16
- 21
5
votes
1 answer
android - kotlin infix not working on one parameter extension function
take a look at this simple extension function i have infix:
infix fun View.isValidColor(hexColor: String?): Boolean {
var isValid = true
return hexColor?.let {
try {
Color.parseColor(it)
} catch (e: Throwable) {
…

j2emanue
- 60,549
- 65
- 286
- 456
5
votes
1 answer
Is there a way in Kotlin to combine an inline function and a when expression with an extension function?
I would like to reduce the size of an if-elseif-else ladder but can't use just when because the smartcast doesn't catch the cases I'm dealing with. I'm wondering if I can combine something like a let or an also and a when into a single inline…

Ryan Shea
- 73
- 4
5
votes
1 answer
Kotlin: Possible to modify functions during compile time through metaprogramming?
In dynamic languages like JavaScript/Python, it's possible to overwrite or "modify" functions during run-time. For example, in order to modify the alert function in JS, one could do:
const _prev_alert = window.alert;
window.alert = function() {
…

Griffort
- 1,174
- 1
- 10
- 26
5
votes
5 answers
Extension function of String class
I want to create an extension function of String which takes a String and returns a new String with the characters as the first, but sorted in ascending order. How can I do this? I am new to Kotlin.

MissAmna
- 317
- 5
- 13
5
votes
1 answer
Android Kotlin Extension super calling
i am a Java Android Developer and i'm approaching to Kotlin
I have defined the following class:
open class Player : RealmObject() {
...
}
And i defined the following two extensions, one for the generic RealmObject class and one for the specific…

Gianni Genovesi
- 91
- 6
5
votes
3 answers
Kotlin - Extension for final class
Is it possible to create extension of final classes like String? Like in swift it is possible to add additional methods inside a extension of final class.
For an example - I would like to create a method in String extension which will tell me String…

Rahul
- 10,457
- 4
- 35
- 55
5
votes
1 answer
Android kotlin overriding interface methods inside onCreateView() method
I am new to Kotlin. I have an interface containing two method definitions:
fun onSuccess(result: T)
fun onFailure(e: Exception)
Now, in my fragment I have implemented this interface and want to use these methods inside as:
override fun…

Pinaki Mukherjee
- 1,616
- 3
- 20
- 34
4
votes
1 answer
Kotlin list extension function
I have an extension function shown below
infix fun List.sumList(that: List) =
this.zip(that, Int::plus)
I am now trying to make this extension function generic so that it works on list of integers as well as float and…

Nagasree
- 173
- 2
- 2
- 9
4
votes
1 answer
How can I optimize code for extension function of Context, Fragment and Activity in Kotlin?
The Code A is extension function for Context, Fragment and Activity.
I think it's redundant, how can I optimize it?
Code A
fun Context.toast(msg: String){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
}
fun…

HelloCW
- 843
- 22
- 125
- 310
4
votes
0 answers
Generic constraint based on KClass property to scope down an extension function
I am writing a kotlin extension function that should only work for sealed classes. Can I define a Generic Constraint - or something similar that serves the same purpose - such that an extension function would only work on classes that are a sealed…

A1m
- 2,897
- 2
- 24
- 39
4
votes
1 answer
Could not set unknown property 'implementation' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
I was adding new dependencies for new android-kotlin project but getting the following error after sync the project.
Could not set unknown property 'implementation' for object of type
…

Ritesh
- 533
- 2
- 7
- 18
4
votes
0 answers
Lifecycle.addObserverUntilDestroy extension stopped working with AndroidX
I've been using following Kotlin extension to add my lifecycle observers:
fun Lifecycle.addObserverUntilDestroy(observer: LifecycleObserver) {
addObserver(observer)
addObserver(object : LifecycleObserver {
…

Jan Slominski
- 2,968
- 4
- 35
- 61
4
votes
1 answer
How to list all Kotlin extension functions in project in Android Studio
Is here a way to list all Kotlin extension functions in a project / module in Android Studio?
I've seen this way to list all extension functions for one class but nothing for all classes.

Oliver Metz
- 2,712
- 2
- 20
- 32