question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is 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:

  1. An instance of scope B can be created from an instance of scope A (but not from the global Koin),
  2. Scope B “sees” all bindings from scope A.
  3. 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:

  1. ScopeSet needs to define fun scope similar to Module.
  2. Scope needs to define fun createScope similar to Koin - 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
valeriyocommented, Jun 28, 2019

The approach I settled on is:

module {
    scope(A) {
        scoped(NUMBER) { 123 }
    }

    scope(B) {
        scoped(STRING) { scopeA.get<Int>(NUMBER).toString() }
    }
}

where scopeA is an extension property of Scope doing something like GlobalContext.get().koin.getScope("A") - assuming, there is an instance of scope A with name “A”.

0reactions
nschwermanncommented, Apr 17, 2020

Says complete in 2.1 but still not working. You can do

scope<A>{
    scoped<B>{}
    scope<C>{
           scoped<D>{
                 linkTo(A.scope) //Must be inside scope block, unfortunate if multiple scoped items in scope C depending on B
                 D(get()) //get() B from scope A
            }
     }
}

Works but not ideal

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found