rook
02/19/2020, 5:33 PMlateinit var func: () -> Unit
has been initialized? Or, if there’s a better strategy for this, I’m open to other solutions.Aleksa Boškov
02/19/2020, 5:36 PM::func.isInitialized
streetsofboston
02/19/2020, 5:38 PMisInitialized
with caution.
I’d use either a nullable var:
var func: (() -> Unit)? = null
or a lazy one, where it will be initialized lazily given the class’ dependencies:
val func: () -> Unit by lazy { /* create value for func */ }
streetsofboston
02/19/2020, 5:40 PMval func
) correctly, using by lazy
should work fine)rook
02/19/2020, 5:42 PMby lazy
not an option. But I think the nullable field is probably a better route.dam5s
02/19/2020, 6:20 PMlateinit
is not designed for things that may not be set, it's definitely for values that you know will have a value the first time you access them.streetsofboston
02/19/2020, 6:30 PMlateinit
only for classes whose lifecycle is not under your control and are initialized on a callback later (eg onCreate(...), initialize(...) calls, dependency @Inject, etc).