I’m curious what other Kotlin programmers are doing here. I of course agree that it’s a step up from Java to be explicit about nullable types. But I’m finding that sometimes I have a hard time creating my objects with all their fields initialized from the start, and then the code is polluted with some amount of
!!, which I don’t like.
For example, in some Kotlin/JS I have some
Controller objects – one for each tab in the UI. These controllers setup some DOM only when the user clicks a tab. I’ve also had some state machines where variables aren’t initialized until that state is reached.
So, I’m curious…
- How rare is !! in your code?
- Are there some common techniques for dealing with situations like I described.
I’ve been using Swift and Kotlin lately. Swift has another nullable variation…
var foo: Foo!
which goes back to the old way. It’s like
Foo? except the
!! is implicit when you reference it. I wonder if a similar thing is possible in Kotlin, or might someday be possible.