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

What is the difference between Comparable and operator compareTo?

Lets say I have class A(val foo: Double). I want to be be able to compare it to other A, Double, and Int. If I implement Comparable, I can only compare it against one other object type. override fun compareTo(other: A): Int { return when { …
user2211907
0
votes
1 answer

What does "Building tree for null using TinyBuilder" mean with Saxon extension function and using -t option?

With my Saxon extension function code I have the log messages: > java -cp ./saxon-he-10.2.jar:./ net.sf.saxon.Transform -t -init:MyInitializer -xsl:./exttest.xsl -o:./out.xml -it:initialtemplate Saxon-HE 10.2J from Saxonica Java version…
user5924595
0
votes
1 answer

How to create extension function to resize an image in Kotlin?

I am trying to make an extension function to use it in a different class. I am trying to create it but it does not work. Can anyone solve this problem. Here is my extension: fun resize.toBitmap(image: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap…
0
votes
1 answer

Unable To Define An Operator Extension Function For The Increment and Decrement Operators

For some reason. I am unable to define an extension operator function for the operators ++ and -- while being able to define member operator functions for the same operators. @TestInstance(TestInstance.Lifecycle.PER_CLASS) object…
user5132301
0
votes
1 answer

Do Kotlin Extension Functions declare member variables implicitly?

I have a data class User data class User(name: String?, email: String?) and I created an extension function to get the best identifier (name first, then email) fun User.getBestIdentifier(): String { return when { …
Brandon Xia
  • 407
  • 3
  • 19
0
votes
1 answer

Kotlin: How can I determine the extension function exists

Suppose I have a function (in Kotlin over Java): fun myFun() = ... where E is a general type I know nothing about. Can I determine within this function whether there exists an extension function E.extFun()? And if so, how?
mpts.cz
  • 221
  • 1
  • 10
0
votes
1 answer

Unexpected behavior of also/apply, cannot pass references of a instance function into also/apply

To sum up the question in a few words, here is the catch: The also(strings::add) doesn't work, it says Type inference failed fun test() = "Test String" val strings = mutableListOf() // Type inference failed: inline fun T.also(block:…
0
votes
0 answers

Primary variable convention for extension function

My question is not really technical, so there will be no definite right or wrong. For extension functions, I have a package containing a file for every type: Context.kt contains all extension functions for Context, Uri.kt contains all extension…
Michiel
  • 767
  • 4
  • 19
0
votes
1 answer

How to write my own by extension function

I have a default function like this: fun makeDefault(): Animal = Animal( name = "", size = 0, age = 0 ) I saw that there is the by operator, which can be used for view models like this: val model: MyViewModel by…
J_Strauton
  • 2,270
  • 3
  • 28
  • 70
0
votes
1 answer

Calling Kotlin extension function from Java from kotlin.text package

I'm trying to use toRegex() extension function from the kotlin.text package from my Java code. Here is the code of that file: /* * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be…
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
0
votes
1 answer

How to restrict the generic extension function parameter type?

I try to use the list extension function binarySearch public fun List.binarySearch(element: T, comparator: Comparator, fromIndex: Int = 0, toIndex: Int = size): Int This is my definition: open class Z open class ZZ : Z() open class ZZZ…
mark
  • 133
  • 1
  • 7
0
votes
2 answers

How to define top-level function with sharedPreferences inside?

I'm trying to make this function accessible from all files but if I define it outside class in Kotlin file, it shows me error for getSharedPreferences as unresolved reference. I think this is my answer but I don't know how to declare top-level…
Pranciskus
  • 487
  • 1
  • 6
  • 17
0
votes
1 answer

What is a user case for creating an extension function as a method inside a class/interface?

I saw some examples of extension functions being defined inside a class/interface but I didn't understand the reason it would be done. Could someone show when it would be the proper way to implement some use case? One particular example that I…
Diego Marin Santos
  • 1,923
  • 2
  • 15
  • 29
0
votes
1 answer

why the methods /propreties of an interface can be passed in an extension function

Today I had to use an extension function which had as predicate an Interface, a kind of Interface.whateverExtensionFunction() and a I had a class ViewModel () :InterfaceInherithingFromAnotherInterface. The architectural idea behind is to…
Drocchio
  • 383
  • 4
  • 21
0
votes
1 answer

What is the effect of access modifiers on extension functions?

What will happen if I change the access modifier of an extension function to private? Does it have any effect? Does it matter?! private fun String.myExtensionFunction() { // ... }
Mahozad
  • 18,032
  • 13
  • 118
  • 133