Ben Woodworth
03/29/2021, 5:29 PMinterface SomeTrait { ... }
private class SomeTraitImpl(...) : SomeTrait { ... }
fun SomeTrait(...): SomeTrait = SomeTraitImpl(...)
And used like this:
class MyClass : SomeTrait by SomeTrait(...)
// Or just used to get an instance of the default implementation:
val trait = SomeTrait(...)
What if a default implementation could be given, something like this:
interface SomeTrait {
...
default class(...) { // Optional name, like companion objects. Implicitly implements SomeTrait.
...
}
}
// No need for a pseudo-constructor function:
val trait = SomeTrait(...) // gets an instance of the default class
Maybe treat "extending" this interface as delegation to the default implementation:
class MyClass : SomeTrait(...)
// Desugars to:
class MyClass : SomeTrait by SomeTrait(...)
It'd probably just be unnecessary added complexity to the language, but it seems like it has potential to simplify some things, it avoids the whole *Impl
naming controversy, and it's definitely fun to consider :)Hanno
03/30/2021, 6:17 AMBen Woodworth
03/30/2021, 12:51 PMBen Woodworth
03/30/2021, 12:58 PMfun SomeTrait(...): SomeTrait = object : SomeTrait {
...
}
alexsullivan114
03/30/2021, 2:32 PMHanno
03/30/2021, 7:49 PMBen Woodworth
03/30/2021, 8:43 PMHullaballoonatic
03/30/2021, 10:28 PMmcpiroman
04/03/2021, 12:48 PMclass Base1; class Base2;
and then class Derived : Base1(), Base2 by Base2()
And then maybe class Derived : Base1(), Base2()
as syntax sugar for above?mcpiroman
04/05/2021, 8:01 AMHullaballoonatic
04/07/2021, 11:24 PM