Hi again, can anybody help me with kotlin multipla...
# multiplatform
m
Hi again, can anybody help me with kotlin multiplatform and k2 problem? I am encountering the following error:
Copy code
The following declaration is incompatible because return type is different:
    val name: String?
In module A, I have an interface with:
Copy code
abstract val name: String?
In module B, there is a child class with
Copy code
override val name: String = "SampleDestination"
Additionally, both modules contain actual classes with empty expect classes. This setup prevents successful compilation on both iOS and Android platforms. This is a simple example, which is easily avoidable. However, there are more complicated cases that fail with the same error. This code works on k1. It also works if I keep all of it in one multiplatform module which is why I believe it to be a bug.
s
You tighten the type of
name
(from
String?
to
String
) in the base class B, which shouldn't be allowed. I think K2 is correct
Eg if you pass an instance of B around that is declared as of type A, and you assign
null
to
name
you'd have a problem: type A allows it but type B can't handle it.
m
That doesn't seem right, since it works if it is in the same module, or does it?
And as I have written, it is just the simplest reproducible. I still need to change the type from A to B, where B inherits from A. Same error and pretty sure a valid case.
s
Nothing to do with modules. It's about subtyping, assignability.
a.name is nullable. The compiler thinks it's alright to assign null to it. However, you create an instance of B, where b.name is not nullable. If you do val a: A = B() and then a.name = null, you'll have a problem
m
I think your counter comments are only valid if the code was mutable, but it's all immutable. And returning to my comment, we can forget about (non)optional. Same error comes with immutable non optional A, B classes where B inherits from A.
"Nothing to do with modules". Well, it does. Since it works in one module, but breaks in multiple.
s
Ah, yes, you're right... It's val, not var.
Sorry... Then it's indeed an issue, that K2 can't figure this out when it crosses a module boundary.
m
No worries, thanks for confirming it to be an issue. I have copy pasted it to youtrack with sample project https://youtrack.jetbrains.com/issue/KT-68980/KMP-2-incompatible-because-return-type-is-different