y
03/23/2023, 5:23 PMinterface MyInterface<R> {
fun doIt(): R
}
object MyImpl : MyInterface<String> {
override fun doIt(): String = "hello"
}
object MyOtherImpl : MyInterface<Int> {
override fun doIt(): Int = 5
}
which works fine.
now say I've got fun <R> foo(impl: MyInterface<R>): R = impl.doIt()
is it possible to add a default value to impl
?
fun <R> foo(impl: MyInterface<R> = MyImpl) = impl.doIt()
doesn't compile.
the best I could come up with is to also add an overload of `foo`:
fun foo() = foo(MyImpl)
.Paul Griffith
03/23/2023, 5:25 PMLoney Chou
03/23/2023, 5:27 PMMyImpl
emits String
, while MyInterface<R>
emits R
, R
could be something other than String
, so you can't assign impl: MyInterface<R>
with MyImpl
.y
03/23/2023, 5:31 PM