when would you return null for Unit?
# announcements
a
when would you return null for Unit?
o
doAnswer2
defines
answer
as returning
T?
which is nullable of T. You specify
Unit
as type parameter, so your lambda should return
Unit?
, nullable Unit. Which can be either
null
or
Unit
(singleton instance). Kotlin can automatically provide
Unit
when needed, and optimize it to void on JVM, but when it’s
Unit?
it can’t.
1