In Kotlin, we could create a scope function as below.
inline fun <R> run(block: () -> R): R {
return block()
}
We could use it as below to choice which view to show.
run {
if (choice) viewOne else viewTwo
}?.show()
In Swift, we could also declare as below
@inline(__always) func run<R>(block: () -> R) -> R {
return block()
}
However, when we use it, it has to be really verbose as below.
run { () -> View? in // Need to explicitly show `View is return
if choice {
return viewOne // Need to explicitly return
} else {
return viewTwo // Need to explicitly return
}
}?.show()
Is there anywhere we could reduce the verbosity of Swift that I'm not aware, that it looks more concise? (matching closer to Kotlin)
UPDATE
Show entire code
@inline(__always) func run<R>(block: () -> R) -> R {
return block()
}
class View {
func show() {print("A")}
}
let viewOne: View = View()
let viewTwo: View = View()
var choice = true
run { () -> View in
return choice ? viewOne : viewTwo
}.show()