kotlin-style-guide icon indicating copy to clipboard operation
kotlin-style-guide copied to clipboard

Platform type for public functions/properties

Open voddan opened this issue 8 years ago • 3 comments

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)
}

voddan avatar Jun 21 '16 13:06 voddan

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.

voddan avatar Jun 21 '16 13:06 voddan

You have a small typo in the title. Apart from that I obviously support this proposal.

cypressious avatar Jun 21 '16 13:06 cypressious

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. =/

kevinmost avatar Nov 10 '16 21:11 kevinmost