Slackbot
08/06/2026, 6:53 AMephemient
08/06/2026, 7:06 AMYoussef Shoaib [MOD]
08/06/2026, 7:21 AMby delegation:
interface MyInterface {
val number: Int
class Impl: MyInterface { override val number: Int by MyDelegate() }
}
class Foo: MyInterface by MyInterface.Impl()
Or if you really wanna force `MyDelegate`:
interface MyInterface {
/** This property should always return the same value */
val myDelegate: MyDelegate
}
val MyInterface.number: Int by ReadOnlyProperty<MyInterface, Int> { thisValue, property -> thisValue.myDelegate(thisValue, property) }ephemient
08/06/2026, 8:34 AMImpl implements all interface methods (and class MyImplementation : MyInterface by MyInterface.Impl() could work) or Impl is abstract (in which case implementations would use class MyImplementation : MyInterface.Impl() and not be allowed other superclasses). depends on your use case but both could have issues.ephemient
08/06/2026, 8:35 AMinterface MyInterface {
/** This property should always return the same value */
val myDelegate: MyDelegate
val number: Int
get() = myDelegate.getValue(this, MyInterface::number)
}
but I wouldn't like to have it prescribed that way eitherYoussef Shoaib [MOD]
08/06/2026, 8:37 AMinterface MyInterface: MyInterface.Fields {
val someProp: String
interface Fields {
val number: Int
class Impl: Fields { override val number: Int by MyDelegate() }
}
}
class Foo: MyInterface, MyInterface.Fields by MyInterface.Fields.Impl() {
override val someProp = "Hello World"
}ephemient
08/06/2026, 8:42 AMMyDelegate doesn't need anything else from this. (Scala has a cake pattern trait Impl { this: MyInterface => } so that such a mixin could make use of other outer methods but it leads to other complexity)ephemient
08/06/2026, 8:43 AMephemient
08/06/2026, 8:45 AM