https://kotlinlang.org logo
#koin
Title
c

Christopher Porto

10/10/2022, 5:30 AM
Possible to use
@Single
annotation on a function? The annotation has function as a target, but when I try creating a top level function to provide singleton, generated code doesnt include it
Looking at the scanning code for the ksp compiler plugin, seems it only scans for classes. https://github.com/InsertKoinIO/koin-annotations/blob/main/compiler/koin-ksp-compi[…]ler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinScanner.kt Annotation is bit misleading?
a

arnaud.giuliani

10/10/2022, 5:08 PM
it scan also function inside Module classes
just @Single a function that returns a value and you’re done
c

Christopher Porto

10/10/2022, 5:37 PM
Beautiful thank you
m

Mantas Varnagiris

01/30/2023, 8:35 PM
Is it possible to somehow add everything to default module? If I have multiple packages/project-modules in which I need to create Retrofit services, for each I would need to create new @Module class and then don't forget to add it to
startKoin
. If @Single @Factory and other annotations would work on any top level functions then I don't need to write anything extra. Am I missing something?
c

Christopher Porto

01/30/2023, 9:38 PM
@Mantas Varnagiris From my initial understanding when making this post I also thought
@Single
and
@Factory
annotations worked on top level functions but they didn't (Not sure if that's still the case but most likely not). The annotated functions were only picked up by the processor when inside a class annotated as
@Module
. Also as far as I know it's not possible to have components generate in module A from a dependent project module B. Ksp scanning seems to happen per project module basis and if no koin module is defined it creates one for you. Think your best bet is creating module classes for your project modules and then including them in your main
@Module
annotation or in
startKoin
Of course @arnaud.giuliani probably has more insight on this 😛
m

Mantas Varnagiris

01/30/2023, 9:52 PM
Thanks for a reply on an old thread 😄 Yeah with the current way it works that's the only way I see, which is a shame. I would have really liked to avoid writing modules at all.
a

arnaud.giuliani

01/31/2023, 7:55 AM
The annotated functions were only picked up by the processor when inside a class annotated as
@Module
.
yes, improvement are coming for this 👍
Ksp scanning seems to happen per project module basis and if no koin module is defined it creates one for you. Think your best bet is creating module classes for your project modules and then including them in your main
@Module
annotation or in
startKoin
Exact 👌 For now, KSP Annotation scanning is done on the current Gradle module
m

Mantas Varnagiris

01/31/2023, 9:15 AM
Amazing good to hear. Thanks 🙏
16 Views