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
0 answers

How to create a generic extension function for an enum class annotated with kotlinx.serialization.Serializable?

Given the following Kotlin enum: @kotlinx.serialization.Serializable enum class MyEnum { @SerialName("One") ONE, @SerialName("Two") TWO, @SerialName("Three") THREE } I can call the following generated…
JJD
  • 50,076
  • 60
  • 203
  • 339
1
vote
1 answer

How to shadow member function with a custom Kotlin extension function? (like Kotlin stdlib does)

I am reading Kotlin in Action 2nd edition. Chapter 3 says: If the class has a member function with the same signature as an extension function, the member function always takes precedence At the same the book demonstrates the CharSequence.split…
Kirill
  • 6,762
  • 4
  • 51
  • 81
1
vote
1 answer

Can I add companion extension without first having companion object within a class?

For the below code, I can add invoke extension to the Companion operator fun MyValue.Companion.invoke(value: Int) = MyValue(value.toString()) class MyValue(private val value: String) { companion object fun print() = println("value =…
Elye
  • 53,639
  • 54
  • 212
  • 474
1
vote
1 answer

how to import an extension function in Kotlin?

this is the function i'm trying to import https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Density#(androidx.compose.ui.unit.DpRect).toRect() here is my code val cardWidth = 180f.dp val cardHeight = 270f.dp val…
Mars
  • 873
  • 1
  • 11
  • 24
1
vote
1 answer

Android Formatting String in Extension Function

I am trying to get Formatting String in AndroidViewModel by using following extension function but failed: Version %1$s val versionText = getString(R.string.version_text, "1.0.0") fun…
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
1
vote
2 answers

kotlin-android-extensions not working. what will be the problem?

I was following some guide(download android studio today) of kotlin and I have use the setText and it's not working. what will be the problem? package com.example.basic import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import…
kimhaju
  • 19
  • 6
1
vote
1 answer

Why is this extension function slower than non extension counterpart?

I was trying to write a parallel map extension function to do map operation over a List in parallel using coroutines. However there is a significant overhead in my solution and I can't find out why. This is my implementation of the pmap extension…
Fedelway
  • 21
  • 2
1
vote
1 answer

Kotlin extension function failed to compile in java

I've defined the following extension function fun T.showAppPresentation( appPresentable: Maybe, appPresentationView: AppPresentationView, closeListener: () -> Unit ) where T : Fragment, T : FragmentPresentable { …
Benjamin
  • 7,055
  • 6
  • 40
  • 60
1
vote
2 answers

Lint error with snackbar extension function

I have the following extension function to reduce a bit the code and to avoid forgetting the duration when showing a snackbar: fun Fragment.showSnackbar(text: String, length: Int = Snackbar.LENGTH_SHORT) { view?.run { Snackbar.make(this, text,…
1
vote
1 answer

Kotlin - Is there a way of defining an explicit scope of a function with receiver defined in companion object?

Imagine you have a following code: interface SomeClient { fun isValid(value: String): Boolean } class SomeData private constructor( val value: String ) { companion object { fun SomeClient.create(value: String): SomeData? = …
alakida
  • 48
  • 6
1
vote
1 answer

Android KTX: how to override Kotlin added property extension

I am trying to override View.setRotation() method in Kotlin. Since AndroidKTX already provided property extension "rotation", caller's can simply call viewObject.rotation = 90.0f to rotate the view. However, I want to add some additional operation…
Robin
  • 10,052
  • 6
  • 31
  • 52
1
vote
2 answers

How to create a View extension function in Kotlin

I'm trying to create an extension function in Kotlin. I did try several tutorials, but didn't quite understand, how to implement this one. I'm trying to create a setWidth() function as such //Find my_view in the fragment val myView =…
1
vote
1 answer

How to filter a single list with multiple "cases" in Kotlin

I have a Sealed class as follows:- sealed class SoundEffect { sealed class Acoustic : SoundEffect() { object Active : Acoustic() object Inactive : Acoustic() object Disable : Acoustic() object Enable :…
Hector
  • 4,016
  • 21
  • 112
  • 211
1
vote
0 answers

How to supply side effect for RxBinding.textChanges() and preserve it's functionality on client side?

Suppose that i'm writing my own View class, that represents editable field and contains EditText tv_input inside: class EditTextIconItemView : LinearLayout { fun setInputText(text: String?) { with (tv_input) { setText(text) …
1
vote
1 answer

Extension property inside class: Unresolved reference: errorResponse

In my android project: // Extension Properties val Response<*>.errorResponse: ErrorResponse get() = ErrorUtils.parseError(this) class TransportService { companion object { private val SERVICE_UNAVAILABLE_CODE = 503 private…
Alexei
  • 14,350
  • 37
  • 121
  • 240