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
2
votes
1 answer
In Kotlin, how do I convert "CompletableFuture>" to "Flow"?
I'm trying to convert a CompletableFuture> to a Flow. The extension function I'm trying to write is
fun CompletableFuture>.asFlowOfNullable(): Flow =
this.toMono().map { (if (it.isPresent) it.get() else null)…

Matthew Adams
- 2,059
- 2
- 21
- 31
2
votes
2 answers
Coroutine on an extension function
I am using extension function to bind list data to recyclerview , with pagedlist it doesn't need any coroutine
@BindingAdapter("pagedListAdapterData")
fun submitPagedList(recyclerView: RecyclerView, list: PagedList?) {
if (list !=…

joghm
- 569
- 3
- 20
2
votes
2 answers
How to declare an extension static function in Kotlin on Java Classes?
I want declaring an extension func in kotlin but on Java classes Library, I know that do in Kotlin when you resolve companion in extension function.
Like:
class Food {
companion object {
fun foo() = Unit
}
}
fun…

ImanX
- 789
- 6
- 23
2
votes
2 answers
Call extension in data binding expression
I was wondering if there is any way we can call extensions in expression attribs,
For Eg, here is some extension
fun String.parseServerDate():String{
...
}
To call while in kotlin, we use myTime.parseServerDate()
So, my problem is how can I call…

Aziz
- 1,976
- 20
- 23
2
votes
1 answer
Cancel all Kotlin coroutines pending jobs on ViewModel's onCleared()
Stopping jobs in onCleared() of ViewModel after activity finish shows the JobCancellationException: Job is being canceled and keeps app freeze crash:
What would be the right way to cancel all kotlin coroutines pending jobs from ViewModel's…

Nabin
- 1,451
- 3
- 19
- 26
2
votes
1 answer
Call extension function from different class
I'm trying to create a very simple transaction manager like this:
object PersistenceManager {
private val dataSource: DataSource by lazy {
val config = ConfigFactory.load()
hikari(config.getConfig("postgres"))
}
private…

carcaret
- 3,238
- 2
- 19
- 37
2
votes
1 answer
extension function for Kotlin data class
I have a data class which looks something like this
data class SuggestionResponse(
val metadata: Metadata,
val response: Response
)
data class Response(
///blah blah
)
data class Metadata(
val timeleft: String,
val totalTime:…

Nari
- 333
- 5
- 13
2
votes
1 answer
Typealias and extension function in Kotlin
I need some help understanding the following code as I am complete Kotlin newbie. This is from a kotlin post I found online
typealias genericContext = Demo.() -> Unit
class Demo {
infix fun doThis(block: genericContext) = block()
…

Jim
- 3,845
- 3
- 22
- 47
2
votes
2 answers
Kotlin: Find Count from Nested set in List (more functional approach)
Below function creates a Map, gets the count of passengers where passengers are > minTrips. The code works completely fine. Please see below
fun List.filter(minTrips : Int): Set {
var passengerMap: HashMap =…

Ahmed
- 2,966
- 7
- 42
- 69
2
votes
3 answers
How to convert an extension into a member?
Kotlin allows you to extend concrete instances of a generically typed class. For example, suppose I have the following class Foo, with extension functions Foo.bar() and Foo.bar():
class Foo(t: T) {
val a: String = "bar"
val b:…

breandan
- 1,965
- 26
- 45
2
votes
1 answer
Does Kotlin permit extension methods on lambdas?
I'm trying to make a nice SAM-like API for instantiating abstract classes because I don't like object expressions. I'm trying to do something like:
{my lambda code here}.my_extension_function()
Is this possible with Kotlin?

piotrek
- 13,982
- 13
- 79
- 165
2
votes
0 answers
MediatorLiveData vs LiveData
I am not able to get the idea of MediatorLiveData while working with LiveData. In documentation it is mentioned that MediatorLiveData is a subclass of LiveData that will observe LiveData and react on onChanged method. My question is what's it's…

musooff
- 6,412
- 3
- 36
- 65
2
votes
3 answers
Can't run kotlin project
I am brand new to kotlin. I have installed kotlin plugins to eclipse. I found the simple example posted below in one of the tutorials.
The issue is, when I run the project i receive the below stated error.
To solve this issue I tried to run the…

Amrmsmb
- 1
- 27
- 104
- 226
2
votes
1 answer
Can I develop apps for iOS on Windows using Kotlin?
I want to build an app for iOS on Windows. I know how to develop apps for iOS on windows using Objective-C but I would like to know how I can build apps for iOS on windows using Kotlin.

Gourav Manuja
- 89
- 1
- 10
2
votes
1 answer
Error in TypeReference in Kotlin Jackson Mapper
I've got the following data class
data class MyResponse(val header: String,
val body: T)
I want to write a generic Kotlin function that can deserialise json to various MyResponse or MyResponse
class…

Tin Ng
- 937
- 2
- 11
- 25