Can you make the `viewModel<…>()` compile time saf...
# compose
u
Can you make the
viewModel<…>()
compile time safe? I.e. if I accidentally dont plug my view model into DI? Currently it fails at runtime, that no such viewmodel/ factory was sound in the multibound map
w
I think this discussion should belong to your DI library.
☝🏻 1
u
Well it's a compose integration function,.so it belongs here fine
m
I believe Metro and Koin should be able to do it by compiler plugins, you can ask it in #C67HDJZ2N they are working on their compiler plugin.
u
I dont think so, since its a multibinding map underneat from what I can tell, and map access can always return null unless im missing something - which is my question
m
Yes you are right, but there is a way with compiler plugins, so they can check the code during compilation and look for
koinViewModel
and see the type parameter passed to it for example
koinViewModel<MyViewModel>
and then check if the class MyViewModel is defined and has the correct annotation and is part of the DI graph otherwise fail the compilation.
u
so linting like detekt, not metro/koin, right?
m
It's more powerful than lint, compiler plugins can add new behavior to the compiler, so they can add/remove/modify the code during compilation and verify your code and fail the compiler. I don't think lint can fail the compilation (except if you do some special work with gradle to put the task in between of AGP tasks). and I honestly don't know much about how lint works and how powerful they are.
u
which compiler plugins are you talkong about
m
I'm talking about Metro and Koin compiler plugin, Koin compiler plugin is still in progress: https://github.com/InsertKoinIO/koin/issues/2333 I mean they can technically make the
koinViewModel
compile time safe with their new compiler plugin but I haven't tried it yet.
u
are you sure? i dont thiji these compiler know about the xViewModel() callsites, theyte not annotated
m
Technically they can do that but I don't know if they did it or not. I also asked this question in koin, https://kotlinlang.slack.com/archives/C67HDJZ2N/p1766054364122199?thread_ts=1765988765.166449&amp;cid=C67HDJZ2N and Arnaud answered "Yes".
u
but that is very different, thats asking for concrete type, viewmodels are multibound (with dagger), not registered by their concrete type afaik