IfKotlinsupportscompileclass"namespace"in different files, it meansthat the class of "namespace"is apartialclass.Why notextendthis capabilityto otherclassesin the module?For example
// generated/MyClass.kt
partial class MyClass {
val one: Int = 1
}
// hand-written/MyClass.kt
partial class MyClass {
val two: Int = 2
}
fun test() {
val myClass = MyClass()
myClass.one = -1
myClass.two = -2
}
P.S. Trait as an argumentnot taken into account.These are different things.
The "namespace" class is not a class from the language perspectivew, only from the runtime perspective. So, "extend this capability" is a little bit of an exaggeration here :)
Anyway,it would benice to havethis feature.Partialclassesare partialonlybefore and during thecompilation.This is true only forclassesfrom the sourcecodewithin amodule.Atruntime, theyare no longerpartial.
Why use inheritance when you don't need it or want it? I think the point of partial classes if you want to have some generated code (which lives in a different file) be part of a class. This could be useful in either something like a generated model classes that an ORM tool may generate, or like ASP.NET does with code-behind files. It's all about tool integration as I see it, not about writing code manually.
The only somewhat valid justification for Partial Classes existence is code generation, but often that has its own problems. I'd be really against have partial classes in Kotlin. Many times it's nothing but an excuse to violate SRP.
I was just remembering an old/existing bug in Kotlin where code in the namespace will break if you do it in both say src/main/kotlin and src/test/kotlin - as you'll end up with ONLY the src/test/kotlin variation of the class.
If such a “class extension” mechanism was to work we could solve this. However - adding partial classes REALLLLLLY brings in horrible issues when it comes to modularity/dependency management.