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

Android Java Project Converts to Kotlin in Android Studio

I want to convert my java project to Kotlin, there are more than 500 java files. How can I convert it into Kotlin easily? I know that we can convert one by one java file to Kotlin, But there are many files. Can I convert it at once?
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
23
votes
3 answers

Extension functions for generic classes in Kotlin

What's wrong with my extension function below class Foo { fun Foo.plus(that: Foo): Foo = throw Exception() init { Foo() + Foo() // A receiver of type Foo is required } } Update I wonder why it's…
Yaroslav
  • 4,543
  • 5
  • 26
  • 36
21
votes
5 answers

Cannot resolve symbol repeatOnLifecycle in Android

I'm following this article to collect flows in UI. But I couldn't resolve repeatOnLifeCycle API in my code. I have added the below dependency, though. lifecycle:lifecycle-runtime-ktx:2.4.0-alpha03 Please help
Rakesh
  • 1,205
  • 1
  • 14
  • 33
21
votes
4 answers

Kotlin: How to extend the enum class with an extension function

I'm trying to extend enum classes of type String with the following function but am unable to use it at the call site like so: fun > Class.join(skipFirst: Int = 0, skipLast: Int = 0): String { return this.enumConstants …
geg
  • 4,399
  • 4
  • 34
  • 35
19
votes
1 answer

Kotlin synthetic extension for view

I have a layout with some views, one of them has the id title_whalemare import kotlinx.android.synthetic.main.controller_settings.* import kotlinx.android.synthetic.main.view_double_text.* class MainSettingsController : BaseMvpController
whalemare
  • 1,107
  • 1
  • 13
  • 30
18
votes
1 answer

Kotlin Android: Property delegate must have a 'getValue(DashViewModel, KProperty*>)' method

I am trying to follow the official Android guide for ViewModels in Kotlin. I literally copy pasted the easiest official example but the syntax seems to be illegal. This section causes the problem: private val users: MutableLiveData> by…
user123456789
  • 301
  • 1
  • 4
  • 15
18
votes
4 answers

how to instantiate an object from a class in kotlin

I am learning Kotlin, and I googled how to create a class in kotlin. So, I created the below class as a test. In the main activity, I am trying to instantiate an object from the class Board, but i get the following error: classifier Board does not…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
18
votes
3 answers

Modify "this" in extension function

I want to write extension function that modifies "this", for example: var a = false a.toggle() // now a contains false or var a = 1 a.increment() // now a contains 2 Is it possible in Kotlin? I can create extension function that returns modified…
Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
17
votes
1 answer

how to use the new extension functions in okhttp 4

Hi got this compile error when upgrading from okHttp version 3 to version 4: val JSON = MediaType.parse("application/json; charset=utf-8") //Compile Error: Kotlin: Using 'parse(String): MediaType?' is an error. moved to extension function I changed…
MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
17
votes
2 answers

Kotlin Extension Functions - Override existing method

is it possible to do something like: /** * Converts all of the characters in the string to upper case. * * @param str the string to be converted to uppercase * @return the string converted to uppercase or empty string if the input was null …
Psest328
  • 6,575
  • 11
  • 55
  • 90
16
votes
2 answers

kotlin extension method access in another kt

I'm thinking about adding a global extension method to String in just one file,and wherever i use a String i can always use this extension. But i failed to find a way to do so...i just paste the extension everywhere now. extension here in…
Draculea
  • 303
  • 2
  • 7
16
votes
1 answer

Unit test on Kotlin Extension Function on Android SDK Classes

Kotlin extension function is great. But how could I perform unit test on them? Especially those that is of Android SDK provided class (e.g. Context, Dialog). I provide two examples below, and if anyone could share how I could unit test them, or if I…
Elye
  • 53,639
  • 54
  • 212
  • 474
16
votes
2 answers

Use Kotlin extension in android java class

Is it possible to use a kotlin extension in a android java class? Example: fun String.getSomething(): String { return "something" } then in Java use it like: String someString = "blabla"; someString.getSomething(); is this possible?
Informatic0re
  • 6,300
  • 5
  • 41
  • 56
15
votes
4 answers

Kotlin extension functions vs member functions?

I am aware that extension functions are used in Kotlin to extend the functionality of a class (for example, one from a library or API). However, is there any advantage, in terms of code readability/structure, by using extension functions: class Foo…
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
15
votes
4 answers

this reference in a lazy initializer of kotlin extension property

I'm trying Kotlin and want to implement a lazy extension property for Activity: /** * Activity module */ val Activity.activityModule: ActivityModule by lazy { ActivityModule(this) } The compiler errors with: 'this' is not defined in this…
Motorro
  • 227
  • 2
  • 10
1
2
3
22 23