https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
d

Derek Peirce

10/12/2023, 7:02 AM
I'd like some way to inform my Kotlin library that a Java class provided from a library can be treated as if it has
in
or
out
generics. For example, in RxJava, we have
Single<T>
, which in Kotlin would have been represented as
Single<out T>
. Ideally, there'd be some directive like
import io.reactivex.rxjava3.core.Single as Single<out T>
, which could be conveniently distributed through an entire project, perhaps by each file in the project importing from some file containing directives like this. Would something like this be reasonable to add, or would the complications or the risk of someone importing a class that doesn't actually conform to
out
be too great?
c

CLOVIS

10/12/2023, 7:59 AM
I think it would be much too dangerous to have this as an import. Both because we usually don't read them, and because presumably you'd want this behavior in multiple files, so you'd have to copy it around.
y

Youssef Shoaib [MOD]

10/12/2023, 2:13 PM
I think a typealias should do the trick here, no?
Copy code
typealias Single<T> = io.reactivex.rxjava3.core.Single<out T>
👍 1
d

Derek Peirce

10/13/2023, 3:01 AM
The
typealias
seems to work, so it would be a matter of having a way to configure the IDE to automatically import
Single
without
Copy code
import io.reactivex.rxjava3.core.Single
but instead
Copy code
typealias Single<T> = io.reactivex.rxjava3.core.Single<out T>
so that it's the standard for the codebase. Would that be a reasonable feature for IntelliJ/Android Studio? I could run a massive find-replace, but then the system would decay over time as engineers still count on auto-imports, and in particular would cause conflicts in Dagger if not every file is on the same page about what
Single
should mean.
c

CLOVIS

10/13/2023, 7:59 AM
You don't have to copy the typealias each time, you can just write it once and import it everywhere. You just have to teach your engineers to import your own Single, but at this point maybe it's simpler to call it differently to avoid accidental imports to the old one.
👍 1