DALDEI
09/11/2019, 5:22 AM"hi".if( this == "bye" ) println( "BYE" ) else println( this )
But you can do this
"hi".run {
if( this == "bye" ) println( "BYE" ) else println( this )
}
Another common use case for run is as the body of a function that is more then 1 expressions so you can 'avoid' using return ..
fun printAndAdd( a: Int ) : Int {
println(a)
return a+1
}
vs
fun printAndAdd( a: Int ) = run {
println(a)
a+1
}
Note: the later case allows the compiler to infer the return type as well as eliminate the return statement
fairly minor difference in the above case but can be more useful as the body gets more complex.