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

Filter null keys and values from map in Kotlin

I have an extension function that filters away entries with null keys or values: fun Map.filterNotNull(): Map = this.mapNotNull { it.key?.let { key -> it.value?.let { value -> key to value } …
neu242
  • 15,796
  • 20
  • 79
  • 114
3
votes
1 answer

Attach context for base object of kotlin extension function

This question is specific for extension function of Kotlin used in Android development. So Kotlin provides us capability to add certain extension behavior in to a class to extend the based class behavior. Example: (take from my current Android…
toantran
  • 1,789
  • 17
  • 25
3
votes
2 answers

Kotlin strange behaviour when calling extension function on java.lang.reflect.Proxy object

Today I played with some java.lang.reflect.Proxy in Kotlin, and I was surprised by this behaviour: import java.lang.reflect.Proxy interface Dog { fun bark() fun bark3Times() } class DogImpl : Dog { override fun bark() = println("Bark!") …
xinaiz
  • 7,744
  • 6
  • 34
  • 78
3
votes
0 answers

Why inc() cannot be defined as an extension function in a class different from the receiver type?

This is what I encountered when I was doing Koan N28: class DateRange(override val start: MyDate, override val endInclusive: MyDate) : ClosedRange, Iterable { private operator fun MyDate.inc() : MyDate { return…
3
votes
3 answers

Change name from this@ on kotlin

I have the follow situation, an extension function named as save from Marker 1. The code fun Marker.save(ctx: Context) { //database is an extension property from Context ctx.database.use { insert("markers", "id" to…
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
3
votes
2 answers

In C# how do I create an extension method that takes a lambda with generic parameters, Func as a parameter.

I'm trying to create an extension method that takes a Func as a parameter but I'm getting an error. Here's what I got: public static class MyExtensions { public static TResult ModifyString(this string s, Func f) { …
Joels Elf
  • 714
  • 6
  • 10
2
votes
3 answers

Dependency injection inside extension function

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 every time. DI framework can be any of Koin, Hilt,…
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
2
votes
0 answers

Access extention functions (values) through reflection in Kotlin

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() = 2 val A.value: Int get() = 2 fun main()…
Victor Ermolaev
  • 721
  • 1
  • 5
  • 16
2
votes
0 answers

Can't import extension function in Android App Kotlin

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 compile but when i remove it there is no problem…
Ben
  • 23
  • 3
2
votes
2 answers

Define Kotlin extension function in java code

I'd like to have a Kotlin extension function for string that is accessible only in inheritors of MyParent class (protected function). MyParent class is written in java and cannot be converted to Kotlin. Is it possble to define a method in java code…
Feedforward
  • 4,521
  • 4
  • 22
  • 34
2
votes
2 answers

Open Close Principle and Extension Functions

I have learned that Open Close Principle is allowing extension to classes and restricting from modification. So in Kotlin, when we use extension function Are we extending a class Or are we modifying a class Can extension functions in kotlin be an…
Mehroze Yaqoob
  • 1,011
  • 1
  • 12
  • 29
2
votes
3 answers

Kotlin - Val cannot be reassigned , but can be reassigned by accessing from index?

I have these two functions where i am trying to modify the elements. One of them compiles and other says 'val cannot be reassigned'. What is the difference between the following functions? Why does one compile and the other does not? The one that…
Dishonered
  • 8,449
  • 9
  • 37
  • 50
2
votes
2 answers

How to access class members with same name in extension function in Kotlin android

i'm new to kotlin for android. and i want to access class member that having same name in extension function. for example: var visibility = null //class level variable //EXTENSION FUNCTION fun ProgressBar.changeVisibleState(flag:…
Pradeep Kumar
  • 586
  • 3
  • 19
2
votes
0 answers

Getting a NoClassDefFoundError when calling a kotlin extension function

I'm using the Android Studio 3.0 Beta 2 Canary Preview, and am calling a Kotlin extension function from Java: The Kotlin code: fun Metadata.validate(context: Context) { validateInnerList(context) // other validation functions } private fun…
MichaelThePotato
  • 1,523
  • 2
  • 10
  • 19
2
votes
2 answers

Accessing static extension function from another class in Kotlin?

Let's say we have the following extension function: class Helper { companion object { fun Int.plus(value: String) = Integer.valueOf(value).plus(this) } } How can you access the plus extension function from the Helper class in another class.…
MoMo
  • 186
  • 1
  • 3
  • 15