kotlin - run vs elvis operator - 有什么区别?

j2e*_*nue 3 kotlin

我试图了解kotlin中以下两段代码之间的区别:

myVar?.let { print(it) } ?: run { print("its null folks") }
Run Code Online (Sandbox Code Playgroud)

VS

myVar?.let { print(it) } ?:  print("its null folks")
Run Code Online (Sandbox Code Playgroud)

他们相同吗?运行只是为了我们可以使用一个代码块而另一个只用于一个语句?

yol*_*ole 9

是的,它们是等价的.run允许您在elvis运算符的右侧使用多个语句; 在这种情况下,只有一个,所以run不需要.