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
2
votes
2 answers

Kotlin Extension Function

Considering this: MyView.setVisibility(View.VISIBLE) can be simplified to this: inline fun View.setVisible() = apply { visibility = View.VISIBLE } MyView.setVisible() Or this if you prefer: inline infix fun View.vis(vis: Int) = apply { visibility…
johnny_crq
  • 4,291
  • 5
  • 39
  • 64
2
votes
3 answers

How do I use an extension function in another class? C#

I've got a couple of extension functions that I want to transfer between classes. I have a class called Helpers.cs that I want to have the following: namespace XYZ { public class Helpers { public static string Encrypt(this string…
2
votes
2 answers

Setup Guice bindings in Kotlin

I want to setup Guice bindings so I created a module in Java that works perfectly: public class CrashLoggerModule extends AbstractModule { @Override public void configure() { bind(CrashLogger.class).to(ConcreteCrashLogger.class); …
Michael
  • 53,859
  • 22
  • 133
  • 139
1
vote
1 answer

How to call Kotlin extension function from Groovy code?

I am writing a Gradle plugin in Kotlin language using kotlin-dsl plugin: plugins { `kotlin-dsl` } I have created a plugin class and an extension: class MyPlugin : Plugin { private lateinit var extension: MyPluginExtension …
1
vote
1 answer

Why cannot I create extension to BigDecimal in Kotlin?

I am trying to create an extension as a property and I experimented with an extension function as well, below an example for BigDecimal: val BigDecimal.HUNDRED: BigDecimal get() = TEN.multiply(TEN) fun BigDecimal.HUNDRED_ONE(): BigDecimal { …
Dmytro Chasovskyi
  • 3,209
  • 4
  • 40
  • 82
1
vote
0 answers

How to get Dagger2 class property in extension function?

I'm using Dagger2, and would like to use my app's version string in an extension function for a third party class. The app exposes the version as so currently: BuildConfigProvider.kt interface BuildConfigProvider { val version:…
user1114
  • 1,071
  • 2
  • 15
  • 33
1
vote
1 answer

Create extension function to assign resource to ImageView with Kotlin

I am starting to work with the Kotlin fun extensions. I've created some that are working correctly for me, but I have one with the ImageView context that doesn't work and I don't understand why. The extension function is this: fun…
Sergio76
  • 3,835
  • 16
  • 61
  • 88
1
vote
0 answers

How to supply side effect for RxBinding.textChanges() and preserve it's functionality on client side?

Suppose that i'm writing my own View class, that represents editable field and contains EditText tv_input inside: class EditTextIconItemView : LinearLayout { fun setInputText(text: String?) { with (tv_input) { setText(text) …
1
vote
2 answers

Can I use an extension *member* from outside the class?

If I have an extension function for type A declared inside class B: class A class B { fun A.foo() = "Hello" } Can I call this function at all from code that is outside class B? val a = A() val b = B() a.foo() // error: unresolved…
Tobia
  • 17,856
  • 6
  • 74
  • 93
1
vote
0 answers

Disambiguating kotlin generic functions

Background: I'm working on a small code generator. I would like to avoid the usual problem of no code hints before the code is actually generated. To solve this I created a general generic dummy method of the sort I intend to generate. inline fun…
Lukasz
  • 2,257
  • 3
  • 26
  • 44
1
vote
2 answers

Companion object with extension function in kotlin?

I would like to have extension function and use logger from kotlin-logging and have constants inside companion object. My function: fun String.toFoo(): Foo { logger.debug { "Mapping [$this] to Foo" } if(MY_CONST.equals(this) { …
pixel
  • 24,905
  • 36
  • 149
  • 251
1
vote
1 answer

Kotlin - Make extension function accessible in whole project

I have this extension function in a class: fun Double.round2() : Double { return Math.round((this) * 100.0) / 100.0 } I want to reuse this function in another class without the need to duplicate the code. How can I do it? Thank you.
1
vote
1 answer

Kotlin/Android: Finding all Views of type T recursively

So I have this extension function for ViewGroup : inline fun ViewGroup.allViewsOfType(action: (T) -> Unit) { val views = Stack() afterMeasured { views.addAll((0 until childCount).map(this::getChildAt)) …
Dale Julian
  • 1,560
  • 18
  • 35
1
vote
1 answer

saxon extension function with NODE_SEQUENCE or ANY_SEQUENCE

I am switching from Saxon 9.3 (PE) to more recent version. I am using Extension functions (with "extends ExtensionFunctionDefinition") I have arguments defined as public SequenceType[] getArgumentTypes() { return new…
Fabien
  • 11
  • 2
1
vote
3 answers

Kotlin extension function with Generic doesn't work with Any?

I want to have an extension function from the RealmList class: private inline fun RealmList.saveAll() { this.forEach { item -> Realm.getDefaultInstance().insert(item!! as RealmModel) } } But whenever I use it, this error…
Dale Julian
  • 1,560
  • 18
  • 35