gabrielfv
10/19/2018, 3:22 PMfun method(obj: Class) { //... }
2️⃣ (Will do nothing when obj
is null)
fun method(obj: Class?) {
if (obj == null) return
// ...
}
3️⃣ (Behaves like 2, assignment notation also applies method(...) = obj?.let{
, which may be seen as better alternative?)
fun method(obj: Class?) {
obj?.let {
// Wraps whole method around this block, up to 10 lines of code
}
}
Mike
10/19/2018, 3:25 PMcheckNotNull
or requireNotNull
at the top which will throw an IllegalArgumentException/IllegalStateException.
Until 1.3 is released, you’ll end up with
val obj = checkNotNull(objMaybeNull)
1.3 leverages contracts and the assignment won’t be required anymore.bartvh
10/21/2018, 11:08 AMnull
?Mike
10/21/2018, 2:15 PM