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
4
votes
1 answer
Kotlin Android Extensions vs Android Data Binding Library, memory usage
I have a question about memory used in both cases ->Android Data Binding vs Android Kotlin Extensions.
In which case will be less memory used on the device?
Kotlin Android-extension is calling first findViewById and after that, the result will be…

LaviniaDragunoi
- 43
- 5
4
votes
4 answers
Could not find method androidExtensions()
I want to use the features of kotlin @Parcelize,
I have apply the plugin: 'kotlin-android-extensions' on gradle, and I added
androidExtensions {
experimental = true
}
but errors continue to appear.this error message :
Error:(28, 0) Could…

salim
- 95
- 2
- 5
4
votes
2 answers
Swift enumerated equivalent in Kotlin
In a 3x3 matrix representation, i can find the sum of both diagonals with one liners in Swift as below,
let array = [
[1, 2, 3],
[4, 5, 6],
[-7, 8, 9]
]
let d1 = array.enumerated().map({ $1[$0] }).reduce(0, +)
let d2 =…

Kamran
- 14,987
- 4
- 33
- 51
4
votes
2 answers
Kotlin databinding with extension methods
I'm trying to use Kotlin extension methods inside Android's databinding. For example; calling an onclick handler. So I've made this code:
posttest_list_item.xml
…

Milan
- 61
- 5
4
votes
2 answers
How to parse below Json data in Kotlin?
I need to parse this information-
[
{
"artist": "12",
"image": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id": "12",
"video_title": "23"
},
{
"video_id":…

shivam Kapoor
- 105
- 2
- 3
- 9
4
votes
3 answers
add extension on Log in android (Kotlin)
I use this code to add extension for Log class android
fun Log.i2(msg:String):Unit{
Log.i("Test",msg)
}
when using in the activity
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
…

Rasoul Miri
- 11,234
- 1
- 68
- 78
4
votes
1 answer
How to reference anonymous inner class in Kotlin?
I have following extension method. How can I reference the OnGlobalLayoutListener that is passed into the addOnGLobalLayoutListener() method? I need to pass the listener to the removeOnGlobalLayoutListener() method.
fun…

jellyfication
- 1,595
- 1
- 16
- 37
3
votes
1 answer
Can Kotlin extension functions be called without an import declaration?
Is it possible to call an extension function from another package without importing it?
Given an extension function:
package ext
fun Int.plusOne() = this + 1
Is there any way to call this function without importing the function first?
I can call…

Kolja
- 1,197
- 9
- 17
3
votes
2 answers
Casting Integer to Unit compiles successfully
Why does this program prints kotlin.Unit when it should fail with ClassCastException at runtime?
class Animal {
}
fun Animal.extension(block: ()-> T){
print(block())
}
fun main(){
//(4 as Unit) //Runtime ClassCastException,…

Omkar T
- 755
- 8
- 19
3
votes
0 answers
@StringRes annotation fails for extension function return type
I have an extension function for an enum that return a string resource.
In order to make sure that the function only returns a valid String resource, I have annotated the function with @StringRes.
@StringRes
fun Fruit.getDescription(): Int {
…

rgv
- 1,186
- 1
- 17
- 39
3
votes
1 answer
Kotlin, implement extension var property
It seems that a simple extension property like the following does not work.
var Dog.age = 0;
What is the recommended way to implement this? I have tried the following, and it worked, but this will prevent any Dog object from cleaned up by the…

Damn Vegetables
- 11,484
- 13
- 80
- 135
3
votes
1 answer
Error: Call requires API level 24 (current min is 21): java.util.Map#forEach [NewApi]
I have two enum class EventKey and EventProperty
enum class EventKey(val firebaseKey: String? = null) {
SIGNIN(firebaseKey = "singin"),
.....
}
enum class EventProperty(val property: String) {
TYPE1("type1"),
....
}
I am…

Kotlin Learner
- 3,995
- 6
- 47
- 127
3
votes
2 answers
Kotlin “toString()” Not Available in Android DataBinding
Just learned DataBinding and find out that the powerful built-in toString() from Kotlin is not available:

Sam Chen
- 7,597
- 2
- 40
- 73
3
votes
2 answers
Add Extension function in kotlin to all classes
Is it possible to add extension function to all classes? I was thinking about adding it to some common base class like Object. Is it possible?

mike
- 1,670
- 11
- 21
3
votes
1 answer
How to return boolean value from a function in Kotlin
I am new to kotlin.
I am reading a key and value from properties file, in a kotlin program. But I don't know how to directly return the value of a key.
Please find the application.yml and abc.class(this is a kotlin class)…

umesh
- 312
- 3
- 4
- 15