Questions tagged [extension-function]

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

100 questions
1
vote
2 answers

Kotlin extension field for SharedPreference value by string constant key

Extension functions are great for the SharedPreference api in android. Jake Wharton has an interesting implementation at time code 32:30 of this video tutorial where he implements SharedPreferences extension function like so: preferences.edit{ …
flopshot
  • 1,153
  • 1
  • 16
  • 23
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
0
votes
1 answer

After upgrading from SaxonCS from 11 to 12 ExtensionFunction are no longer working

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 a 2-argument function named…
Kingpin
  • 1,067
  • 2
  • 14
  • 34
0
votes
3 answers

How can I create an extension function on System class?

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 to avoid duplicate code and to reuse it in the…
B.56
  • 11
  • 1
0
votes
1 answer

how to stub kotlin map function | unittest

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 is not the way that i should do. here is what i…
aligur
  • 3,387
  • 3
  • 34
  • 51
0
votes
1 answer

flatMapConcat for convert

I have a flow that return my event type generic like Flow> But sometimes I have to convert T type to another type. For example User type to DashBoardUser with an extension function like below; fun User.toDashBoardUser():…
emreturka
  • 846
  • 3
  • 18
  • 44
0
votes
0 answers

What happens when I override a built-in operator, e.g., to, in Kotlin?

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 pair = Pair("verbose", true) In Kotlin, one can define a…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
0
votes
1 answer

I make extension function in android using kotlin and method from the data type, but my extension function not working properly?

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 addTextChangedListener for the TextInputEditText type…
0
votes
1 answer

Kotlin collection operations effectiveness

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 Iterable.containsAny(values: Iterable): Boolean =…
Francis
  • 6,788
  • 5
  • 47
  • 64
0
votes
1 answer

Mock extensions function inside Companion Object

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 class A instance . To mock A.extension(). To use a…
LeYAUable
  • 1,613
  • 2
  • 15
  • 30
0
votes
1 answer

type mismatch when create a kotlin infix notation to nest a function to another

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 { private fun f(x: Int) = x * 2 private fun g(x:…
0
votes
1 answer

vararg and extension function in kotlin

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 = null): Bundle = Bundle().apply { for (method in…
Kratos
  • 681
  • 1
  • 13
  • 30
0
votes
0 answers

Unit testing list extension function - Kotlin

I am new to unit testing, and facing issue with a extension function, which is kind of: fun List.getFileteredList() = return listOf(filteredWater) Now if I want to test this function, how can I provide the list to the function while…
Ankush
  • 175
  • 1
  • 12
0
votes
1 answer

Unable to understand extension function in Kotlin Android?

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

How to use parameters inside a block when defining an extension function?

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 defining extension functions at the same time as…
ybybyb
  • 1,385
  • 1
  • 12
  • 33