聪明地施放到kotlin.String

Mal*_*ngh 5 android compiler-warnings kotlin kotlin-null-safety

我正在尝试Kotlin并从编译器获得消息:

Smart cast to kotlin.String
Run Code Online (Sandbox Code Playgroud)

码:

/*"mTripStatus" is a nullable String*/
var html :String = HTML
html = if (mTripStatus!=null) html.replace("TRIP_STATUS_VALUE", mTripStatus) else html
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

Oli*_*rth 9

编译器知道这mTripStatus不可能是null,如果if条件满足,所以它执行智能施法String?String.这就是允许html.replace("TRIP_STATUS_VALUE", mTripStatus)编译的东西.

但请注意,这不应被解释为编译器警告.这是惯用的Kotlin代码.