https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
s

Seri

07/19/2018, 8:53 PM
Is anyone using Kotlin Objects in their Android code? I’m curious what common use cases it has.
a

alex.krupa

07/19/2018, 9:04 PM
I use them for parameterless sealed class subtypes.
m

maxmello

07/19/2018, 9:05 PM
When starting with Kotlin I used Objects all over the place and was excited, until I realized the pain attached to testing them, because you cannot do constructor based dependency injection (don’t know about dagger, I haven`t used it yet) with Objects (also mocking Objects is a pain too). So I first created init() Methods in the Objects until I decided to just make them normal classes and create a lazy val or lateinit var instance in my Application class. For pure functional code, you can just use top level functions so the actual use cases for Objects are limited, in my opinion.
5
n

nounours

07/20/2018, 8:05 AM
I use objects for ViewStates principally
m

Max Russek

07/21/2018, 8:31 PM
@maxmello We realized that too, but we unfortunately couldn’t get around a singleton object that was used heavily in an old Java code base. We came up with a neat solution, which was to write the functionality as a class, and then have a singleton object provide the functionality through delegation. It ended up looking like this
Copy code
class CleanTestedClass : CleanInterface { ... }
object NastySingleton : CleanInterface by CleanTestClass()
which I thought was cool
👍 2
s

scottmeschke

07/27/2018, 7:52 PM
I use them for stateless functional namespaces.
Where extension functions aren't appropriate
4 Views