Augusto Megener
12/28/2024, 1:13 PMimplement
statements,
they would serve as an alternative way to create extension members
kt
class Foo {
val bar = "hi"
}
implement Foo {
val baz get() = "$bar, kotlin!" // would work like val Foo .baz get() = "$bar, kotlin!"
}
syntax sugar for interface implementation wrappers for existing classes, when used in classes from other libraries
kt
class ExternalClass {
val foo= "hi"
}
interface MyInterface {
fun getText(): String
}
implement ExternalClass : MyInterface {
override fun getText() = foo
}
// internally creates a wrapper
@JvmInline
value class MyInterfaceExternalClass(val value: ExternalClass) : MyInterface {
override fun getText() = value.foo
}
// when ExternalClass is used in contexts that require MyInterface, internally the wrapper will be used, but in practice, you can use it as if it were actually ExternalClass
fun myFun(arg: MyInterface) {
println(arg.getText())
}
val obj = ExternalClass()
myFun(obj) // prints "hi"
and for separating classes in multiple files, but uniting everything in a single class, when used in project classes
// a.kt
class Class {
[...]
}
// b.kt
implement Class {
[...]
}
// c.kt
implement Class : MyInterface {
override fun getText() =randomValue
[...]
}
in my opinion this would be viable and useful, what do you think?jw
12/28/2024, 1:26 PMmyFun
with the same instance twice, will actually cause two different boxing operations and not support referential equality (which may be required for something like an add/remove listener).Augusto Megener
12/28/2024, 1:37 PMjw
12/28/2024, 1:40 PMjw
12/28/2024, 1:40 PMAugusto Megener
12/28/2024, 1:47 PMjw
12/28/2024, 1:51 PMAugusto Megener
12/28/2024, 2:15 PMMyInterfaceExternalClass
object that will be put as an argument, however, it will be cast to MyInterface
if it were necessary to cast to ExternalClass
, it might just not be possible or it could be cast back to MyInterfaceExternalClass
and get the value
then the value would always be the wrapper, however, it would be used as MyInterface
jw
12/28/2024, 2:21 PMmyFun
twice in your example you get two allocations of wrappers (the inline and value part of the class are useless because it implements an interface)Augusto Megener
12/28/2024, 2:24 PM