Koin and KoinApplication implement AutoCloseable#2374
Open
lidonis wants to merge 1 commit intoInsertKoinIO:4.2.0from
Open
Koin and KoinApplication implement AutoCloseable#2374lidonis wants to merge 1 commit intoInsertKoinIO:4.2.0from
lidonis wants to merge 1 commit intoInsertKoinIO:4.2.0from
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
KoinandKoinApplicationnow implementAutoCloseable, enabling Kotlin'suse { }pattern for safe resource cleanupclose()already existed with the right signature on both classes, so the only change is adding the interface declaration andoverridemodifierMotivation
Koin's
KoinandKoinApplicationhaveclose()methods but don't implementAutoCloseable. This means developers can't use Kotlin'suse { }pattern and must rely on manualtry/finallyblocks, which is error-prone.With this change:
koinApplication { modules(myModule) }.use { app -> val service: MyService = app.koin.get() // work with service } // app.close() called automatically, even on exceptionsThis is the same pattern Kotlin developers use for
InputStream,Connection,ResultSet, etc. Koin instances hold resources and have a lifecycle, so they belong in this family.This is a companion to #2363, which added
AutoCloseabletoScope.Changes
Koin.kt: AddedAutoCloseableinterface andoverrideonclose()KoinApplication.kt: AddedAutoCloseableinterface andoverrideonclose()KoinAutoCloseableTest.kt: Four tests verifyinguse { }works and closes on exceptions for both classesstart-koin.md/starting-koin.md: Updated documentation withuse { }examplesCompatibility
close()already exists with identical semanticsAutoCloseableis available inkotlin.stdlibfor all targets since Kotlin 1.8Test plan
Koin implements AutoCloseable and works with useverifiesuse { }resolves instances and auto-closesKoin use closes on exceptionverifies close is called even when an exception is thrownKoinApplication implements AutoCloseable and works with useverifiesuse { }resolves instances and auto-closesKoinApplication use closes on exceptionverifies close is called even when an exception is thrown