kotlin - run vs elvis operator - what is the diffe

2019-07-04 12:05发布

问题:

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 ?

回答1:

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.



标签: kotlin