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
1
vote
1 answer

Kotlin as multi platform dependency issue

I am writing an experiment project for multi platform by using Kotlin. I wrote some common logic by using RXjava and retrofit. There are three folder Kotlin[commonMain] Kotlin[androidMain] Kotlin[iosMain] The code work fine in Kotlin[androidMain]…
user1154390
  • 2,331
  • 3
  • 25
  • 32
1
vote
1 answer

Kotlin - Serialize JSON Array to multiple classes

I'd like to parse JSON Array(in String) to Kotlin Array of multiple classes. Is it possible to do serialization like below? I'm using kotlinx.serialization, but can use other libraries if needed. abstract class DataTypeBase(val dataType:…
1
vote
3 answers

Create extension that run a function if object is not null

Can I have kotlin extension function that do something like this: // extension inline fun T?.runIfNotNull(function: (T) -> Unit) { this?.let { function(it) } } // some function fun doSomething(int: Int){ // do something } //…
SIRS
  • 624
  • 6
  • 20
1
vote
1 answer

Kotlin synthetic view in activity gets null when a custom dialog is shown and dismissed

I am updating a view in activity's onCreate method which is working fine using kotlin extension as stated below. Activity's onCreate import kotlinx.android.synthetic.main.activity_otpverification.* override fun onCreate(savedInstanceState:…
Bee
  • 142
  • 17
1
vote
2 answers

Selecting function reference before application: problem with type inference

I want to choose function reference before applying it to arguments, but Kotlin cannot infer its type. Suppose I have a class class Circle(val radius: Float) and a function fun sortByRadius(circles: MutableList, ascending: Boolean) { if…
1
vote
1 answer

Reference to multiple bounds generic receiver extension function in Kotlin

Has an extension function fun T.doSomething() where T: A, T: B If there only one generic bound A, I can use syntax (A::doSomething)(instanceOfA) to reference to the function, but how to do this with multiple bounds? Example: interface A,…
Zouyiu Ng
  • 193
  • 1
  • 12
1
vote
1 answer

Kotlin: Multiple operations on Collections

Edit: I had to return a set of passengers who had majority of discounts. The solution to this riddle was as following, Extract all the passengers count in a Map Extract all the passengers count in a Map who received discounts. Then in a loop I had…
Ahmed
  • 2,966
  • 7
  • 42
  • 69
1
vote
1 answer

Kotlin extension function with request to rest server

I'm setting up extension function for Timber. I want to have kind of function to send log to my server. The problem for me is Dagger. I have instance of RestService class in dagger and I'm using it in my whole app. But to use it I need inject…
AdamN
  • 476
  • 5
  • 13
1
vote
1 answer

Kotlin - Create custom ext function for SpannableStringBuilder without duplicate arguments when declaring start, end & flasg for setSpans()

this is MainActivity.kt before var spannable = SpannableStringBuilder("$noColorText$coloredText") spannable.setSpan( ForegroundColorSpan(ContextCompat.getColor(textView.context, R.color.mainGreen)), noColorText.length, spannable.length, …
1
vote
2 answers

Refactoring Kotlin method using a lambda

I have the following emun class to represent my navigation bar views. enum class NavigationPosition(val position: Int, val id: Int) { HOME(0, R.id.nav_home), SEARCH(1, R.id.nav_search), PROFILE(2, R.id.nav_profile), SETTINGS(3,…
patrick-fitzgerald
  • 2,561
  • 3
  • 35
  • 49
1
vote
2 answers

Reflecting the name of 'this' in a Kotlin extension function

Is there a way to get the name of 'this' in an extension function? fun Boolean?.persist() { if (this == null) return // Do Nothing val nameOfVariable:String = //get the name of the variable? // Persist variable …
Crocodile
  • 5,724
  • 11
  • 41
  • 67
1
vote
2 answers

contents of a list after filter

would you please let me how can I print out the contents of persons? the output of the code is [Person@72ea2f77, Person@33c7353a, Person@681a9515, Person@3af49f1c, Person@19469ea2] code: fun main(args: Array) { val person1 =…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
1
vote
1 answer

Is this usecase of kotlin reified type useful

The Reified Type parameters support run-time access to types passed to functions. I understand that this can be useful in certain scenarios to avoid reflection. But there are examples of creating extension functions with reified type parameters…
1
vote
1 answer

unit testing extension function and mocking the other methods of the class

I am writing an extension function adding some retry capabilities to AmazonKinesis.putRecords. In my extension method i do some logic and some calls to the original putRecords method: fun AmazonKinesis.putRecordsWithRetry(records:…
David Karlsson
  • 9,396
  • 9
  • 58
  • 103
1
vote
2 answers

How to write rx concatArrayEager equivalent in Kotlin CoRoutine?

I would like to convert my rxJava Code to Kotlin CoRoutine. Below is the code makes both the api and db call and returns the data to UI whatever comes first. Let us say if DB response happens to be quicker than the api. In that case still, the api…
Bulu
  • 1,351
  • 3
  • 16
  • 37