Hi :wave:, maybe a silly question related to Java/...
# announcements
o
Hi 👋, maybe a silly question related to Java/Kotlin interop, I face something that worries me 😱 In Java, a legacy class exposes
Copy code
public void setFoo(@NonNull Bar bar)
{
  // ...
}
In Kotlin side
Copy code
myObject.foo = null // OK
myObject.setFoo(null) // KO
The property access compiles 😱 The function call doesn't compile 👍 Is it expected? Can I avoid exposing
setFoo
as a Kotlin property at will? It seems fragile to allow such property access, no?
😱 2
👀 4
r
Seems like a bug. Is the getter also @NonNull? https://youtrack.jetbrains.com/issue/KT-33393 is close
👌 1
o
Indeed, the getter isn't
@NonNul
but
@Nullable
, so I guess it brings such conflict… My use case might be a bit weird, I have get/set and clear for this API. In fact, what I'd like here is to disabled property feature of Java/Kotlin interop for this specific APIs. Maybe my use case can't be achieved?
Oh, I double checked and in fact,
getFoo
isn't annotated at all 😱
Copy code
public class Foo {
//    @Nullable
    public String getFoo() { return ""; }

    public void setFoo(@NonNull String bar) { }

    public void clearFoo() { }
}
If getFoo isn't
@Nullable
then
Copy code
fun x() {
    val foo = Foo()
    foo.setFoo(null)
    foo.foo = ""
    foo.foo = null
    foo.clearFoo()
}
both
foo.foo
assignments will compile, if it's
@Nullable
, then both assignments won't compile
Val cannot be reassigned
At the light of your hint, everything becomes obvious now. I wasn't looking at the whole problem, only focusing on
setFoo