Would it be a good idea that when a function directly calls another of the same parameter types and order, there is some feature in the language to omit the parameters?
For example if we use ...
to omit parameters, it would be like this:
fun fun1(param1: Type1, param2: Type2): ReturnType {
// implementation
}
fun fun2(param1: Type1, param2: Type2): ReturnType = fun1(...)
This can be quite useful when there is a factory interface that doesn’t know what constructor to call and it has to be an abstract method inside, and there are a lot of classes implementing this interface.
interface FactoryInterface<Data> {
fun create(param1: Type1, param2: Type2): Data
// other funs
}
object SomeDataFactory: FactoryInterface<SomeData> {
override fun create(param1: Type1, param2: Type2): SomeData = SomeData(...)
}
When create
and Data
constructors have a lot of parameters (much more than 2), doing this can reduce a lot of redundant code that can’t be generated automatically by IntelliJ IDEA. Meanwhile, it doesn’t really impact readability.
Of course I can try this workaround:
interface FactoryInterface<Data> {
val creator: (Type1, Type2) -> Data
// other funs
}
object SomeDataFactory: FactoryInterface<SomeData> {
override val creator: (Type1, Type2) -> Data = ::SomeData
}
But using function type val
doesn’t seem to be the commonly accepted practice, and it introduces a little bit of reflection call overhead. It doesn’t seem worth it just for simplicity.