https://kotlinlang.org logo
#announcements
Title
# announcements
f

feroz_baig

10/03/2017, 6:45 PM
how can i do
Copy code
dataList?.size+1 ?: 0
basically, if datalist isnt null, return its size+1. else, 0
k

kevinherron

10/03/2017, 6:47 PM
val size: Int = list?.let { it.size + 1 } ?: 0
🙂 1
s

Shawn

10/03/2017, 6:48 PM
Does that really have much benefit over an
if/else
?
k

kevinherron

10/03/2017, 6:49 PM
eye of the beholder 🤷
s

Shawn

10/03/2017, 6:49 PM
Is there any kind of performance penalty with the extra lambda there?
k

kevinherron

10/03/2017, 6:50 PM
i don’t know what the bytecode becomes. if worried, if/else would work too
val size: Int = if (list == null) 0 else list.size + 1
s

skennedy

10/03/2017, 6:50 PM
dataList?.size?.let { it + 1 } ?: 0
d

dalexander

10/03/2017, 6:51 PM
Shouldn’t be much of one,
let
is inline.
s

Shawn

10/03/2017, 6:51 PM
Copy code
val size = list?.size?.plus(1) ?: 0
🤔
d

dalexander

10/03/2017, 6:52 PM
(dataList?.size ?: -1) + 1
🧌
🧌 7
s

Shawn

10/03/2017, 6:52 PM
you monster
😛
k

kevinherron

10/03/2017, 6:52 PM
well this got out of hand
s

Shawn

10/03/2017, 6:52 PM
better in a thread than spamming #general I guess
f

feroz_baig

10/03/2017, 6:53 PM
@dalexander lol. ya thought of that (for a workaround) but it is not clean
@Shawn that would check if size is null
s

Shawn

10/03/2017, 6:56 PM
would it not essentially do what the
size?.let
block does?
d

dalexander

10/03/2017, 6:56 PM
I think Shawn’s last suggestion is probably the best, I think using
let
is probably worse than just using
if/else
s

Shawn

10/03/2017, 6:57 PM
if you wanna be even more clever about it, perhaps
Copy code
dataList?.size?.inc() ?: 0
but I’m not sure how that works with r/o properties
f

feroz_baig

10/03/2017, 6:59 PM
dataList?.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 here
d

dalexander

10/03/2017, 6:59 PM
Size can definitely be null,
?.
doesn’t short-circuit the statement if there’s a null value (unlike C#)
s

Shawn

10/03/2017, 7:00 PM
whoa, TIL
f

feroz_baig

10/03/2017, 7:01 PM
you're right
s

Shawn

10/03/2017, 7:02 PM
https://kotlinlang.org/docs/reference/null-safety.html#safe-calls the copy seems to kinda imply it does just short-circuit if it is null, but maybe that’s just me reading it a bit over eagerly
f

feroz_baig

10/03/2017, 7:02 PM
could you give me a case where size can be null
d

dalexander

10/03/2017, 7:02 PM
if
dataList
is null then
dataList?.size
will evaluate to null.
👍 1
s

Shawn

10/03/2017, 7:02 PM
if
dataList
is
null
,
null?.size
would also be null
👍 1
d

dalexander

10/03/2017, 7:14 PM
Oh, I guess I should clarify, after poking at
?.
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.
s

Shawn

10/03/2017, 7:15 PM
@dalexander ahh, gotcha. good to know 👍
d

dalexander

10/03/2017, 7:16 PM
this is my test statement
dataList?.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
?.
a

Andreas Sinz

10/03/2017, 7:34 PM
but be aware of the fact that if the lambda of
let
returns
null
, the elvis part will still be called
try:
""Hello World"?.let { println("Not null"); return@let null } ?: println("Its null :(")
3 Views