galex
11/23/2017, 5:52 AMinline fun <reified T> logd(message: String) {
if (BuildConfig.DEBUG) {
Log.d(T::class.java.simpleName, message)
}
}If used without a specified type, the compiler will complain:
To fix this we need to specify the type:logd("abandonAudioFocus() called") // type inference failed: Not enough information to infer parameter T in (...). Please specify explicitly.
Wouldn’t it be convenient if the compiler would infer the type of the class this code is called in in this case? Maybe with a new inline keyword ?logd<PlayerManager>("abandonAudioFocus() called")
gildor
11/23/2017, 6:05 AMgildor
11/23/2017, 6:06 AMinline fun <reified T> T.logd(message: String) {
if (BuildConfig.DEBUG) {
Log.d(T::class.java.simpleName, message)
}
}
class PlayerManager {
fun doSmth() {
logd("Log!")
}
}
gildor
11/23/2017, 6:07 AMgildor
11/23/2017, 6:09 AMgildor
11/23/2017, 6:11 AMgalex
11/23/2017, 6:15 AMgalex
11/23/2017, 6:15 AMfun <T: Any> T.logd(message: String) {
if (BuildConfig.DEBUG) {
Log.d(this.javaClass.simpleName, message)
}
}
galex
11/23/2017, 6:17 AMgildor
11/23/2017, 6:18 AMgalex
11/23/2017, 6:27 AM