kotlin-style-guide
kotlin-style-guide copied to clipboard
Platform type for public functions/properties
A public function/method returning an expression of a platform type must declare its kotlin type explicitly:
fun apiCall(): String = MyJavaApi.getProperty("name")
Any property (package-level or class-level) initialised with an expression of a platform type must declare its kotlin type explicitly:
class Person {
val name: String = MyJavaApi.getProperty("name")
}
A local value initialised with an expression of a platform type may or may not have a type declaration:
fun main(args: Array<String>) {
val name = MyJavaApi.getProperty("name")
println(name)
}
This is a follow up for KT-12310 Add inspection for methods with platform return type
I honestly don't know why having a kotlin method with type String!
is allowed at all, but this proposal should level the inconvenience.
You have a small typo in the title. Apart from that I obviously support this proposal.
A local value initialised with an expression of a platform type may or may not have a type declaration:
Dunno that I like this. Just because the scope is smaller doesn't mean that the possibility of NPEs goes away. I personally think that platform types being treated as nullable without null-safe calls is the single biggest design mistake in the Kotlin language, so maybe my opinion here is overly strong, but I can't see why we would compromise null-safety for the sake of saving a few characters. =/