I work in Kotlin, using Kotlin-native library object that contains the method .nameIsMuchTooLongAndIsStillNotClear
. Similarly typealias
, I want to create a method alias, so I can refer to it as .shortAndClear
. To complicate the situation, these functions have several parameters, many of which have default values, which I would prefer not to pre-process in the wrapper. After further research, it still seems that the way is the way to go.
To use an example function that is easy to verify, let's say I want to create an alias extension for String.startsWith
what is called String.beg
. I can easily get the following solution to work:
inline fun String.beg(prefix: CharSequence, ignoreCase: Boolean = false) = startsWith(prefix, ignoreCase)
However, it seems that this requires me to specify all the arguments and their default values, and do this for each overload. (The signatures of the actual methods in the question are significantly longer with many other defaults.) In the spirit of “don't repeat yourself,” is there a way I can use with, so I don’t have to list all the arguments? I tried several forms, but none of them work:
fun String.beg = String::startsWith
fun String.beg = this::startsWith
val String.beg: (CharSequence, Boolean) -> Boolean = String::startsWith
source
share