Skip to main content
Filter by
Sorted by
Tagged with
Best practices
0 votes
2 replies
46 views

I am expecting that only the first line of main function will be compiled. fun<T> T.shouldBe(arg:T) : Boolean { return this == arg } fun main() { 1.shouldBe(2) 1.shouldBe(true) ...
Nikita Chegodaev's user avatar
1 vote
2 answers
56 views

Consider the following functions: fun Int.ext() { // TODO } fun doSomething(f: Int.() -> Unit) { // TODO } The functional parameter f has the same type as the extension function ext. So, ...
birojow's user avatar
  • 63
0 votes
0 answers
28 views

While doing some refactoring I came across the following scenario where I had a method with a single parameter like this: private fun doSomething(thing: Thing) {} and I thought as this method just ...
franks's user avatar
  • 309
0 votes
1 answer
71 views

I'm trying to create an extension function I can apply to collections similar to filter/map/etc. Typing due to generics is pretty loose and I want to be more strict. Is there anything I can do in ...
Claudio's user avatar
  • 83
2 votes
0 answers
48 views

Consider this example of a fictive old Java library, which I can't change but want to adapt for my Kotlin projects. public final class SomeOldJavaClass { private SomeOldJavaClass() { } public ...
java.is.for.desktop's user avatar
0 votes
1 answer
160 views

I have tried upgrading from SaxonCS 11 to 12 after my ExtensionFunctions are no longer called I get the following exception when I try to compile my xslt. Saxon.Hej.trans.XPathException: 'Cannot find ...
Kingpin's user avatar
  • 1,164
1 vote
1 answer
1k views

I am writing a Gradle plugin in Kotlin language using kotlin-dsl plugin: plugins { `kotlin-dsl` } I have created a plugin class and an extension: class MyPlugin : Plugin<Project> { ...
Vadik Sirekanyan's user avatar
0 votes
3 answers
1k views

I am trying to create an extension function on java.lang.System to refactor currentTimeMillis usage to the following System.currentTimeinSeconds() extension method that returns a String. (This is done ...
B.56's user avatar
  • 11
0 votes
1 answer
1k views

i want to stub kotlin's map extension function in my unit test. couldn't find any proper solution on internet to do that. ctually i have put empty blocks inside curly brackets but it seems like this ...
kokoko's user avatar
  • 4,128
0 votes
1 answer
248 views

I have a flow that return my event type generic like Flow<BaseResourceEvent<T>> But sometimes I have to convert T type to another type. For example User type to DashBoardUser with an ...
emreturka's user avatar
  • 876
0 votes
0 answers
28 views

Kotlin defines an infix operator to for building Pairs, which is basically a shorthand for calling the constructor of Pair. In other words val pair = "verbose" to true is the same as val ...
tunnuz's user avatar
  • 24.2k
0 votes
1 answer
193 views

I want to make extension function using TextInputEditText and call addTextChangedListener, but when i run the app and using the feature, my app crash. My goal is to create an extension function ...
Yosua Haloho's user avatar
5 votes
2 answers
7k views

In Kotlin language, what does this syntax do and how does it work ? class ClassName1 { companion object { fun ClassName2.funName()="" } }
Imane's user avatar
  • 85
1 vote
1 answer
413 views

I would like to create an extension function for a collection to check if one collection contains any item of defined set. I think about two implementations: infix fun <T> Iterable<T>....
Francis's user avatar
  • 7,164
20 votes
4 answers
14k views

I want my extension function to have a couple of receivers. For example, I want function handle to be able to call methods of both CoroutineScope and Iterable instances: fun handle() { // I want ...
Sergio's user avatar
  • 31.1k
3 votes
0 answers
1k views

Is it possible to apply an extensions function to several types at once? Something link this fun <Double || Float>.toString(): String
yaugenka's user avatar
  • 2,891
4 votes
1 answer
2k views

below is my Kotlin extension function : infix fun Int.send(data: String) { MsgSendUtils.sendStringMsg( this, data ) } called like this : 8401 send "hi" and my ...
Hui He's user avatar
  • 187
1 vote
1 answer
882 views

I have a class (class A) to which I define an extension function (A.extension()) inside a companion object of another class (class B) for a matter of organization. On my tests I need: To use a real ...
LeYAUable's user avatar
  • 1,782
1 vote
1 answer
524 views

I am trying to create an extension as a property and I experimented with an extension function as well, below an example for BigDecimal: val BigDecimal.HUNDRED: BigDecimal get() = TEN.multiply(TEN)...
Dmytro Chasovskyi's user avatar
5 votes
2 answers
1k views

Introduction In Kotlin I have a generic conversion extension function that simplifies conversion of this object of type C to an object of another type T (declared as the receiver) with additional ...
goshki's user avatar
  • 130
0 votes
1 answer
146 views

I am trying to create a infix notation as a extension function of (Int) -> Int function which is used to nest a function to another. For example: class Entry { companion object { ...
Langua's user avatar
  • 3
0 votes
1 answer
947 views

I am trying to write extension function in kotlin. I came almost to the end but one simple thing stopped me. Code goes like this: fun Bundle.applyWithUserParameters(vararg functionList: () -> Unit =...
Kratos's user avatar
  • 1,007
0 votes
0 answers
391 views

I am new to unit testing, and facing issue with a extension function, which is kind of: fun List<Water>.getFileteredList() = return listOf(filteredWater) Now if I want to test this function, ...
Ankush's user avatar
  • 185
4 votes
2 answers
1k views

Consider the following interface interface EntityConverter<in A, out B> { fun A.convert(): B fun List<A>.convert(): List<B> = this.map { it.convert() } } I want to use it ...
Linde_98's user avatar
  • 418
3 votes
3 answers
4k views

Is there a way to inject an object inside an extension function or a global function using DI framework in Android Kotlin? I use this function in many places. So I do not want to pass a parameter ...
Yeldar Nurpeissov's user avatar
0 votes
1 answer
307 views

I am not able to understand extension function and use it in my project. Can anyone guide me here please?
user avatar
0 votes
1 answer
329 views

val test: Int.(String) -> Int = { plus((this)) } When defining this type of extension function, how can I use arguments( Here, the argument of type String) inside the block? When ...
ybybyb's user avatar
  • 1,809
-1 votes
1 answer
429 views

I have a custom progress bar class that I want to convert to an extension function so that i can use it any where in the project (Both fragment and activity) without initialising. I want to be able to ...
Oluwafemi's user avatar
  • 366
-2 votes
2 answers
266 views

I want to make an extension function(named (printDatatype) for example) that can be applied on all datatypes and just prints it ... for example : "example".printDatatype() ==>output: ...
Omar Younis's user avatar
1 vote
0 answers
132 views

I'm using Dagger2, and would like to use my app's version string in an extension function for a third party class. The app exposes the version as so currently: BuildConfigProvider.kt interface ...
user1114's user avatar
  • 1,189
5 votes
2 answers
2k views

I see some usages of Extension functions in Kotlin I don't personally think that makes sense, but it seems that there are some guidelines that "apparently" support it (a matter of ...
Lior Bar-On's user avatar
  • 11.7k
8 votes
3 answers
802 views

I Kotlin if I have an interface like this: interface User { val name: String val email: String } I can write an extension function like this anywhere in the code: fun User.toUserDto(): ...
Adam Arold's user avatar
  • 30.8k
0 votes
3 answers
452 views

Lets say I have class A(val foo: Double). I want to be be able to compare it to other A, Double, and Int. If I implement Comparable, I can only compare it against one other object type. override fun ...
user avatar
0 votes
1 answer
110 views

With my Saxon extension function code I have the log messages: > java -cp ./saxon-he-10.2.jar:./ net.sf.saxon.Transform -t -init:MyInitializer -xsl:./exttest.xsl -o:./out.xml -it:initialtemplate ...
user avatar
2 votes
0 answers
2k views

I would like to access Kotlin's extension functions/values through reflection. I have a very simple example, but I cannot figure out what is wrong here import kotlin.reflect.full.* class A fun A.ext(...
Victor Ermolaev's user avatar
0 votes
1 answer
868 views

I am trying to make an extension function to use it in a different class. I am trying to create it but it does not work. Can anyone solve this problem. Here is my extension: fun resize.toBitmap(image: ...
Muhittin Kaya's user avatar
0 votes
1 answer
509 views

For some reason. I am unable to define an extension operator function for the operators ++ and -- while being able to define member operator functions for the same operators. @TestInstance(...
user avatar
8 votes
2 answers
4k views

In Java you cannot extend a final class, but in Kotlin you can write extension method to such final classes. How did they make it work internally? I couldn't find any article that explained the inner ...
Prabesh's user avatar
  • 87
0 votes
1 answer
627 views

I have a data class User data class User(name: String?, email: String?) and I created an extension function to get the best identifier (name first, then email) fun User.getBestIdentifier(): String { ...
Brandon Xia's user avatar
2 votes
0 answers
703 views

Running into an Issue early on in importing a declared extension function in another Kotlin File (Extensions.kt), call the extension function From another class (ForecastsRepository.kt) it doesn't ...
Ben's user avatar
  • 23
0 votes
1 answer
593 views

Suppose I have a function (in Kotlin over Java): fun <E> myFun() = ... where E is a general type I know nothing about. Can I determine within this function whether there exists an extension ...
mpts.cz's user avatar
  • 261
3 votes
1 answer
966 views

String division using operator overloading in Kotlin, please help me to complete this code. No change can be made in main function. Find common from both string like a=aabcc b=abdec answer=abc (...
divyesh's user avatar
  • 31
3 votes
1 answer
73 views

I came across a strange bug and I can't figure out why it occurs. If I invoke my original function, roundToMidnight() is not called and the date is not rounded. My original function, what doesn't ...
Michiel's user avatar
  • 768
0 votes
1 answer
109 views

To sum up the question in a few words, here is the catch: The also(strings::add) doesn't work, it says Type inference failed fun test() = "Test String" val strings = mutableListOf<String>() //...
Animesh Sahu's user avatar
  • 8,284
0 votes
0 answers
61 views

My question is not really technical, so there will be no definite right or wrong. For extension functions, I have a package containing a file for every type: Context.kt contains all extension ...
Michiel's user avatar
  • 768
1 vote
1 answer
1k views

I am starting to work with the Kotlin fun extensions. I've created some that are working correctly for me, but I have one with the ImageView context that doesn't work and I don't understand why. The ...
Sergio76's user avatar
  • 3,996
0 votes
1 answer
53 views

I have a default function like this: fun <T> makeDefault(): Animal<T> = Animal<Nothing>( name = "", size = 0, age = 0 ) I saw that there is the by operator, which ...
J_Strauton's user avatar
  • 2,438
0 votes
1 answer
765 views

I'm trying to use toRegex() extension function from the kotlin.text package from my Java code. Here is the code of that file: /* * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is ...
Sergey Emeliyanov's user avatar
0 votes
1 answer
291 views

I try to use the list extension function binarySearch public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Int ...
mark's user avatar
  • 133
0 votes
2 answers
314 views

I'm trying to make this function accessible from all files but if I define it outside class in Kotlin file, it shows me error for getSharedPreferences as unresolved reference. I think this is my ...
Pranciskus's user avatar