<@U3QQ65FUZ> What kind of vars are those? Why are ...
# language-proposals
e
@edenman What kind of vars are those? Why are they nullable? Have you tried using
lateinit
? Can you share some code?
1
e
@elizarov an example: my app has a list of place cards (for all the nearby hotels)…because they’re reusable views in a
PagerAdapter
, each
PlaceCardView
can be in a different state (loading, showing a place, or showing the “no nearby places” state).
PlaceCardView
has a
private var place: Place? = null
property and three modifier methods (
showPlace(place: Place)
,
showLoading()
,
showNoNearbyPlaces()
) that set/unset the
place
var then the click listeners for the various views all have to do
Copy code
foo.setOnClickListener { 
  val place = place
  if (place == null) {
    // do a thing
  } else {
    // do the other thing
  }
}
lateinit doesn’t work because i need to check if the value is set or not and i need to be able to unset it.
the alternative is to call
setOnClickListener
every time the
place
changes, but that feels even more gross to me in this case
o
Use
EmptyObject
pattern. Make a singleton
object Nowhere : Place
and use it whenever place is not defined. You will save yourself a lot of issues with null handling.
e
i thought the whole point of modelling optional things in the type system was to take advantage of that nullability? 🙂
o
Well, it’s not a silver bullet, sometimes other tools suite better