Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default value in constructor DSL #1853

Open
wiryadev opened this issue Apr 13, 2024 · 0 comments
Open

Support default value in constructor DSL #1853

wiryadev opened this issue Apr 13, 2024 · 0 comments

Comments

@wiryadev
Copy link
Contributor

wiryadev commented Apr 13, 2024

Is your feature request related to a problem? Please describe.
For Android Viewmodel, i expose nullable coroutine scope in the constructor so that i can pass my own dispatcher for testing and use default implementation on the real app. So the class will look like this

class LoginViewModel(
    private val loginUseCase: LoginUseCase,
    coroutineScope: CoroutineScope? = null,
) : ViewModel()

And then i can use it like this:

stateIn(
    scope = coroutineScope ?: viewModelScope,
    started = SharingStarted.WhileSubscribed(5000),
    initialValue = false,
)

Unfortunately, if we use constructor DSL, either

  1. it will crash due to missing CoroutineScope definition, or
  2. it will retrieve whatever coroutinescope that is declared that is not meant to be for that class.

Either way, we have to go back to Koin DSL:

viewModel { LoginViewModel(get()) }

It's not a problem when the class only needs few deps, but could become bloated by get() if there are many deps.

Describe the solution you'd like
Add support in constructor DSL to use default value provided in the class declaration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant