wcaokaze
05/02/2018, 9:10 AMrrader
05/02/2018, 9:12 AMa?.b?.toString()?.length is more natural? even if compiler knows b can not be null?karelpeeters
05/02/2018, 9:12 AMedwardwongtl
05/02/2018, 9:15 AM?. return is nullable, so the compiler will always be unsure about it is null or notwcaokaze
05/02/2018, 9:17 AMrrader
05/02/2018, 9:17 AM?. everywhere is just like to put if not null everywhere in javaedwardwongtl
05/02/2018, 9:17 AMedwardwongtl
05/02/2018, 9:18 AMrrader
05/02/2018, 9:18 AM. operator ? just use ?. everywheremenegatti
05/02/2018, 9:18 AMedwardwongtl
05/02/2018, 9:19 AMa.b is for Non-null types, which you're telling the compiler a will never be nullmenegatti
05/02/2018, 9:19 AM. and !!rrader
05/02/2018, 9:20 AMa?.b?.toString()?.length in this chain b for sure is not null so why to put ?. ?edwardwongtl
05/02/2018, 9:20 AMb is not for sure not nullkarelpeeters
05/02/2018, 9:20 AM(a?.b) can be though.karelpeeters
05/02/2018, 9:20 AM?. has to work on.edwardwongtl
05/02/2018, 9:20 AMedwardwongtl
05/02/2018, 9:21 AM?. will be nullablekarelpeeters
05/02/2018, 9:22 AMdata class A(val b: Int))karelpeeters
05/02/2018, 9:22 AMa.b is never null.edwardwongtl
05/02/2018, 9:23 AMa?.b will be translated by the compilerrrader
05/02/2018, 9:24 AMdata class A(val b: Int) this mean b for sure can not be null or not?karelpeeters
05/02/2018, 9:24 AMkarelpeeters
05/02/2018, 9:24 AMa.b is never null, a?.b is null if a was null.rrader
05/02/2018, 9:24 AM?. after it?karelpeeters
05/02/2018, 9:25 AM?. really comes after (a?.b) which can be null.menegatti
05/02/2018, 9:25 AMval c = a?.b what’s the value of c?menegatti
05/02/2018, 9:25 AMInt or null, do you agree?edwardwongtl
05/02/2018, 9:25 AMa will be null or not, so the result of a?.b is also not sure at compile timemenegatti
05/02/2018, 9:27 AMc could be either one of those, then it’s type can only be Int?, as a result operating on c requires a null check, the safe call operator, or the double bangs (!!)rrader
05/02/2018, 9:28 AMkarelpeeters
05/02/2018, 9:29 AMkarelpeeters
05/02/2018, 9:29 AMkarelpeeters
05/02/2018, 9:29 AMc in @menegatti's example to be trivally inlinable.karelpeeters
05/02/2018, 9:30 AMmenegatti
05/02/2018, 9:33 AMa?.b?.c getting in your waymenegatti
05/02/2018, 9:33 AMb is not nullable if a is also non-null it can cast it to Int instead of keeping it as an Int?rrader
05/02/2018, 9:34 AMa?.b?.c if a is null the chain will continue, so no stop at first nullrrader
05/02/2018, 9:34 AMmenegatti
05/02/2018, 9:37 AMrrader
05/02/2018, 9:37 AMmenegatti
05/02/2018, 9:38 AMa?.b?c, it is:
if (a != null) {
    B b = a.getB();
    if (b != null) {
        C c = b.getC(); 
    }
}menegatti
05/02/2018, 9:39 AMa is null, the rest of the chain is never called, unless the function you’re calling has an extension to Any?rrader
05/02/2018, 9:39 AMclass A(val b: Int)
fun main(args: Array<String>) {
    val b : Int? = null
    val a = if (b != null) { A(b) } else { null }
    println(a?.b.toString().length)
    
}
prints 4menegatti
05/02/2018, 9:40 AMtoString() works with nullable typesrrader
05/02/2018, 9:41 AMrrader
05/02/2018, 9:42 AMmenegatti
05/02/2018, 9:43 AM1.
if (a != null) {
    println(a.b.toString().length()
} else {
    // not sure
}
2.
println(a?.let { it.b.toString().length} ?: //not sure )edwardwongtl
05/02/2018, 9:43 AMtoString() in this conversation is not really helpful as it has hidden logic inside to deal with nullmenegatti
05/02/2018, 9:43 AMmenegatti
05/02/2018, 9:44 AMmenegatti
05/02/2018, 9:45 AM