Hi folks, i browsed kotlinx.coroutines lib and i h...
# coroutines
g
Hi folks, i browsed kotlinx.coroutines lib and i have a question about package structure: all flow operators even though they are in separate file their package is still
package kotlinx.coroutines.flow
. I guess this is for easier discover-ability?. I am wondering if i can use also this pattern when i seem fit in my application code or it is not recommended at all.? Thanks in advance for any help and answers ! Also how come the intelij warning is not suppressed (@file:Suppress("PackageDirectoryMismatch"))? Does not that lead to confusion if the package is intentional or not ?
s
KotlinX does more than just place them in the same package. Kotlin has great support for splitting files, but merging them together when compiling. IIRC almost all Flow files turn into a single Class file in the JVM. This happens by using
@file:JvmMultifileClass
and an explicit name
@file:JvmName("FlowKt")
. https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/common/src/flow/Builders.kt#L6
This is really awesome for organising code, whilst still keeping the same API as if it would all be in a single file.
g
My use-case: i have a
service
package and i use some extensions in all my services. for example a ResponseHandler. I think it would be better to be placed in another package inside
service
but would like to keep the ability to discover it without importing.
s
Ah, that is a slightly different use-case. A hack for this could be to put a
predef.kt
file without a package name. IIRC, it doesn't need importing in that case.
g
you mean something like this:
Copy code
//package org.msensis.imahub.workhorse.service.converters

import imahub.sdk.hub.HubPayload

interface HubPayloadConverter<out T> {
    fun convert(): HubPayload<T>
}
?
s
Yes, just quickly checked in my local project and that doesn't require any imports.
g
when i use it in package
service
intelij promts me to import it like this:
import HubPayloadConverter
s
Oh sorry, indeed you're right. IDEA automatically added
import example
without me noticing. So you're only option would be to put it in the
service
package, so it doesn't require imports in the service package.
g
yup, that's why i was wondering if it's ok to use kotlinx's lib pattern package, though maybe i am over-complicating thigns and should just not care so much for one import?
s
In most cases explicit import are desired, given that IDEA has such great support for auto-suggestions and auto-importing it's probably better to not overcomplicate things.