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
0
votes
1 answer

Add new custom properties to retrofit2.Response

In my android app: Here my interface method: import retrofit2.Response import retrofit2.http.GET import retrofit2.http.Path import retrofit2.http.Query @GET("traders/json") suspend fun getTraidersList(): Response> Nice. but I need to…
Alexei
  • 14,350
  • 37
  • 121
  • 240
0
votes
1 answer

How to write extension function with nested generics?

I have extension function on Single class: fun > Single.checkResponse(): Single { return this.map { some code } } And trying to use it like: fun sign(body: Map): Single { …
0
votes
1 answer

Kotlin how to combine different receivers via with clause without repeating it

I have Parent-Search-Child system as below: class Room class Building { fun find(by: By) = Room() } sealed class By { abstract fun search(): Room class ById(id: String) : By() { override fun search(): Room = Room() // epic…
xinaiz
  • 7,744
  • 6
  • 34
  • 78
0
votes
1 answer

Data from firebase realtime wont display on my recyclerView

I need to display data from firebase database using a recyclerView. everything works fine, I can see the cards and all but there wont be and data on the card. here is my adapter class: class MyAdapter(c: Context, t: ArrayList) :…
0
votes
2 answers

How to access a view inside of another class using Kotlins view binding

I am currently following a Tutorial about RecyclerView, which is "written" in java. Now I am trying to basically take the tutorial and code it in Kotlin. I now want to access Views from the layout_listitem.xml (which basically just describes the…
0
votes
1 answer

How can I add some generated code in existing method

Synthetic adds clearFindViewCache() in Activity.onDestroy() and Fragment.onDestroyView() methods. How can I add extra code to existing method with my own annotation processor? If we look to the synthetic's implementation we can see usage of classes…
0
votes
2 answers

Kotlin pass function as a parameter vs lambda implementation

I'm using the io.reactivex.rxkotlin extension function: fun Observable.subscribeBy( onError: (Throwable) -> Unit = onErrorStub, onComplete: () -> Unit = onCompleteStub, onNext: (T) -> Unit = onNextStub …
CookieMonster
  • 1,723
  • 1
  • 15
  • 15
0
votes
2 answers

Possible to Kotlin extend/override Double.toString to produce less decimal places?

I already have a hack: /** Shorter round for the logs */ val Double.str: String get() = "%.3f".format(this) But that has to be manually inserted into all of my LOG.info { "It went ${distance.str}" } statements. I'd prefer to be able to set the…
Benjamin H
  • 5,164
  • 6
  • 34
  • 42
0
votes
1 answer

how to get the coefficients out of the polynomal expression?

At the input I get a polynomial as a string, I want to get its coefficients in variables, but i have no idea gow do this. example:7x^4+3x^3-6x^2+x-8.Maximum degree is not known, coefficients are integers. I will be very grateful for any help.
Marlock
  • 275
  • 2
  • 9
0
votes
0 answers

Cannot choose among the following candidates without completing type inference

I have these three helper extensions: fun BaseQuickAdapter.onItemClick( onItemClick: (adapter: BaseQuickAdapter<*, *>, position: Int, item: T) -> Unit ): BaseQuickAdapter { setOnItemClickListener {…
humazed
  • 74,687
  • 32
  • 99
  • 138
0
votes
1 answer

How to unit test an kotlin extenion function that has a generic type

kotlin 1.2.51 I have the following shared preferences that uses a generic extension function. class SharedUserPreferencesImp(private val context: Context, private val sharedPreferenceName: String):…
ant2009
  • 27,094
  • 154
  • 411
  • 609
0
votes
2 answers

Kotlin Class Extension

I'm just learning Kotlin as a fun side-project for Project Euler. I just started literally 5 minutes ago, in IntelliJ IDEA. I have this code: fun Number.isMultipleOf(n: Number): Boolean { return this % n == 0 } fun main(args: Array){ …
mackycheese21
  • 884
  • 1
  • 9
  • 24
0
votes
2 answers

Extension function on a generic type

I am new in Kotlin and I am trying to understand type alias and functions. I have the following example: interface EmptyInterface typealias GenericCase = T.(EmptyInterface) -> T val myFunctionVariable: GenericCase = { _ -> "Hello…
Jim
  • 3,845
  • 3
  • 22
  • 47
0
votes
1 answer

Kotlin Extensions - Synthetic imports show compilation error in child class

I have a class A where my view fragment_my_quests is defined. Synthetic imports works fine in class A. Now I extend B with A and try to use Synthetic import and id's inside my child class B. It shows red (compilation error). But when I run the app,…
0
votes
1 answer

How can I test/access companion object extension functions in Kotlin?

I have an Base64Util class with amongst others an extension function decodeBase64ToByteArray : class Base64Util { companion object { fun String.decodeBase64ToByteArray(): ByteArray { return Base64.getUrlDecoder().decode(this) } …
Tranquillo
  • 235
  • 2
  • 13