hallvard
10/19/2020, 11:08 AMlateInitVar.isInitialized() from java?ribesg
10/19/2020, 11:12 AMisInitialized?Vampire
10/19/2020, 11:48 AMNullPointerException if the variable was not initialized in time, but either handle it differently or throw a more meaningful error.hallvard
10/19/2020, 11:51 AMribesg
10/19/2020, 11:55 AMlateinit here. I only use isInitialized when debuggingVampire
10/19/2020, 11:57 AMhallvard
10/19/2020, 12:00 PMribesg
10/19/2020, 12:02 PMonCreate should be nullablekralli
10/19/2020, 12:27 PMisInitialized is an intrinsic. It does not exist in the compiled code. Instead it’s translated by the compiler to lateInitVar != null .hallvard
10/19/2020, 12:30 PMfun lateInitVarInitialized() = ::lateInitVar.isInitializedkralli
10/19/2020, 12:31 PMhallvard
10/19/2020, 12:32 PMVampire
10/19/2020, 12:37 PMonCreate sounds to me like the typical injection use-case for which lateinit vars are intended.
Might be my lack of Android dev knowledge, but why do you think it is not appropriate and what do you think it is appropriate for instead?ribesg
10/19/2020, 12:42 PMonCreate is called on it. It’s not like when there’s something like a deserializer which calls an empty constructor and sets lateinit fields right away. It’s easy to write myActivity.x without thinking that at this point onCreate() hasn’t been called, so I prefer to have to write val x = myActivity?.x ?: error("…"). Just writing myActivity.x and seeing the error makes me think about it.kralli
10/19/2020, 12:42 PMVampire
10/19/2020, 12:46 PMIt’s just that you can reference the instance and call stuff on it beforeOh, really? Android has a strange API imho then. From the name I would have expected it to be like ais called on it.onCreate
@PostCreate method in Spring or `@Inject`ed field in CDI which are done right after instantiation and before you get it for further operations.CLOVIS
10/19/2020, 3:00 PMonCreate is called when the activity is displayed to the user (or just a bit before that) which is not necessarily at the same time as object instantiation 🤔