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
1 answer

Why am I getting "inferred type is Observable! but Observable was expected" with Observable Kotlin extension function?

I have the following extension function: import io.reactivex.Notification import io.reactivex.Observable import io.reactivex.subjects.Subject fun Subject>.fromNotifications(): Observable { return compose { …
serg66
  • 1,148
  • 1
  • 17
  • 31
0
votes
1 answer

Kotlin - Modifying Picasso's RequestCreator based on extension function arguments

I've an extension function to ImageView class. I've implemented some logic on how to load an image according to arguments passed. But here I'm stuck. These fit(), centerCrop() etc. return Picasso's RequestCreator, which I can't even construct (it…
Bartek Pacia
  • 1,085
  • 3
  • 15
  • 37
0
votes
2 answers

Kotlin extension function to an unknown type

Is it possible to use an extension function with a function that returns an unspecified value type? defaultPreferences(this)["some string"]?.let { ... I have to do this to avoid error, but I really want it to be in a single line. val value: String?…
0
votes
0 answers

Object extension function TypeScript Angular 6

why is when I attempt the code declare interface String { testsdfsdffds(); } String.prototype.testsdfsdffds = () => { }; it works, but substituting Object for String causes errors. I thought you could extend Object without issues? My goal…
Brad
  • 280
  • 4
  • 7
0
votes
1 answer

Add methods to multiple non-inheriting classes

Is there any way to add a method to multiple classes, or reduce the amount of duplicated code needed to achieve the same. At the moment I use the following (in this example to add a c method which count repetitions of items in the iterable): /**…
jrtapsell
  • 6,719
  • 1
  • 26
  • 49
0
votes
1 answer

OGC Filter Specification in Xpath

I've got some XML documents which conform to a known schema which include geometries in GML format. I'm looking to perform validation on the XML using XSD and Schematron validation, but I'll need some way of performing spatial queries within the…
GHC
  • 2,658
  • 5
  • 30
  • 35
0
votes
0 answers

Extension function collision

When I define two extension functions with the same signature I get an conflicting overloads error, even if the extension functions are defined in different files. Does this make different libraries that define the same extension function…
Dakkaron
  • 5,930
  • 2
  • 36
  • 51
-1
votes
1 answer

How may i convert a progress bar class to an extension function in kotlin?

I have a custom progress bar class that I want to convert to an extension function so that i can use it any where in the project (Both fragment and activity) without initialising. I want to be able to inflate the progress bar layout in the function…
Oluwafemi
  • 306
  • 1
  • 4
  • 14
-2
votes
2 answers

How to make an extension function that prints all datatypes in kotlin?

I want to make an extension function(named (printDatatype) for example) that can be applied on all datatypes and just prints it ... for example : "example".printDatatype() ==>output: example 56.prinDatatype()==>output: 56 …
-2
votes
1 answer

How to write in-place extension-function

fun String.reversed():String{ // return another String..no prob } fun String.reverse(){ //in-place change this } How to write the String.reverse()?
1 2 3 4 5 6
7