Nested Scopes
See original GitHub issueIs your feature request related to a problem? Please describe. Currently, with Koin 2.0 it’s possible to declare nested scopes:
module {
scope(A) {
scoped(NUMBER) { 123 }
// nested scope!
scope(B) {
scoped(STRING) { get(NUMBER).toString() }
}
}
}
With implied understanding that:
- An instance of scope B can be created from an instance of scope A (but not from the global Koin),
- Scope B “sees” all bindings from scope A.
- Closing an instance of scope A would close all instances of B, which were created from it (i.e. child scopes)
However, that’s not what’s happening. In Koin 2.0, the above DSL is equivalent to:
module {
scope(A) {
scoped(NUMBER) { 123 }
}
// actually, it's not nested :(
scope(B) {
scoped(STRING) { get(NUMBER).toString() }
}
}
… and at runtime, the get(NUMBER)
will throw a NoBeanDefFoundException
.
Koin should do better than that.
Describe the solution you’d like Nested scoped should be supported. For that, two things are necessary:
ScopeSet
needs to definefun scope
similar toModule
.Scope
needs to definefun createScope
similar toKoin
- for the nested scopes.
Describe alternatives you’ve considered An alternative to nested scopes is doing extra work (like duplicating same bindings across multiple scopes - but that will break singletons)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:6 (2 by maintainers)
Top Results From Across the Web
What is a "nested scope" ? | Verification Academy
In this context, a nested scope is any region of code inside a compilation unit that creates a new scope. A module declaration...
Read more >Nested Scopes - C2 wiki
In a progamming language with NestedScopes, source code has a nested structure such that variable definitions can be introduced at any level of...
Read more >Nested scope - IBM
A nested commit scope is a commit scope that is activated after the root scope has been activated. A root commit scope may...
Read more >Class 14: Nested Scopes and Declaration Order
In C++, nested scopes are made using curly braces (1 and l). The scope resolution operator :: allows jumping between scopes manually. In...
Read more >Let's Build A Simple Interpreter. Part 14: Nested Scopes and a ...
In a nested scope you can re-declare a variable with the same name as in the outer scope, thus effectively hiding the outer...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The approach I settled on is:
where
scopeA
is an extension property ofScope
doing something likeGlobalContext.get().koin.getScope("A")
- assuming, there is an instance of scope A with name “A”.Says complete in 2.1 but still not working. You can do
Works but not ideal