warriorprincess
05/24/2018, 5:12 AMgildor
05/24/2018, 6:07 AMwhy no ‘?’ necessary? because checkced above alreadyYes, this is smartcast https://kotlinlang.org/docs/reference/typecasts.html
gildor
05/24/2018, 6:08 AMwarriorprincess
05/24/2018, 6:50 AMwarriorprincess
05/24/2018, 6:51 AMwarriorprincess
05/24/2018, 6:51 AMwarriorprincess
05/24/2018, 7:19 AMwarriorprincess
05/24/2018, 7:20 AMwarriorprincess
05/24/2018, 7:20 AMwarriorprincess
05/24/2018, 7:21 AMk1 = k1?.next; k2 = k2?.next;
warriorprincess
05/24/2018, 7:21 AMgildor
05/24/2018, 7:23 AMit does so because of the null safety of ‘?.’ right?What do you mean?
warriorprincess
05/24/2018, 7:24 AMwhile (!(k1 == null && k2 == null)) {
the loop will run till head of both lists is nullwarriorprincess
05/24/2018, 7:25 AMk1 = k1?.next; k2 = k2?.next;
will keep advancing the head of each listwarriorprincess
05/24/2018, 7:25 AMwarriorprincess
05/24/2018, 7:25 AMnext
does not exist for null: class
warriorprincess
05/24/2018, 7:26 AMk1 = k1?.next
will set k1
to null
if k1.next
is null?gildor
05/24/2018, 7:28 AMk1
will be set to null if k1
is null or if k1.next
is nullgildor
05/24/2018, 7:28 AMwarriorprincess
05/24/2018, 7:29 AMk1?.next
BUT k1 is already null
, what happens then?warriorprincess
05/24/2018, 7:30 AMgildor
05/24/2018, 7:30 AMwarriorprincess
05/24/2018, 7:31 AMnull
, so why no error?warriorprincess
05/24/2018, 7:31 AM?.
operator works?gildor
05/24/2018, 7:31 AM?
gildor
05/24/2018, 7:32 AMwarriorprincess
05/24/2018, 7:32 AM"gtgt"?.onfrionfreinfroinf
and it will just give null but no error?gildor
05/24/2018, 7:32 AMwarriorprincess
05/24/2018, 7:33 AMwarriorprincess
05/24/2018, 7:33 AMgildor
05/24/2018, 7:34 AM!!
) operator, which is unsafe one and throws execption if expression returns nullgildor
05/24/2018, 7:35 AMseems like what happens in dynamic typingno, why? it’s not a dynamic typing, it’s actually more strict static typed than in languages without native support of nullable types
gildor
05/24/2018, 7:36 AMvar foo = null
foo.bar // Crash on runtime
gildor
05/24/2018, 7:36 AMString foo = null
foo.length // Crash on runtime
gildor
05/24/2018, 7:37 AMval foo: String = null // Compile time error
val foo: String? = null // this is fine
foo.length // this is restricted, compile time error
foo?.length // this is fine
foo!!.length // this is also fine, but will crash on runtime, it's unsafe operator
gildor
05/24/2018, 7:40 AM?.
is just shorter version of if condition for null or ternary operator:
foo = foo?.bar
//same as ternary operator for null check:
foo = foo == null ? null : foo.bar
gildor
05/24/2018, 7:41 AMif (foo != null) {
foo.bar()
}
the same with safe call operator:
foo?.bar()
gildor
05/24/2018, 7:41 AMfoo?.bar?.baz?.doSomething()
Now try to rewrite this with if condtionwarriorprincess
05/24/2018, 8:03 AMwarriorprincess
05/24/2018, 8:03 AMwarriorprincess
05/24/2018, 8:03 AMwarriorprincess
05/24/2018, 8:03 AMgildor
05/24/2018, 8:07 AM