Questions tagged [kotlin]

26 questions
37
votes
3 answers

Why is there no static keyword in Kotlin?

Kotlin is known primarily as a drop-in replacement for Java, but it gets rid of a well-known Java construct: the static keyword. Instead, that class-level functionality is offered mainly by companion objects. What is wrong with static methods and…
user1446
  • 492
18
votes
7 answers

Should one prefer a generic version of a function, even if it's not re-used (yet)?

YAGNI might tell us, that in the below implementation the generic version is not needed, as long as the function is only used once. But to me personally, it seems, the generic version is more readable because I'm not distracted by all the…
Tobias Hermann
  • 608
  • 5
  • 15
15
votes
4 answers

When to write extension methods for your own classes?

I recently saw a code base which had a data class Address defined somewhere and then in a different place: fun Address.toAnschrift() = let { address -> Anschrift().apply { // mapping code here... } } I found it confusing to not have…
6
votes
2 answers

Why Kotlin doesn't allow assignments as expressions?

Coming from Java, I was surprised to find out that Kotlin doesn't allow assignments as expressions. Is there a reason for that? Java (Works) @Test public void test_x() { List elements = null; for (final String x :…
sero
  • 169
4
votes
1 answer

Do kotlin libraries with inline APIs encourage high coupling and discourage unit testing?

As an example, let's assume our application needs some way to communicate with other systems using HTTP. interface HttpClient { fun get(url: String, returnType: Class): T fun post(url: String, body: Any) } Now it seems like good practice…
enp4yne
  • 149
3
votes
4 answers

Is there ever a reason in Kotlin to use threads over coroutines?

I have a good understanding of when coroutines in Kotlin are superior to threads. I know that coroutines are lightweight and are great for massive concurrency when, in contrast, spinning up lots of threads is costly in compute resources, memory…
J-bob
  • 299
2
votes
1 answer

Is returning Result types the standard way of dealing with errors in Kotlin?

Given that there are no checked exceptions in Kotlin, are Result types the correct way to indicate an exception occurred to the caller? For example, I have the following function in my code: suspend fun addUserToSignInHistory(user: User):…
Adam
  • 272
2
votes
1 answer

Android Jetpack DataStore item separation

I am trying to migrate to Jetpack DataStore from good ol' SharedPreferences and there is one thing I am struggling to come to terms with and that is the amount/size of the data pulling out of DataStore at once. SharedPreferences were asynchronous…
2
votes
1 answer

What is the reasoning behind Kotlin using non-nullable types for Java interop methods?

Considering Kotlin Java Interop: Null Safety and Platform Types Why is code like this legal in Kotlin? fun envString(key: EnvVars): String { return System.getenv(key.toString()) } getenv() can return null, as indicated by its JavaDoc: Returns:…
F.P
  • 609
1
vote
1 answer

How to test functionality that requires a certain internal state?

I'm struggling to test functionality in a class where the class has to be in a certain state for the functionality to work, but the class cannot be put directly into a given state by design, to maintain state integrity. I'm developing the back end…
gotube
  • 127
1
vote
2 answers

Is using KDoc/Javadoc comments inside of a function considered bad practice?

I often find it helpful to use KDoc/Javadoc comments inside of a class or function instead of normal comments. IntelliJ colorizes them more obviously by default and, more importantly, allows hyperlinking to definitions. I'm wondering if it is…
1
vote
0 answers

Common patterns for Observable data layer on iOS

I am working on learning native iOS development in Swift, and I am trying to find something that is similar to what I've learned in Android development with Kotlin. In particular I am referring to the concept of having the db as the "single source…
papafe
  • 171
1
vote
1 answer

Kotlin delegation, what should I test?

In Kotlin the powerful construct of delegation can be used to extend functionality of existing interfaces by reusing existing implementations. class Demo : Map by HashMap {} Questions: What should I be testing? Testing hashmap from the example is…
0
votes
0 answers

Trying to pass a funtionality to a composable , what is a better design?

So i have a bio-metric prompt implementation as below class BiometricAuthentication( private val activity: AppCompatActivity ) { private val resultChannel = Channel() val promptResults = resultChannel.receiveAsFlow() …
0
votes
1 answer

Separation of concerns: persisting complex types

I have the following module structure: core persistence extension In my core module, I have an interface: Handler. This interface has multiple implementations in the core module as well as extension module. Some implementations depend on other…
1
2