gbaldeck
02/06/2020, 6:32 PMlatinit val
in Kotlin?streetsofboston
02/06/2020, 6:36 PMval myLateVal
correctly, you can use by lazy
to postpone evaluation of this val.streetsofboston
02/06/2020, 6:37 PMval myLateInitFullName by lazy { dep1.firstName + "." + dep1.lastName }
Mike
02/06/2020, 6:50 PMval
implies immutable. But lateinit
means it won't be set right away, and will be changed at some point to point to something real. var
makes that aspect clear.
I also suspect var
was chosen as we're being trained to not want to see var
, so we'll be more inclined to look at alternatives.
Constructor injection, lazy
or other approaches so that we don't have a var
in our code.gbaldeck
02/06/2020, 7:20 PMval
immediately, so technically all local val
variables are optionally lateinit
. The only thing I can think of is that they did it because of concurrency reasons. For instance, in Dart, they allow late final fields, but Dart is single-threaded.diesieben07
02/06/2020, 7:24 PMlateinit
was designed primarily for interop with "injection by magic" frameworks, e.g. DI without constructor injection.
In those cases lateinit
marks a property as publicly accessible and designed to be modified by third party code. Because it's third party code you cannot guarantee that it's not going to be modified more than once. So it's a var
gbaldeck
02/06/2020, 7:29 PMlateinit val
also just for everyday use? It doesn't have to be final when it compiles to Java. It can be a normal variable but the immutability is enforced by the Kotlin compiler.diesieben07
02/06/2020, 7:31 PMby Delegates.notNull()
is for. lateinit
fundamentally only works for reference typesgbaldeck
02/06/2020, 7:36 PMDelegates.notNull()
, and that is not what its for at all. It is basically lateinit
for primitive types. How does it allow you to do a lateinit val
exactly?diesieben07
02/06/2020, 7:37 PMdiesieben07
02/06/2020, 7:37 PMnotNull
is definitely not just for primitive types.gbaldeck
02/06/2020, 7:38 PMlateinit
. https://americanexpress.io/advanced-kotlin-delegates/diesieben07
02/06/2020, 7:39 PMlateinit
is a kludge for interaction with "lets inject stuff into fields" frameworks. Delegates.notNull()
is a standard built-in delegate for properties that will be initialized at a later point in time.diesieben07
02/06/2020, 7:40 PMlateinit
is not just "this will be initialized later", it also has @JvmField
built-in.gbaldeck
02/06/2020, 7:42 PMval
had that as well. Other languages do, and the only reason I can see Kotlin not having some sort of feature like that is for concurrency reasons.diesieben07
02/06/2020, 7:43 PMDelegates.initializedOnce
.diesieben07
02/06/2020, 7:44 PMgbaldeck
02/06/2020, 7:44 PMdiesieben07
02/06/2020, 7:44 PMlateinit var
also has no compile-time checking.diesieben07
02/06/2020, 7:45 PMgbaldeck
02/06/2020, 7:45 PMdiesieben07
02/06/2020, 7:46 PMgbaldeck
02/06/2020, 7:46 PMgbaldeck
02/06/2020, 7:46 PMdiesieben07
02/06/2020, 7:46 PMgbaldeck
02/06/2020, 7:46 PMdiesieben07
02/06/2020, 7:47 PMgbaldeck
02/06/2020, 7:47 PMgbaldeck
02/06/2020, 7:47 PMdiesieben07
02/06/2020, 7:47 PMgbaldeck
02/06/2020, 7:47 PMdiesieben07
02/06/2020, 7:48 PMgbaldeck
02/06/2020, 7:51 PMgbaldeck
02/06/2020, 7:51 PMclass Test {
late final String value;
init(){
value = "test";
value = "test";
}
}
gbaldeck
02/06/2020, 7:52 PMdiesieben07
02/06/2020, 7:53 PMdiesieben07
02/06/2020, 7:53 PMgbaldeck
02/06/2020, 7:56 PM