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
2
votes
2 answers
Testing Kotlin extensions inside a class
I know it's possible to test global extensions, but how to test Kotlin extensions inside a class?
class D
class C {
fun D.foo() {
println("123")
}
}
Should I interpret D.foo as private of D or can it be unit tested?

Luís Soares
- 5,726
- 4
- 39
- 66
2
votes
3 answers
Create EnumMap from List of values in Kotlin
I would like to define an extension of the enum class which accepts a list of values in the same order as the enum constants and outputs an EnumMap.
What I've been able to do instead is create and extension of the List object with an array of…

J.E.Tkaczyk
- 557
- 1
- 8
- 19
2
votes
1 answer
Unresolved kotlin android extensions
I've noticed that after implementation 'com.google.android.material:material:1.0.0-alpha3' to my build.gradle kotlin android extensions stop working.
Imports like this import kotlinx.android.synthetic.main..* became unused and all…

Dmitriy
- 135
- 1
- 8
2
votes
1 answer
Android Kotlin Kotlinx Module Level View Binding
I am trying to bind views from another module layout with kotlin extention. There is no problem if the layout in the same module that I'm working currently. But if I use a layout from another module and what if I want to bind that layout with…

DiRiNoiD
- 1,281
- 2
- 18
- 24
2
votes
2 answers
How to properly use the URL with Kotlin Android
I want to use
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val json = URL("https://my-api-url.com/something").readText()
…

Rod
- 712
- 2
- 12
- 36
2
votes
1 answer
Required
class TaskRepo(taskData: TaskData) {
companion object {
private val repoByTask: LRUMap = LRUMap(2, 10);
fun getInstance(taskData: TaskData): OrderFormRepo {
if (notFoundObject(taskData.taskId)) {
…

Anuj Jindal
- 1,711
- 15
- 24
2
votes
2 answers
How do I extend Kotlin Number class or use generics to create a simple property getter which will operate on all Number subclasses?
I am trying to learn more about Kotlin abstract class extensions and generics by building very simple methods and property getters that extend built-in classes. I've mostly been successful, but I'm stumped by the Number class. My test property…

sirksel
- 747
- 6
- 19
2
votes
2 answers
How to place a Kotlin extension in a class file?
Maybe this question is answered in the official doc, but I'm not seeing it...
In Swift we are used to write something like
class Jazz {
...
}
extension Jazz {
func swing() {
...
}
}
and place that whole code snippet in a single file,…

Algar
- 5,734
- 3
- 34
- 51
2
votes
1 answer
Not able to add library into my application?
I am trying to add the library into the Kotlin but it is not getting proper install in it.
Following error I am getting while importing the library, please check it
Error: Could not find com.android.tools.build:gradle:2.3.1.
Searched in the…

Ravindra Kushwaha
- 7,846
- 14
- 53
- 103
2
votes
1 answer
Unexpected tokens (use ; to seperate expressions on the same line)
I think I messed up when I tried to write one line code in Kotlin, It seems like there is no problem but IntelliJ gives me this error here:
val cards : Array = Array(52 { i -> Card(i % 13, getSuit(i))})

Ege Kuzubasioglu
- 5,991
- 12
- 49
- 85
2
votes
1 answer
Generics for RecyclerView.Adapter Android
I am trying to use a common extension function for all RecyclerView.Adapter in my app so I created an ext fun called notifyAdapter, But it is not working as expected.
fun RecyclerView.Adapter.notifyAdapter() {
…

Sai
- 15,188
- 20
- 81
- 121
2
votes
1 answer
findViewById ClassCastExcpetion
I'm using the kotlin-extension plugin for finding the views.
But now I get an exception by
setSupportActionBar(mainActivity_toolbar)
It worked until now, but now it gives an
ClassCastException: android.widget.FrameLayout cannot be cast to…

lampenlampen
- 947
- 6
- 21
2
votes
2 answers
Not nullable Mutable Map
Java : 1.8.0_102
Kotlin: 1.0.4
I'm trying to create a map where you can do something like map["key"] += 5 similar to javascript.
Kotlin already has withDefault that solves one part of this, but map's get function still returns a nullable value, so…

martin treurnicht
- 1,223
- 1
- 10
- 17
2
votes
1 answer
Kotlin build problems with Android
Integrated Kotlin into a somewhat large project that uses multidex.
So when I try to build, I get this error:
:incrementalDesygnerDebugJavaCompilationSafeguard UP-TO-DATE
:compileDesygnerDebugKotlin
WARN: Failed to initialize native filesystem for…

Alexandre G
- 1,655
- 20
- 30
2
votes
1 answer
Can I create a Kotlin extension method for adding an rxJava Subscription to a CompositeSubscription?
I've been playing with Kotlin/RxJava and tried to create an extension method for adding a Subscription to a CompositeSubscription, that would work like:
search.subscribe {
//do stuff
}.addToComposite(compositeSubscription)
This is my attempt…

grepx
- 443
- 5
- 7