Harry
09/13/2020, 11:37 AMMarat Akhin
09/13/2020, 11:57 AMMarat Akhin
09/13/2020, 12:04 PMHarry
09/13/2020, 12:07 PMlouiscad
09/13/2020, 12:27 PMerror("…")
if the value is already initialized.Harry
09/13/2020, 12:32 PMlouiscad
09/13/2020, 12:52 PMlateinit val
could easily be more abused and cripple codebases.
I had a few times where I would have used a lateinit val
too, but many times where I had to think about it because I couldn't and I didn't want it to become a var
, I ended up with better solutions, including using lazy
and coroutines.Big Chungus
09/13/2020, 12:54 PMlouiscad
09/13/2020, 12:56 PMDelegates
nor in the stdlib AFAIK.Big Chungus
09/13/2020, 12:56 PMlouiscad
09/13/2020, 12:57 PMHarry
09/13/2020, 1:08 PMI think it's because Kotlin wants to discourage such use as lateinit val could easily be more abused and cripple codebases.
Could you let me know any example for that cases?louiscad
09/13/2020, 1:10 PMlateinit val
would still be like a var
in the sense that it makes the code harder to reason about as things can change from outside/under and initialization can become undeterministic when looking at the code only.Big Chungus
09/13/2020, 1:12 PMvar: Any?
Over lateinit var: Any
anytimeBig Chungus
09/13/2020, 1:13 PMHarry
09/13/2020, 1:21 PMlouiscad
09/13/2020, 1:28 PMActivity
or whatever). And thanks to that, I never need lateinit var
in my code.Nir
09/13/2020, 1:31 PMNir
09/13/2020, 1:31 PMNir
09/13/2020, 1:32 PMNir
09/13/2020, 1:32 PMHarry
09/13/2020, 1:32 PMHarry
09/13/2020, 1:34 PMlouiscad
09/13/2020, 1:34 PMActivity
/whatever class. But I don't see how it's an issue to create a new file unless you struggle to find a better name than `WhateverActivity`/`WhateverWhatever`louiscad
09/13/2020, 1:35 PMNir
09/13/2020, 1:35 PMNir
09/13/2020, 1:36 PMNir
09/13/2020, 1:36 PMHarry
09/13/2020, 1:37 PMlouiscad
09/13/2020, 1:39 PMlouiscad
09/13/2020, 1:40 PMHarry
09/13/2020, 1:45 PMedrd
09/13/2020, 2:24 PMlateinit
is problematic because it's stateful and the compiler cannot track the variable initialization. It's only necessary for dependency injection where constructor injection is not supported. All other cases could be solved with constructor injection or, as Martynas said, using var
with a nullable type. This allows the compiler to identify all uninitialized cases.Nir
09/13/2020, 7:53 PMNir
09/13/2020, 7:54 PMNir
09/13/2020, 7:54 PMNir
09/13/2020, 7:56 PMNir
09/13/2020, 7:57 PMNir
09/13/2020, 7:57 PMHarry
09/14/2020, 1:58 PMlateinit val
.
Thanks for your answers, it is helpful to me.