class Kt19<T>(var isShow: Boolean, var info: T) {
fun <O> map(mapAction: (T) -> O) = mapAction(info).takeIf { isShow }
}
fun <T, R> map(isShow: Boolean,info: T,mapAction: (T) -> R) = mapAction(info).takeIf { isShow }
fun main() {
val info = Kt19(true, 3434)
var ss = info.map {
"包裹了【${it}】"
}
println("int to string:${ss}")//结果:int to string:包裹了【3434】
var ww = map(false,23){
"啦啦啦啊【${it}】"
}?:"为空了!"
println(ww) //结果:为空了!(isShow为false,所以takeIf返回null)
}
12-28
381
12-16
1582