Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.
Questions tagged [kotlin-extension]
333 questions
0
votes
1 answer
Is there any difference between "receiver object" and "extension receiver"
It's hard to tell acording to the docs https://kotlinlang.org/docs/reference/extensions.html
so I wonder are receiver object and extension receiver the same? or does the name depends on context?

Igor
- 2,039
- 23
- 27
0
votes
1 answer
How to create variables on parent context
I'm trying to implement something similar to this syntax in Kotlin
class MyClass() {
fun before(init: () -> Unit): Unit {
with(this) init
}
fun after(block: () -> Unit): Unit {
with(this) block
}
}
fun main () {
…

Hartimer
- 525
- 6
- 20
0
votes
1 answer
Why doesn't Kotlin let me use writeText extension?
I have a Kotlin class with a method, which creates some text and then I want to write it to a file:
import java.io.File
import java.util.*
import kotlin.io.*
class MyClass {
fun run() {
val result = html {
head {
…

Glory to Russia
- 17,289
- 56
- 182
- 325
-1
votes
3 answers
how to add text in top border of view in andriod
I am new to android development please help me in creating this type design
I want to add text in the center of the top borders in the card
please help me or guide me to achive the same view

Vuyyururajashekar Reddy
- 83
- 1
- 6
-1
votes
2 answers
Generic extension function Kotlin
Since getParcelable is deprecated for Android Tiramisu and later i want to make an extension function to get the parcelables from my Intent through my Bundle
So i have
val server = if (Build.VERSION.SDK_INT >= 33) {
…

james04
- 1,580
- 2
- 20
- 46
-1
votes
2 answers
Java own List function for .NullOrEmpty
In Java you have a lot of List Functions. e.g: List.size(), List.isEmpty()...
Is it possible to make my own Function like List.nullOrEmpty() and how can i do this?
i create a class who do this.
public class ListUtil {
public static boolean…

Shoxious
- 11
- 3
-1
votes
1 answer
kotlin property extensions vs local class extensions, which is better?
Anyone know why i would want to use a property extension as opposed to just defining a local class extension? here is an example of a property extension that i have tried:
class MyClass {
val String.concatMyName: String
get() {
…

j2emanue
- 60,549
- 65
- 286
- 456
-1
votes
1 answer
pass function with parameters in extension function(kotlin)
I am trying to create an Extension function in android with Handlers but facing issue:
Extension fun Code:
fun delayTask(millis: Long, myFunction: (data:String) -> Unit) {
Handler().postDelayed({
myFunction(data) //why error is of…

Sumit Shukla
- 4,116
- 5
- 38
- 57
-1
votes
1 answer
In Kotlin Programming language importance of varargs in terms of uses
Let me know what is the importance of varargs in Kotlin, if there is any document or some useful links. Please share.

Nishant Gupta
- 457
- 3
- 16
-1
votes
2 answers
Check if my Activity is in MultiWindowMode or not using Kotlin
How to check if my activity is in MultiWindowMode or not.
I have used Myactivity.isInMultiWindowMode() and MYactivity.isInPictureInPictureMode(), but showing error
Do I have to extend something as MultiWindow is not recognizing
I have gone through…

Pranav Ashok
- 581
- 1
- 6
- 18
-1
votes
2 answers
How to run Different .kt file/Class in same src folder?
I am new in kotlin , I started to learn Kotlin Language by installing intellij IDEA , now the issue is when i want to run second .kt file by run button its only showing what i had run first .kt file .
Always showing Run HelloWorld.kt, I want to…

Abhishek kumar
- 4,347
- 8
- 29
- 44
-2
votes
2 answers
Kotlin inner class can't resolve extension function declared in outer class
Why inner class in Kotlin can't not access extension function declared in outer class as below:
class A{
val a = "as".foo() // LINE 1
class B{
val b = "as".foo() // LINE 2
}
fun String.foo(){}
}
On LINE 1 extension…

Farid
- 2,317
- 1
- 20
- 34
-2
votes
1 answer
how to use distinct operator
would you please let me know the reason why I am getting the output mentioned below??
I expected to get the following out put:
[Person(_name=xyz4, _age=30),
Person(_name=xyz5, _age=50)]
because of distinct operator
Main:
fun main(args:…

Amrmsmb
- 1
- 27
- 104
- 226
-2
votes
3 answers
Kotlin : Create extension function file?plus
I am developing some lib in Kotlin, I am not finding this solution,
How can I resolve this issue, It's saying that create extension function File?.plus,
In java it works fine but in Kotlin how to write this code?

Mohit Suthar
- 8,725
- 10
- 37
- 67
-3
votes
2 answers
how to pass object from one activity to another activity in android Kotlin?
I am new to android Kotlin. I am trying to Pass object from one activity to another activity I got stuck,
MyClass :
data class MyPojo(val name: String, val age: String) : Serializable
FirstActivity :
class MainActivity : AppCompatActivity()…

Siva
- 81
- 1
- 9