Exerosis
04/09/2022, 3:28 PMinterface Foo
class MyClass : private Foo
Unless I'm missing something it's more memory efficient than dedicating a private/protected field to an instance of Foo to be used internally.louiscad
04/09/2022, 8:56 PMmyObject is Foo
behave when myObject
is of type MyClass
?gildor
04/10/2022, 3:00 PMKlitos Kyriacou
04/11/2022, 9:22 AMmyObject is Foo
would be false, since it's not anyone's concern that MyClass privately inherits Foo. It's slightly more memory efficient (in typical implementations) because it avoids having a reference to a private member object. It's basically the same memory as publicly extending a class, except it does it privately.
In C++, where private inheritance has been there from the beginning, it's very rarely used. One common use is in the Adapter design pattern, where the adaptee is privately inherited while the target is publicly inherited. This is just a minor convenience, as the pattern can easily be implemented without private inheritance.gildor
04/11/2022, 9:31 AMItās slightly more memory efficient (in typical implementations) because it avoids having a reference to a private member objectI doubt that any implementation which Kotlin could provide would be more memory efficient, at least on JVM, it anyway will be just a syntactic sugar on level of JVM and it would work as implicit delegation to private parent class
louiscad
04/11/2022, 10:03 AMis
check always be false, even within the class, or be true only when within the class. Therefore, I think it should not be a feature because of the added confusionExerosis
04/11/2022, 3:35 PMis
check would be true within the class but false outside. I'm not sure I understand why the implementation wouldn't perform identically to the public inheritance.
I can formulate a more reasonable example but I was testing an idea I had and was thinking it would be handy.
Don't mind the rest of the code it's messy and unsafe. https://srcb.in/3vj3x9zJkC
In this case, STreeEvent would only publically extend suspend (SToggled, Boolean, Listener) -> (Unit)
The only way to do that currently is to have an inner class that extends everything else and keep an instance of that in each STreeEvent.gildor
04/11/2022, 4:38 PMThe only way to do that currently is to have an inner class that extends everything else and keep an instance of that in each STreeEventThis how JVM works, you really cannot avoid it with any kind compiler magic, at least I donāt see how itās possible. If keeping 32/64 bit reference + 32/64 bit on minimal object on each instance is too big overhead for you, it really looks that you need some kind specialised data structure for this , not classic objects But I honestly doubt that it would be any kind significant difference which can be measured, especially in such coroutines and lambdas heavy code
gildor
04/11/2022, 4:39 PMI understand why the implementation wouldnāt perform identically to the public inheritance.But how this implementation can be theoretically implemented on jvm?
gildor
04/11/2022, 4:44 PMsuspend (SUpdate<Listener>) -> (Unit)
you just create internal class, as you said, just keep anonymous instance in private/internal property. Overhead will be only 1 additional anonymous class and one additional reference field
So in terms of code it would be almost identical, no additional boilerplate (also arguably more readable). On runtime yes, a bit more, but saving a single object in this code looks as very small overheadExerosis
04/11/2022, 11:30 PMgildor
04/13/2022, 2:30 AMKlitos Kyriacou
04/13/2022, 8:05 AMinterface Foo
class myClass : private Foo
be compiled into the same bytecode as this:
interface Foo
@__kotlin_special_annotation_saying_Foo_is_privately_inherited
class myClass : Foo // public inheritance!
The the JVM can see the inheritance, but the Kotlin compiler (only) knows it's private.gildor
04/13/2022, 9:21 AMmyObject is Foo
would be falseā will not be false anymore, because class actually implements foo
It also means that anyone can cast it to Foo and call those methodsgildor
04/13/2022, 9:23 AMKlitos Kyriacou
04/13/2022, 9:24 AMis
operator would be augemented to check the special annotation as well as inheritance, and thus would return false due to the presence of the annotation. (Java would ignore the annotation and treat the inheritance as public, but there are already existing limitations to the interoperability and this will be just another one.)Klitos Kyriacou
04/13/2022, 9:29 AMinternal
access modifier doesn't exist in the JVM. So internal classes are compiled into bytecode that treats them as public classes, with an annotation. Only the Kotlin compiler knows they're internal (Java can access internal classes as though they were public). The same trick can be applied to private inheritance.gildor
04/13/2022, 9:32 AMBut then in Kotlin the definition of theSo it will be converted from JVM INSTANCEOF instruction to some custom code, which actually checks that this class is Kotlin classoperator would be augementedis
gildor
04/13/2022, 9:33 AMgildor
04/13/2022, 9:35 AM