Andrew Gazelka
05/14/2019, 10:02 PMInteger
for lateinit num: Int
Integer
for Int?
serebit
05/14/2019, 10:04 PMnum
to an arbitrary number by default, since it’ll be set later anyway?Ruckus
05/14/2019, 10:04 PMAndrew Gazelka
05/14/2019, 10:04 PMserebit
05/14/2019, 10:05 PMAndrew Gazelka
05/14/2019, 10:07 PMRuckus
05/14/2019, 10:07 PMNormally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class.
Andrew Gazelka
05/14/2019, 10:07 PMlateinit Integer
Ruckus
05/14/2019, 10:08 PMserebit
05/14/2019, 10:08 PMAndrew Gazelka
05/14/2019, 10:09 PMlateinit
is nice because if we do make a mistake and forget to initialize it properly, we won't get weird errors-1
from a function instead of null
is kinda jank (and esp bad if the range of the fun is [-inf,inf]
serebit
05/14/2019, 10:11 PMAndrew Gazelka
05/14/2019, 10:12 PMserebit
05/14/2019, 10:12 PMAndrew Gazelka
05/14/2019, 10:13 PMCody Engel
05/14/2019, 10:17 PMInteger
in that case.serebit
05/14/2019, 10:22 PMInt?
can and should be autoboxed because it might be null after being initialized. A lateinit Int
would only be null pre-initialization, so it seems odd to box it for the entirety of the runtime.karelpeeters
05/14/2019, 10:36 PMCody Engel
05/14/2019, 10:40 PMlateinit Int
never crossed my mind or bothered me. I assume Kotlin is going to do stuff under the hood, as long as my code works I’m not too concerned. If I had to get too nitty gritty with that stuff I’d imagine things like Kotlin/Native would be much more verbose and difficult.karelpeeters
05/14/2019, 10:41 PMserebit
05/14/2019, 10:41 PMRuckus
05/14/2019, 10:44 PMkarelpeeters
05/14/2019, 10:44 PMserebit
05/14/2019, 10:44 PMkarelpeeters
05/14/2019, 10:45 PMRuckus
05/14/2019, 10:45 PMkarelpeeters
05/14/2019, 10:45 PMserebit
05/14/2019, 10:46 PMRuckus
05/14/2019, 10:48 PMit’s only there for things that can’t be initialized where they standWhich happens quite often anytime you use a framework with custom life cycles
ilya.gorbunov
05/15/2019, 2:01 AM