Is there a way to inhibit the Kotlin null safety c...
# getting-started
d
Is there a way to inhibit the Kotlin null safety check, like making a field
private var myThing: MyThing? = null
is the current form in use. The item is initialize via a
@PostConstruct
method, but I don't want to use
myThing!!
every time I use it. Thanks
y
lateinit var myThing: MyThing
☝️ 5
👍 2
d
Hmm, I use that for Dependency Injected fields, so I can also use it for this scenario ?
p
var myThing: MyThing = EmptyObjectThing()
Or simply in every usage site:
Copy code
val myThingNonNull = myThing ?: return
myThingNonNull.foo()
d
The
lateinit
looks more correct, but it will be a few minutes before I validate the answer. My talk of using a
@PostConstruct
implies I have complex initialization of this item, and the assumption I have a compatible instance of the type to use as a placeholder (in the language construction of the object) until I re-assign it in
fun postConstruct() {}
would not work such as
var myThing = MyThing.emptyPlaceholder()
as the type is too complex
p
That's fine but lateinit is null unsafe
Not much different than
!!
d
Understand (null unsafe, as per my original description to inhbibit null safety check) The issue is the 500 times the term
!!
appears and the eyesore that produces, for a scenario that if it is found to be a null value should raise an exception at runtime, because it will be a programming error that needs a fix (not a runtime error recovery, which is the function of exceptions). Thanks for your reply.
👍 1
p
I see, in this situation
lateinit
will be the man for the job.
s
lateinit
😁 1
mandalorian 1