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
6
votes
1 answer

Is there a way to show all the extension functions of a given Kotlin class in Intellij IDE?

The only way I've found so far is to create an instance of the given class and then using the autocomplete to see all possible functions. Obviously, this way is cumbersome and takes too much time. Is there a neater way to see all possible functions?
Oren
  • 118
  • 7
5
votes
1 answer

How to use a Kotlin extension function on a class?

I have a pretty short question about an extension function that would help clear some of my code. Basically I have some transformations on the hashCode of a class name and I want an extension function to do the transformations. Example: Getting the…
M'aiq the Coder
  • 762
  • 6
  • 18
5
votes
2 answers

Confusion with understanding lambda and receivers

Kotlin version 1.2.50 I have been following some examples on this tutorial on youtube https://www.youtube.com/watch?v=gPH9XnvpoXE. And there are a few things that I have understood, but there is still some confusion. I have left the comments in the…
ant2009
  • 27,094
  • 154
  • 411
  • 609
5
votes
1 answer

Kotlin extension log function with logback(slf4j)

I have create an extension function for logging: import org.slf4j.LoggerFactory fun Any.log(msg: String) { LoggerFactory.getLogger(javaClass.name).debug(msg) } But i'm not sure about will it be init any time when it will be called or not,…
user2870934
  • 679
  • 1
  • 7
  • 22
4
votes
2 answers

Kotlin : function extension inside companion object

In Kotlin language, what does this syntax do and how does it work ? class ClassName1 { companion object { fun ClassName2.funName()="" } }
Imane
  • 75
  • 1
  • 4
4
votes
2 answers

Providing only one type parameter to an extension function with multiple type parameters in Kotlin

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 conversion action that treats receiver as this and…
4
votes
2 answers

Kotlin: Idiomatic usage of extension functions - putting extension functions next to the class it extends

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 interpretation). Specifically: defining an extension function outside…
Lior Bar-On
  • 10,784
  • 5
  • 34
  • 46
4
votes
1 answer

Kotlin extension function of a function with receiver

I'm playing with Kotlin's extension functions. I'd like to create an extension function for boolean-returning function with receivers which returns the complement function. My goal is to be able to write: val isEven: Int.() -> Boolean = { this % 2…
4
votes
1 answer

How can I inline the receiver parameter of an extension function in Kotlin?

Inspired by this question, I was thinking about how could one inline the receiver parameter of an extension function? In theory, like this: inline fun not(crossinline predicate : (T) -> Boolean) = { e : T -> !predicate(e) } Just that…
msrd0
  • 7,816
  • 9
  • 47
  • 82
4
votes
1 answer

Kotlin KClass instance inside extension function

I need to access to class type inside a generic extension function. For example, if I want to make an extension function to return a collections of the members of the type I am extending, I would do: fun T.getMembers() = …
3
votes
0 answers

Apply extension function to several types at once in Kotlin

Is it possible to apply an extensions function to several types at once? Something link this fun .toString(): String
yaugenka
  • 2,602
  • 2
  • 22
  • 41
3
votes
1 answer

How can I use annotation @IntRange for Kotlin extension function for Integer

below is my Kotlin extension function : infix fun Int.send(data: String) { MsgSendUtils.sendStringMsg( this, data ) } called like this : 8401 send "hi" and my requirement is : the caller must greater than 8400…
Hui He
  • 77
  • 3
3
votes
1 answer

Operator overloading in Kotlin

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 (unique common characters) fun main(args:…
divyesh
  • 31
  • 1
3
votes
1 answer

Last extension function is not called

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 work: suspend operator fun invoke(reference:…
Michiel
  • 767
  • 4
  • 19