Hi folks, i browsed kotlinx.coroutines lib and i h...
# coroutines
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 ?
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
and an explicit name
. 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.
My use-case: i have a
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
but would like to keep the ability to discover it without importing.
Ah, that is a slightly different use-case. A hack for this could be to put a
file without a package name. IIRC, it doesn't need importing in that case.
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>
Yes, just quickly checked in my local project and that doesn't require any imports.
when i use it in package
intelij promts me to import it like this:
import HubPayloadConverter
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
package, so it doesn't require imports in the service package.
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?
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.