Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin - run vs elvis operator - what is the difference?

Tags:

kotlin

im trying to understand the difference between the following two pieces of code in kotlin:

myVar?.let { print(it) } ?: run { print("its null folks") }

vs

myVar?.let { print(it) } ?:  print("its null folks")

are they equivalent ? is run just so we can use a block of code and the the other is for just a single statement ?

like image 587
j2emanue Avatar asked Dec 18 '22 22:12

j2emanue


1 Answers

Yes, they are equivalent. run allows you to use multiple statements on the right side of an elvis operator; in this case there's only one, so run is not needed.

like image 117
yole Avatar answered Dec 20 '22 11:12

yole