106 questions
Best practices
0
votes
2
replies
46
views
How to create a type safe functional extension in Kotlin with generics
I am expecting that only the first line of main function will be compiled.
fun<T> T.shouldBe(arg:T) : Boolean {
return this == arg
}
fun main() {
1.shouldBe(2)
1.shouldBe(true)
...
1
vote
2
answers
56
views
Difference between function type with receiver and extension function
Consider the following functions:
fun Int.ext() {
// TODO
}
fun doSomething(f: Int.() -> Unit) {
// TODO
}
The functional parameter f has the same type as the extension function ext. So, ...
0
votes
0
answers
28
views
Kotlin extension function vs regular method function [duplicate]
While doing some refactoring I came across the following scenario where I had a method with a single parameter like this:
private fun doSomething(thing: Thing) {}
and I thought as this method just ...
0
votes
1
answer
71
views
Compile-time check for lambda type in Kotlin
I'm trying to create an extension function I can apply to collections similar to filter/map/etc. Typing due to generics is pretty loose and I want to be more strict. Is there anything I can do in ...
2
votes
0
answers
48
views
Can I make IDEA show a hint to use a Kotlin extension function instead of a Java original?
Consider this example of a fictive old Java library, which I can't change but want to adapt for my Kotlin projects.
public final class SomeOldJavaClass {
private SomeOldJavaClass() { }
public ...
0
votes
1
answer
160
views
After upgrading from SaxonCS from 11 to 12 ExtensionFunction are no longer working
I have tried upgrading from SaxonCS 11 to 12 after my ExtensionFunctions are no longer called I get the following exception when I try to compile my xslt.
Saxon.Hej.trans.XPathException: 'Cannot find ...
1
vote
1
answer
1k
views
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<Project> {
...
0
votes
3
answers
1k
views
How can I create an extension function on System class?
I am trying to create an extension function on java.lang.System to refactor currentTimeMillis usage to the following System.currentTimeinSeconds() extension method that returns a String. (This is done ...
0
votes
1
answer
1k
views
how to stub kotlin map function | unittest
i want to stub kotlin's map extension function in my unit test. couldn't find any proper solution on internet to do that.
ctually i have put empty blocks inside curly brackets but it seems like this ...
0
votes
1
answer
248
views
flatMapConcat for convert
I have a flow that return my event type generic like
Flow<BaseResourceEvent<T>>
But sometimes I have to convert T type to another type. For example User type to DashBoardUser with an ...
0
votes
0
answers
28
views
What happens when I override a built-in operator, e.g., to, in Kotlin? [duplicate]
Kotlin defines an infix operator to for building Pairs, which is basically a shorthand for calling the constructor of Pair. In other words
val pair = "verbose" to true
is the same as
val ...
0
votes
1
answer
193
views
I make extension function in android using kotlin and method from the data type, but my extension function not working properly?
I want to make extension function using TextInputEditText and call addTextChangedListener, but when i run the app and using the feature, my app crash. My goal is to create an extension function ...
5
votes
2
answers
7k
views
Kotlin : function extension inside companion object
In Kotlin language, what does this syntax do and how does it work ?
class ClassName1 {
companion object {
fun ClassName2.funName()=""
}
}
1
vote
1
answer
413
views
Kotlin collection operations effectiveness
I would like to create an extension function for a collection to check if one collection contains any item of defined set.
I think about two implementations:
infix fun <T> Iterable<T>....
20
votes
4
answers
14k
views
How to create an extension function with multiple receivers in Kotlin?
I want my extension function to have a couple of receivers. For example, I want function handle to be able to call methods of both CoroutineScope and Iterable instances:
fun handle() {
// I want ...
3
votes
0
answers
1k
views
Apply extension function to several types at once in Kotlin
Is it possible to apply an extensions function to several types at once?
Something link this
fun <Double || Float>.toString(): String
4
votes
1
answer
2k
views
How can I use annotation @IntRange for Kotlin extension function for Integer
below is my Kotlin extension function :
infix fun Int.send(data: String) {
MsgSendUtils.sendStringMsg(
this,
data
)
}
called like this :
8401 send "hi"
and my ...
1
vote
1
answer
882
views
Mock extensions function inside Companion Object
I have a class (class A) to which I define an extension function (A.extension()) inside a companion object of another class (class B) for a matter of organization.
On my tests I need:
To use a real ...
1
vote
1
answer
524
views
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)...
5
votes
2
answers
1k
views
Providing only one type parameter to an extension function with multiple type parameters in Kotlin
Introduction
In Kotlin I have a generic conversion extension function that simplifies conversion of this object of type C to an object of another type T (declared as the receiver) with additional ...
0
votes
1
answer
146
views
type mismatch when create a kotlin infix notation to nest a function to another
I am trying to create a infix notation as a extension function of (Int) -> Int function which is used to nest a function to another.
For example:
class Entry {
companion object {
...
0
votes
1
answer
947
views
vararg and extension function in kotlin
I am trying to write extension function in kotlin. I came almost to the end but one simple thing stopped me. Code goes like this:
fun Bundle.applyWithUserParameters(vararg functionList: () -> Unit =...
0
votes
0
answers
391
views
Unit testing list extension function - Kotlin
I am new to unit testing, and facing issue with a extension function, which is kind of:
fun List<Water>.getFileteredList() = return listOf(filteredWater)
Now if I want to test this function, ...
4
votes
2
answers
1k
views
Extension function from a generic interface
Consider the following interface
interface EntityConverter<in A, out B> {
fun A.convert(): B
fun List<A>.convert(): List<B> = this.map { it.convert() }
}
I want to use it ...
3
votes
3
answers
4k
views
Dependency injection inside extension function
Is there a way to inject an object inside an extension function or a global function using DI framework in Android Kotlin?
I use this function in many places. So I do not want to pass a parameter ...
0
votes
1
answer
307
views
Unable to understand extension function in Kotlin Android?
I am not able to understand extension function and use it in my project. Can anyone guide me here please?
0
votes
1
answer
329
views
How to use parameters inside a block when defining an extension function?
val test: Int.(String) -> Int = {
plus((this))
}
When defining this type of extension function, how can I use arguments( Here, the argument of type String) inside the block?
When ...
-1
votes
1
answer
429
views
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 ...
-2
votes
2
answers
266
views
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: ...
1
vote
0
answers
132
views
How to get Dagger2 class property in extension function? [duplicate]
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 ...
5
votes
2
answers
2k
views
Kotlin: Idiomatic usage of extension functions - putting extension functions next to the class it extends
I see some usages of Extension functions in Kotlin I don't personally think that makes sense, but it seems that there are some guidelines that "apparently" support it (a matter of ...
8
votes
3
answers
802
views
How to write a Kotlinesque extension function in Typescript?
I Kotlin if I have an interface like this:
interface User {
val name: String
val email: String
}
I can write an extension function like this anywhere in the code:
fun User.toUserDto(): ...
0
votes
3
answers
452
views
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 ...
0
votes
1
answer
110
views
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
...
2
votes
0
answers
2k
views
Access extention functions (values) through reflection in Kotlin
I would like to access Kotlin's extension functions/values through reflection.
I have a very simple example, but I cannot figure out what is wrong here
import kotlin.reflect.full.*
class A
fun A.ext(...
0
votes
1
answer
868
views
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: ...
0
votes
1
answer
509
views
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(...
8
votes
2
answers
4k
views
How does a extension method work in Kotlin?
In Java you cannot extend a final class, but in Kotlin you can write extension method to such final classes. How did they make it work internally? I couldn't find any article that explained the inner ...
0
votes
1
answer
627
views
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 {
...
2
votes
0
answers
703
views
Can't import extension function in Android App Kotlin
Running into an Issue early on in importing a declared extension function in another Kotlin File (Extensions.kt), call the extension function From another class (ForecastsRepository.kt) it doesn't ...
0
votes
1
answer
593
views
Kotlin: How can I determine the extension function exists
Suppose I have a function (in Kotlin over Java):
fun <E> myFun() = ...
where E is a general type I know nothing about. Can I determine within this function whether there exists an extension ...
3
votes
1
answer
966
views
Operator overloading in Kotlin [closed]
String division using operator overloading in Kotlin, please help me to complete this code. No change can be made in main function.
Find common from both string like a=aabcc b=abdec answer=abc (...
3
votes
1
answer
73
views
Last extension function is not called
I came across a strange bug and I can't figure out why it occurs. If I invoke my original function, roundToMidnight() is not called and the date is not rounded.
My original function, what doesn't ...
0
votes
1
answer
109
views
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<String>()
//...
0
votes
0
answers
61
views
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 ...
1
vote
1
answer
1k
views
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 ...
0
votes
1
answer
53
views
How to write my own by extension function
I have a default function like this:
fun <T> makeDefault(): Animal<T> = Animal<Nothing>(
name = "",
size = 0,
age = 0
)
I saw that there is the by operator, which ...
0
votes
1
answer
765
views
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 ...
0
votes
1
answer
291
views
How to restrict the generic extension function parameter type?
I try to use the list extension function binarySearch
public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>,
fromIndex: Int = 0, toIndex: Int = size): Int
...
0
votes
2
answers
314
views
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 ...