poohbar
08/30/2017, 7:24 PMvar res = someJavaFuncThatActuallyReturnsNull()
// res is now SomeJavaType!
// at this point I can start passing 'res' around and later access anything on it and get an NPE, Kotlin didnt help at all
supaham
08/30/2017, 7:26 PMpoohbar
08/30/2017, 7:26 PMsupaham
08/30/2017, 7:27 PMagomez
08/30/2017, 7:28 PMsupaham
08/30/2017, 7:28 PMpoohbar
08/30/2017, 7:28 PMsupaham
08/30/2017, 7:29 PMagomez
08/30/2017, 7:29 PMsupaham
08/30/2017, 7:29 PMvar res: ReturnType = someJavaFuncThatActuallyReturnsNull()
Would error if it returns null thereRuckus
08/30/2017, 7:30 PMpoohbar
08/30/2017, 7:30 PMsupaham
08/30/2017, 7:31 PMpoohbar
08/30/2017, 7:31 PMsupaham
08/30/2017, 7:31 PM!!
every time you make a java callpoohbar
08/30/2017, 7:32 PMsupaham
08/30/2017, 7:33 PMartem_zin
08/30/2017, 7:33 PMpoohbar
08/30/2017, 7:36 PMagomez
08/30/2017, 7:37 PMartem_zin
08/30/2017, 7:40 PMpoohbar
08/30/2017, 7:42 PMartem_zin
08/30/2017, 7:43 PMpoohbar
08/30/2017, 7:44 PMpublic class MyClass {
public Date getToday() {
return null;
}
}
test.kt
fun main(args: Array<String>) {
val mc = MyClass()
val today = mc.today
today.day.print() // BOOM NPE!!!
}
There are no warnings, no IDE suggestions, nothing. I am saying that the val today
should not be allowed without explicit type. That's all.artem_zin
08/30/2017, 7:45 PMpoohbar
08/30/2017, 7:46 PMjanvladimirmostert
08/30/2017, 7:47 PMagomez
08/30/2017, 7:47 PMpoohbar
08/30/2017, 7:47 PMjanvladimirmostert
08/30/2017, 7:48 PMagomez
08/30/2017, 7:48 PMpoohbar
08/30/2017, 7:49 PMjanvladimirmostert
08/30/2017, 7:50 PMagomez
08/30/2017, 7:50 PMjanvladimirmostert
08/30/2017, 7:51 PMpoohbar
08/30/2017, 7:53 PMagomez
08/30/2017, 7:54 PMjanvladimirmostert
08/30/2017, 7:55 PMpoohbar
08/30/2017, 7:56 PMagomez
08/30/2017, 7:59 PMjanvladimirmostert
08/30/2017, 7:59 PMagomez
08/30/2017, 7:59 PMjanvladimirmostert
08/30/2017, 8:01 PMilya.gorbunov
08/30/2017, 8:20 PMval mc = MyClass()
mc.today.day.print()
codeslubber
08/31/2017, 12:46 AMpoohbar
08/31/2017, 1:03 PMjanvladimirmostert
08/31/2017, 1:33 PMfun String.indent(): String = StringUtils.indent(this) ?: ""
or alternatively:
fun indent(input: String): String = StringUtils.indent(input) ?: ""
poohbar
08/31/2017, 1:35 PMjanvladimirmostert
08/31/2017, 1:40 PMarekolek
09/05/2017, 3:25 PMclass ExampleActivity : Activity {
lateinit var array: IntArray
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
array = intent.getIntArrayExtra("array")
// throws IllegalStateException: getIntArrayExtra("array") must not be null
}
}
dragas
09/05/2017, 7:08 PMIntent.getExtra
states that result MAY be null, if it's not there, thus you should just know your API.arekolek
09/06/2017, 7:05 AMIn kotlin based code, on the other hand, the warning is there.What do you mean by that? Do you mean assigning nullable type to non-nullable? That’s an error, not a warning.
There’s no distinction between non-nullable and nullable properties in java.But Kotlin treats non-annotated Java types as “platform types” and:
When a platform value is assigned to a Kotlin variable, we can choose the type that we expect. If we choose a non-null type, the compiler will emit an assertion upon assignment. This prevents Kotlin’s non-null variables from holding nulls.What I’m saying would be nice, is that if there was a warning (not an error) when assigning platform to non-null. Or at least some Lint check for that. That way, you could for example use the
!!
operator to mark that place as safe, or treat it as nullable instead, depending on the API you are using.