https://kotlinlang.org logo
Title
r

ribesg

08/04/2017, 10:12 AM
try {
    myObject.theField
} catch(t: Throwable) {
    log.error("theField is null")
}
h

horse_badorties

08/04/2017, 10:18 AM
can you provide the declaration of your data class please?
data class Foo(val name: String)
val myFoo = Foo(null)
does not compile, there is no way of getting a Foo with a null name - the compiler prevents that.
r

ribesg

08/04/2017, 10:22 AM
There are multiple obvious ways to have null values in non null fields
h

horse_badorties

08/04/2017, 10:23 AM
show me one 🙂
r

ribesg

08/04/2017, 10:23 AM
I guess the most obvious one is reflection
In my specific case it's Moshi
Retrofit with a MoshiConverter
h

horse_badorties

08/04/2017, 10:24 AM
ok, so we are talking about Interop issues
r

ribesg

08/04/2017, 10:25 AM
Most things running on the JVM do not care about nullability
h

horse_badorties

08/04/2017, 10:26 AM
I wasn't aware you were talking about "kotlin-only"
r

ribesg

08/04/2017, 10:27 AM
You can't do much with Kotlin only 😛
h

horse_badorties

08/04/2017, 10:37 AM
ok, I just did a quick test with a simple static Java method that returns a null String reference.
myObject.theField = JavaTester.returnNull()
throws an
IllegalStateException
oops, one moment... 🙂
reflection might work though
r

ribesg

08/04/2017, 10:39 AM
You need either reflection or the Java part stating it doesn't return null using annotations while still returning null
A simple Java method returning a
String
is considered to return a
String?
to a Kotlin caller
h

horse_badorties

08/04/2017, 10:40 AM
exactly
r

ribesg

08/04/2017, 10:41 AM
Now if you put
@NotNull
on your Java method and still return null, Kotlin is lost
h

horse_badorties

08/04/2017, 10:43 AM
which @NotNull exactly? There are many.
r

ribesg

08/04/2017, 10:43 AM
Any of those understood by the Kotlin compiler I guess
h

horse_badorties

08/04/2017, 10:43 AM
I tried org.jetbrains.annotations.NotNull
r

ribesg

08/04/2017, 10:44 AM
Which should be the same as the ones understood by IDEA by default
h

horse_badorties

08/04/2017, 10:44 AM
still IllegalStateException
r

ribesg

08/04/2017, 10:44 AM
The Java part needs to be compiled separately
h

horse_badorties

08/04/2017, 10:44 AM
org.jetbrains.annotations.NotNull is the IDEA default, yeah
r

ribesg

08/04/2017, 10:44 AM
It needs to be a compiled library
h

horse_badorties

08/04/2017, 10:44 AM
ok, I see
r

ribesg

08/04/2017, 10:45 AM
If it's your own file that you compile at the same time, then it knows
Another problem is that you really never know how all the alibraries you use work, they may use reflection and you don't know it
My problem on the Kotlin side is that Kotlin never checks for that null. This object held a null value for days in the RAM, then at some point somebody did an action which passed this value to a method, which doesn't take nulls, and boom. It should have failed earlier. It was very hard to trace
h

horse_badorties

08/04/2017, 10:53 AM
I see. You might want to ask again in the channel and provide a little more background.