Kotlin扩展方法作为长方法名的别名? [英] Kotlin extension method as alias for long method name?

查看:278
本文介绍了Kotlin扩展方法作为长方法名的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kotlin中使用的是Kotlin本机库对象,其中包含其.nameIsMuchTooLongAndIsStillNotClear方法.以类似于typealias的方式,我想为该方法创建一个别名,因此可以将其称为.shortAndClear.为了使事情复杂一点,这些函数有几个参数,其中许多参数具有默认值,我不希望在包装器中对其进行预处理.经过进一步研究后,看来扩展功能就是这种方式去.

I am working in Kotlin using a Kotlin-native library object containing a method whose .nameIsMuchTooLongAndIsStillNotClear. In a manner similar to typealias, I want to create an alias to the method, so I can refer to it as something .shortAndClear. To complicate matters slightly, these functions have several parameters, many of which have defaults that I'd prefer not to pre-process in a wrapper. After further research, it still seems like an extension function is the way to go.

要使用易于测试的 example函数,假设我要为String.startsWith创建一个别名类型扩展,称为String.beg.我可以轻松获得以下解决方案:

To use an example function that's easy to test, let's say I want to create an alias-type extension for String.startsWith that 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)   // works ok

但是,这似乎要求我列出所有参数及其默认值,并在每次重载时都这样做. (所讨论的真实方法签名的时间更长,并且具有更多默认值.)本着不要重复自己"的精神,有没有办法我可以使用

However, this seems to require that I list all argument and their defaults, and do so for every overload. (The real method signatures in question are considerably longer with many more defaults.) In the spirit of "don't repeat yourself", is there a way I can use a function reference to String::startsWith so that I don't have to enumerate all arguments? I've tried several forms, but none of them work:

// none of these work:
fun String.beg = String::startsWith
fun String.beg = this::startsWith
val String.beg: (CharSequence, Boolean) -> Boolean = String::startsWith

推荐答案

当前没有办法来完全实现您要执行的操作.如果要保留默认参数,则必须执行操作(如您所说):

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

在这种情况下,您不能指定参数的默认值,因为beg是lambda.

In this case, you can't specify the default value of a parameter because beg is a lambda.

自Kotlin 1.2(当前为Beta)以来,您可以避免在函数引用上指定this.上面写过的相同示例,但在Kotlin 1.2中:

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

这篇关于Kotlin扩展方法作为长方法名的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆