Scope implements AutoCloseable#2363
Open
lidonis wants to merge 1 commit into
Open
Conversation
Enables Kotlin's `use { }` pattern on Scope, guaranteeing cleanup
even on exceptions. This is a source-compatible and binary-compatible
change close() already existed with the right signature.
4 tasks
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
Scopenow implementsAutoCloseable, enabling Kotlin'suse { }pattern for safe scope lifecycle managementclose()already existed with the right signature, so the only change is adding the interface declaration andoverridemodifierMotivation
Koin's
Scopehas aclose()method but doesn't implementAutoCloseable. This means developers can't use Kotlin'suse { }pattern and must rely on manualtry/finallyblocks, which is error-prone.With this change:
This is the same pattern Kotlin developers use for
InputStream,Connection,ResultSet, etc. Scopes hold resources and have a lifecycle, so they belong in this family.Changes
Scope.kt: AddedAutoCloseableinterface andoverrideonclose()ScopeAPITest.kt: Two new tests verifyinguse { }works and closes on exceptionsscopes.md: Updated documentation withuse { }as the recommended approach for short-lived scopesCompatibility
close()already exists with identical semanticsAutoCloseableis available inkotlin.stdlibfor all targets since Kotlin 1.8Test plan
scope implements AutoCloseable and works with useverifiesuse { }resolves instances and auto-closesscope use closes on exceptionverifies close is called even when an exception is thrownScopeAPITesttests pass (13/13)