Karlo Lozovina
04/29/2021, 8:30 PMlateinit
can't go on primary constructor parameters, is there a less manual way of doing something like this (lateinited class fields that can be set through the constructor)?:
class Foo(x: String? = null) {
lateinit var x: String
init {
if (x != null) this.x = x
}
fun show() = println("Foo: $x")
}
Shawn
04/29/2021, 8:35 PMx
need to be lateinit
in this example?Javier
04/29/2021, 8:36 PMclass Foo(var x: String) { }
Javier
04/29/2021, 8:37 PMclass Foo(var x: String = null!!) { }
Javier
04/29/2021, 8:37 PMOlivier Patry
04/29/2021, 8:43 PMthis.x
if provided x
is null
then? (answer: a runtime crash ๐ฅ)
lateinit
, the Kotlin's NullPointerException
๐
I would go either for:
class Foo(x: String? = null) {
val x: String = x ?: "SomeFallbackValue"
}
or
class Foo(val x: String? = null)
This way, at compile time your know you are safe either with a valid/known/non-null fallback value or by explicitly checking for null
when using x
James Richardson
04/29/2021, 8:44 PMKarlo Lozovina
04/29/2021, 8:44 PMx
field that's lateinit (with regular dangers that come with it) and ability to set the x
field in the constructor (in case it's possible)Olivier Patry
04/29/2021, 8:46 PMString?
Karlo Lozovina
04/29/2021, 8:46 PMfoo.x!!
all over the codebase, or lateinit x
Olivier Patry
04/29/2021, 8:46 PMlateinit
might be more relevant on Android to initialize value in onCreate
for instance when you know nothing will use it before Android calls you (view reference for instance)Karlo Lozovina
04/29/2021, 8:46 PMx
is accessed is not null, so the !!
is just noiseKarlo Lozovina
04/29/2021, 8:47 PMOlivier Patry
04/29/2021, 8:48 PM!!
, but the checks you mentioned ๐ค either using if (foo.x != null)
(+ smart cast) or foor.x?.bar
Karlo Lozovina
04/29/2021, 8:51 PM?.
are also redundant in huge parts of the code, because the field can't be null
if I'm not mistaken, smart casts also wont work because the field is var
not val
, right?Olivier Patry
04/29/2021, 8:55 PMOlivier Patry
04/29/2021, 8:55 PMOlivier Patry
04/29/2021, 8:56 PMKarlo Lozovina
04/29/2021, 8:58 PMif (x !=null) doSomething(x)
because the else branch in this case has to be error("cant continue")
... either the field is there or the program has to crashOlivier Patry
04/29/2021, 9:00 PMfoo.x?.let { doSomething(it) } ?: error("can't continue")
Olivier Patry
04/29/2021, 9:01 PMdoSomething(checkNotNull(foo.x) { "can't continue" })
Olivier Patry
04/29/2021, 9:01 PMOlivier Patry
04/29/2021, 9:01 PMOlivier Patry
04/29/2021, 9:02 PMlateinit
it's just Russian roulette depending on the mood of who uses your codeKarlo Lozovina
04/29/2021, 9:03 PMKarlo Lozovina
04/29/2021, 9:03 PMOlivier Patry
04/29/2021, 9:04 PMKarlo Lozovina
04/29/2021, 9:06 PMKarlo Lozovina
04/29/2021, 9:07 PMOlivier Patry
04/29/2021, 9:07 PMsealed class Topic {
object Situation1 : Topic()
data class Situation2(val x: String) : Topic()
data class Situation3(val x: String, y: String) : Topic()
}
fun clientSide(topic: Topic) {
when (topic) {
Situation1 -> doBasicStuff()
is Situation2 -> doSomething(topic.x)
is Situation3 -> doAdvancedStuff(topic.x, topic.y
}
}
Karlo Lozovina
04/29/2021, 9:08 PMOlivier Patry
04/29/2021, 9:08 PM