feroz_baig
10/03/2017, 6:45 PMdataList?.size+1 ?: 0
basically, if datalist isnt null, return its size+1. else, 0kevinherron
10/03/2017, 6:47 PMval size: Int = list?.let { it.size + 1 } ?: 0
Shawn
10/03/2017, 6:48 PMif/else
?kevinherron
10/03/2017, 6:49 PMShawn
10/03/2017, 6:49 PMkevinherron
10/03/2017, 6:50 PMkevinherron
10/03/2017, 6:50 PMval size: Int = if (list == null) 0 else list.size + 1
skennedy
10/03/2017, 6:50 PMdataList?.size?.let { it + 1 } ?: 0
dalexander
10/03/2017, 6:51 PMlet
is inline.Shawn
10/03/2017, 6:51 PMval size = list?.size?.plus(1) ?: 0
đ¤dalexander
10/03/2017, 6:52 PM(dataList?.size ?: -1) + 1
đ§Shawn
10/03/2017, 6:52 PMShawn
10/03/2017, 6:52 PMkevinherron
10/03/2017, 6:52 PMShawn
10/03/2017, 6:52 PMferoz_baig
10/03/2017, 6:53 PMferoz_baig
10/03/2017, 6:54 PMShawn
10/03/2017, 6:56 PMsize?.let
block does?dalexander
10/03/2017, 6:56 PMlet
is probably worse than just using if/else
Shawn
10/03/2017, 6:57 PMdataList?.size?.inc() ?: 0
but Iâm not sure how that works with r/o propertiesferoz_baig
10/03/2017, 6:59 PMdataList?.size?.inc() ?: 0
will also check if size == null, then return 0. else inc().
but size can never be null. so elvis operator is useless heredalexander
10/03/2017, 6:59 PM?.
doesnât short-circuit the statement if thereâs a null value (unlike C#)Shawn
10/03/2017, 7:00 PMferoz_baig
10/03/2017, 7:01 PMShawn
10/03/2017, 7:02 PMferoz_baig
10/03/2017, 7:02 PMdalexander
10/03/2017, 7:02 PMdataList
is null then dataList?.size
will evaluate to null.Shawn
10/03/2017, 7:02 PMdataList
is null
, null?.size
would also be nulldalexander
10/03/2017, 7:14 PM?.
for a few minutes, it doesnât call methods if the receiver is null, but it will continue to traverse the statement, which is a bit more nuanced behavior.Shawn
10/03/2017, 7:15 PMdalexander
10/03/2017, 7:16 PMdataList?.size?.let { println("First") }.let { println("Second") }
it prints âFirst\n Secondâ if dataList is null, otherwise just âSecondâ (because the second let statement doesnât use ?.
Andreas Sinz
10/03/2017, 7:34 PMlet
returns null
, the elvis part will still be calledAndreas Sinz
10/03/2017, 7:34 PM""Hello World"?.let { println("Not null"); return@let null } ?: println("Its null :(")