Hullaballoonatic
06/27/2019, 2:21 AMinterface Foo {
val foo: Int
val bar: String?
companion object {
operator fun invoke(foo: Int, bar: String? = null) = object : Foo {
override val foo = foo
override val bar = bar ?: otherLogic...
// etc
}
}
}
In a lot of ways it's like having the ability to inherit from multiple classes. That's probably not a good freedom to have, though.
invoke operator has a few advantages over constructor, too, such as error checking the arguments prior to instantiating the object.
This also opens up delegation to cut down on code
You'd think it'd be unfortunate you couldn't do this:
class Foozle(foo: Int, bar: String?) : Foo(foo, bar)
But you can instead just do this:
class Foozle(foo: Int, bar: String?) : Foo by Foo(foo, bar)
I'd love to hear the drawbacks to this overall approachAdam Powell
06/27/2019, 4:10 AMfun Foo(...): Foo = object : Foo { ...}Hullaballoonatic
06/27/2019, 6:38 AMribesg
06/27/2019, 7:49 AMMike
06/27/2019, 1:14 PMribesg
06/27/2019, 1:15 PMMike
06/27/2019, 1:17 PMribesg
06/27/2019, 1:19 PMMike
06/27/2019, 1:22 PM