poohbar
03/19/2019, 5:47 PMinstance
nullable, i just want simpleName
to return ""
in that case.. but even wrapping it in if (instance != null)
does not helpShawn
03/19/2019, 5:49 PMR
as Any
-
class Wrapper<R : Any>(private val instance: R? = null)
poohbar
03/19/2019, 5:49 PMAny?
by default?Shawn
03/19/2019, 5:50 PMstreetsofboston
03/19/2019, 5:51 PMinstance
is a R?
, it still can be null…poohbar
03/19/2019, 5:51 PMR?
exactly does..poohbar
03/19/2019, 5:51 PMpoohbar
03/19/2019, 5:52 PMpoohbar
03/19/2019, 5:52 PM!!
Shawn
03/19/2019, 5:52 PMstreetsofboston
03/19/2019, 5:52 PMclass Wrapper<R : Any>(private val instance: R? = null){
fun simpleName() = instance?.let { it::class.simpleName } // no more error on this line
}
Shawn
03/19/2019, 5:52 PM<R : Any>
, it’s possible to have a valid wrapper of both Wrapper<Foo>
and Wrapper<Foo?>
Shawn
03/19/2019, 5:53 PMfun simpleName() = if (instance != null) instance::class.simpleName else ""
seems to work just finestreetsofboston
03/19/2019, 5:53 PMval
, not a `var`…Shawn
03/19/2019, 5:54 PMFoo
but actually holds null
is perfectly valid - it’s kinda how Optional workspoohbar
03/19/2019, 5:55 PMShawn
03/19/2019, 5:55 PMstreetsofboston
03/19/2019, 5:55 PMinstance
do class Wrapper<R : Any>(private val instance: R)
Shawn
03/19/2019, 5:55 PMpoohbar
03/19/2019, 5:55 PMShawn
03/19/2019, 5:56 PMWrapper<Foo?>
, you want it to be Wrapper<Foo>
, much like Optional<Foo>
if you’ve used that at all in Java-landShawn
03/19/2019, 5:57 PMR
itself is a nullable type, then there is no way to safely dereference itstreetsofboston
03/19/2019, 5:57 PMclass Wrapper<R : Any>(private val instance: R? = null)
poohbar
03/19/2019, 5:59 PMOptional<Foo>
can hold null
🤔poohbar
03/19/2019, 5:59 PMFoo?
poohbar
03/19/2019, 5:59 PMpoohbar
03/19/2019, 5:59 PMstreetsofboston
03/19/2019, 6:00 PMclass Wrapper<R>(private val instance: R?){
fun simpleName() = instance?.let { it::class.simpleName } // error on this line
}
The it::
has an error about it
being a nullable type. Event though instance?.let { }
was called with a .?
operator, it
is still nullable. It almost looks like instance
is R??
Shawn
03/19/2019, 6:01 PMR??
isn’t a bad way of putting it actuallyShawn
03/19/2019, 6:02 PMOptional<R>
is essentially the only way Java can really express the notion of R | null
or a union of those two typesShawn
03/19/2019, 6:03 PMR
itself to be nullable you’d have some kind of weird Union[Union[R, null], null]
streetsofboston
03/19/2019, 6:03 PMR??
, it is actually R?????????????????????????????????? ....
This still has a nullability error in the compiler: fun simpleName() = instance?.let { it?.let { it::class.simpleName } }
Shawn
03/19/2019, 6:03 PM