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
1
vote
1 answer
How to share image in Android using Kotlin?
I want to share an image located in assets folder using 'Kotlin'.How can I achieve that Similar block of code in android :
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM,…

PKumar
- 516
- 9
- 20
1
vote
4 answers
Why do nullable numbers not preserve identity?
In kotlin nullable values are not preserving identity but equality:
val a: Int = 10000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA === anotherBoxedA) // !!!Prints 'false'!!!
print(boxedA == anotherBoxedA) // Prints 'true'
Why is…

Zumry Mohamed
- 9,318
- 5
- 46
- 51
1
vote
1 answer
Kotlin generics expecting unexpected type
I've defined the following base class with two generic types and using it two levels deep (for lack of a better phrase). Here's my use case.
abstract class BasePresenter {
var model: M? = null
var view: WeakReference? =…

Rafa
- 3,219
- 4
- 38
- 70
1
vote
1 answer
How do Kotlin's extension functions work?
Let's say I want an integer that supplies a square method.
Kotlin:
fun Int.square() = this * this
usage:
println("${20.square()}")
doc:
Extensions do not actually modify classes they extend. By defining an extension, you do not insert new members…

User1291
- 7,664
- 8
- 51
- 108
1
vote
2 answers
Converting my existing android studio project to kotlin?
Error:Could not find com.android.tools.build:gradle:3.0.0-alpha2
Searched in the following…

Paramjit Singh Rana
- 814
- 8
- 29
1
vote
0 answers
Issue with extension method in Kotlin script
I'm trying Kotlin script for gradle configuring. Line from build.gradle.kts:
val deployTest = System.getProperty("deployTest").toBoolean()
Running:
gradle clean build
It works well on my local workstation but fails on…

awfun
- 2,316
- 4
- 31
- 52
1
vote
1 answer
Extension function on enumerator, `values()` unavailable?
If I try to type:
enum class EGraphicsAPIConvention(@get:JvmName("i") val i: Int) {
API_DirectX(0),
API_OpenGL(1)
}
fun EGraphicsAPIConvention.of(i: Int) = values().first { it.i == i }
The compiler complains:
unresolved…

elect
- 6,765
- 10
- 53
- 119
1
vote
0 answers
Any examples of how to 'enable' Kotlin extension functions in Maven dependencies?
I'm getting started with Kotlin and now trying to use the Kotlin-JDBC sample (https://github.com/kotlin-projects/kotlin-jdbc) and have added the following dependency to my Kotlin/Maven project:
…

Jarym
- 2,046
- 1
- 15
- 13
1
vote
1 answer
How do I create an extension function in Kotlin, which operates on a class?
Imagine, I have this line of code:
import org.mockito.Mockito
val mock = Mockito.mock(Sim2ParametersProvider::class.java)
I want to be able to write it like this:
val mock = Sim2ParametersProvider::class.mock()
How do I do this?
I tried
fun

Glory to Russia
- 17,289
- 56
- 182
- 325
1
vote
2 answers
Kotlin extension properties not recognized in V1.0.1
Revisiting some code from a pre-1.0.1 version of Kotlin, it appears that my extension property no longer works. In fact, it still does but I can no longer override it.
Consider this class:
class TestClass {
override val loggerName: String
…

Limnic
- 1,826
- 1
- 20
- 45
1
vote
1 answer
Kotlin: How to run service methods in the context of a transaction class?
I'd like to define database calls in service methods, but have them executed in the context of a Transaction class without opening the connection in the service itself so that I can include multiple service calls in the same transaction.
I'm looking…

geg
- 4,399
- 4
- 34
- 35
1
vote
3 answers
Extend Mockito verify for Kotlin not working (in a "kotlin way")
I want to extend verify to allow checking multiple commands over the same mocked object but it is not working, it compiles but on run it dont run each command over the same.
Just want to avoid writing more things…

Daniel Gomez Rico
- 15,026
- 20
- 92
- 162
0
votes
0 answers
What is the puripose of `additional receiver type` in Kotlin lambda declaration?
I am a java developer but develop Kotlin for two weeks and during test implementation I've found interesting construction :
every {
mockedObject.foo(arg)
} returns bar
I've checked every finction declaration:
/**
* Starts a block of stubbing.…

gstackoverflow
- 36,709
- 117
- 359
- 710
0
votes
1 answer
What is naming convention for a file with Kotlin extensions?
I have generated class MyDto and I want to add couple of new functions
fun MyDto.convert(foo: String): Bar {...
I have no choice - I have to put it to different file but in contrast to java it won't be a class but just a Kotlin file.
How should I…

gstackoverflow
- 36,709
- 117
- 359
- 710
0
votes
1 answer
How to close input or output stream when using Kotlin File's extensions
Do we need to close input or output stream when we use Kotlin's File.readText or File.writeText extensions
file.readText()
file.writeText("something")

Hamza Khan
- 1,433
- 13
- 19