Kotlin extension method as alias for long method name?
Currently there's no way to fully achieve what you are trying to do. If you want to keep your default parameters, you have to do (as you said):
fun String.beg(prefix: CharSequence, ignoreCase: Boolean = false) = startsWith(prefix, ignoreCase)
// Or if you know that ignoreCase will be always false, you can pass the value directly to "startsWith()
fun String.beg(prefix: CharSequence) = startsWith(prefix, false)
Instead, if you haven't default parameters or you don't care if you have to pass the default value when you will invoke the function, you can use a function reference.
val String.beg: (CharSequence, Boolean) -> Boolean get() = this::startsWith
// If the parameters can be inferred, you can avoid the type specification.
// In this case it won't compile because there are several combinations for "startsWith()".
val String.beg get() = this::startsWith
In this case, you can't specify the default value of a parameter because beg
is a lambda.
Since Kotlin 1.2 (currently in beta), you can avoid to specify this
on a function reference. Same examples written above but in Kotlin 1.2:
val String.beg: (CharSequence, Boolean) -> Boolean get() = ::startsWith
// If the parameters can be inferred, you can avoid the type specification.
// In this case it won't compile because there are several combinations for "startsWith()".
val String.beg get() = ::startsWith
You can also use an import alias, for example:
import kotlin.text.startsWith as beg
fun main() {
"foo".beg("fo")
"bar".beg('B', true)
}