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
241
votes
9 answers

Accessing Kotlin extension functions from Java

Is it possible to access extension functions from Java code? I defined the extension function in a Kotlin file. package com.test.extensions import com.test.model.MyModel /** * */ public fun MyModel.bar(): Int { return…
Lovis
  • 9,513
  • 5
  • 31
  • 47
55
votes
5 answers

Kotlin Extension Functions Databinding

Is there any possibility to use extension function with a databinding? XML:
user2725105
  • 669
  • 1
  • 5
  • 15
39
votes
8 answers

Capitalise every word in String with extension function

I want to make a extension function in Kotlin, that converts the first letter of each word of the string to upper case the quick brown fox to The Quick Brown Fox I tried using the capitalize() method. That only though capitalised the first letter…
Abdullah Nagori
  • 441
  • 1
  • 5
  • 10
32
votes
2 answers

difference between kotlin also, apply, let, use, takeIf and takeUnless in Kotlin

I read many Kotlin documents about these items. But I can't understand so clearly. What is the use of Kotlin let, also, takeIf and takeUnless in detail? I need an example of each item. Please don't post the Kotlin documentation. I need a real-time…
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
21
votes
4 answers

Kotlin - How i can access my new class extension function from another file

I am very new to Kotlin. I want to be able to add a function to my abstract class, so when I define that function I will be able to invoke that on every child from that class(they inherit the abstract class) However,I want to define those extension…
K.Os
  • 5,123
  • 8
  • 40
  • 95
16
votes
1 answer

Global extension function in kotlin

Hey I want to make a class in kotlin that will hold all extension functions that I will use in a few places for example: class DateUtils { //in this case I use jodatime fun Long.toDateTime() : DateTime = DateTime(this) fun…
Gil Goldzweig
  • 1,809
  • 1
  • 13
  • 26
13
votes
4 answers

How to create an extension function with multiple receivers in Kotlin?

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 to call CoroutineScope.launch() and Iterable.map()…
11
votes
4 answers

Where to declare Kotlin extension functions in an Android app

Suppose I have the following code that I want to make as a re-usable component: fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] =…
Johann
  • 27,536
  • 39
  • 165
  • 279
11
votes
3 answers

Testing extension functions inside classes

If we want to test an extension function on a type, we can create an instance of this type, call the function and check the returned value. But what about testing extension functions defined inside classes? abstract class AbstractClass { fun…
JavierSA
  • 783
  • 1
  • 10
  • 25
10
votes
1 answer

Difference between private top-level extension function and private extension function inside class

We are currently switching our project to Kotlin, and ran across following question: We need a certain extension function only inside a given class. Thus, we have two possibilities: (1) Declaring the extension function private on the file top-level…
Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
10
votes
4 answers

overloading + and += operators for "Number Classes"

I want to create extension functions for classes that encapsulate simple Numbers. For example DoubleProperty. I encountered the problem, that I can't overload the + and the += operator at the same time. I wan't to create a behaviour, that passes…
Jhonny007
  • 1,698
  • 1
  • 13
  • 33
8
votes
2 answers

How does a extension method work in Kotlin?

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 workings of Kotlin Extension Methods.
Prabesh
  • 87
  • 1
  • 7
8
votes
2 answers

Kotlin: How can I call super's extension function?

How can I call a super's extension function? For example: open class Parent { open fun String.print() = println(this) } class Child : Parent() { override fun String.print() { print("child says ") super.print() // syntax…
Jire
  • 9,680
  • 14
  • 52
  • 87
7
votes
2 answers

Can extension functions be called in a "static" way?

Is it possible to create an extension function and call it as if it were static? For Example... fun System.sayByeAndExit() { println("Goodbye!") System.exit() } fun main(args: Array) { System.sayByeAndExit() // I'd like to be…
byxor
  • 5,930
  • 4
  • 27
  • 44
6
votes
3 answers

How to write a Kotlinesque extension function in Typescript?

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(): UserDto { TODO() } In Typescript if I have a…
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1
2 3 4 5 6 7