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

NPE in using @Parcelize Annotation in Kotlin Data Class

I recently migrate my Java Models class to Kotlin Data class. I'm using @Parcelize annotation to avoid boilerplate code for Parcelable. My data class in Kotlin is shown below, @Parcelize data class TimeLine( @SerializedName("submittedOnDate") …
1
vote
3 answers

Mapping model classes with Kotlin

I'm looking a way to improve the mapping mechanisms between the model classes from the different layers in my App. I've model classes, the ones that I use in my App, in the business logic layer such as a Presenter. For example a User class. I've…
4gus71n
  • 3,717
  • 3
  • 39
  • 66
1
vote
1 answer

How to import type extension function in Kotlin

I wrote an extension function for Any type, that will retrieve object property value by its name. I want to be able to use it from everywhere in my project. Here is my extension function: package com.example.core.utils.validation import…
mustafo
  • 59
  • 1
  • 7
1
vote
0 answers

Kotlin Android Extensions plug-in doesn't work occasionally

As shown in the screenshot below, why is it that the Kotlin Android Extensions plug-in (used for binding views) doesn't get recognized throughout my project's UI classes (Activity/Fragment)? This particularly happened when viewing in-between…
DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37
1
vote
1 answer

Import class and extension functions for the class simultaneously

When you are writing a class (here it will be a simple Integer class, so it will be easy to follow) and you are overloading operators, I already had a problem on how to overload the operator for a stranger class, which takes your object as a…
alexanders916
  • 157
  • 1
  • 16
1
vote
0 answers

Kotlin Generic List Parsing

I was looking other questions but this one is using a different method. I’m using MVVM in my Android app. Actually this is the way that I’m getting data from my server: Inject dataManager into viewModel. viewModel calls…
1
vote
1 answer

capture events by sliding left or right using kotlin

How can I capture events by sliding left or right using kotlin? I've implemented OnTouchListener to try to capture something, but nothing happens. class MyClass : Fragment(), View.OnTouchListener { lateinit var mRootView: View …
1
vote
1 answer

Kotlin Extensions property vs. function - Conflict

I have the two following definitions. The first one as an extension function allowing to set visible a View and also to set it as gone or invisible with the second parameter. fun View.setVisible(visible: Boolean, goneWhenVisibleFalse: Boolean) { …
crgarridos
  • 8,758
  • 3
  • 49
  • 61
1
vote
1 answer

Kotlin and Android Espresso tests: Adding extension function with receiver

I'm still working on improving my understanding of extension functions with receivers and need some help of you experts on a question I have regarding this. I have an Android Espresso testcase where I check that I have selected the items of a…
Bohsen
  • 4,242
  • 4
  • 32
  • 58
1
vote
1 answer

RxJava - Extension Function for Subscribe

I'm trying to use RxJava / Retrofit, and I want to write an extension function to wrap some logic with the network result. I have a class, NetworkConsumer that extends Consumer. abstract class NetworkConsumer : Consumer> { …
advice
  • 5,778
  • 10
  • 33
  • 60
1
vote
1 answer

Extension and higher-order functions referencing

I get an overall idea of what is each of these, I wrote piece of code however that I don't quite understand why it works.The thing is callExtensionOnString expects extension function as its parameter, however it doesn't complain if reference to…
tomwyr
  • 1,351
  • 2
  • 16
  • 21
1
vote
1 answer

Android kotlin extension error

java.lang.VerifyError: .../utils/KotlinViewExtKt$animateFadeOut$1 Getting that error while running app on emulator PRE Lolipop (<21 api) Function causing the trouble: fun View.animateFadeOut(animDuration: Long = 250) { this.animate() …
TooLazy
  • 836
  • 4
  • 11
  • 29
1
vote
1 answer

Cannot resolve type inference when overloading function extensions

Given the following method extensions (JsonResult is just a wrapper with some additional fields): fun T.toJson(transform: (T) -> R) = JsonResult(transform(this)) fun List.toJson(transform: (T) -> R) =…
user8608956
1
vote
1 answer

Kotlins extension method Stream.toList() is missing

I'm trying to convert a stream to a correctly typed list in kotlin. For this case kotlin provides the extension method Stream.toList(). This method is located in kotlin.streams. The IDE (IntelliJ) does not allow to call this method on a stream. Do I…
Marc von Renteln
  • 1,229
  • 15
  • 34
1
vote
3 answers

Kotlin scope functions with vs extension run

From what I've learned, it seems like extension function T.run and with have kind of the same purpose of creating the possibility to group multiple calls on the same object, returning lambda's last object as its result. T.run() has the advantage of…
DoruAdryan
  • 1,314
  • 3
  • 20
  • 35