以Vararg为第一个参数的Kotlin方法 [英] Kotlin methods with Vararg as First Parameter

查看:1032
本文介绍了以Vararg为第一个参数的Kotlin方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我查看了以下问题/解答,以解决该问题,但没有任何运气. 从Kotlin调用Java Varargs方法-这最后有一个varargs参数参数列表,但我的问题是在参数列表的开头处理varargs. Kotlin:将列表转换为Java Varargs -相同.其他搜索产生相同的结果.这些是我能找到的最接近的东西.

Note I've looked at the following questions/answers to solve the problem without any luck. Call Java Varargs Method from Kotlin - this one has the varargs parmeter at the end of the parameter list, but my question deals with varargs at the start of the parameters list. Kotlin: Convert List to Java Varargs - the same. Other searches yield the same thing. These were the closest I could find.

我正在用单个字符定界符调用Kotlin String.split方法. 这是一个vararg方法,其中vararg参数是多个参数中的第一个.该方法的定义如下:

I am calling the Kotlin String.split method with a single character delimiter. This is a vararg method where the vararg parameter is first of multiple parameters. The method is defined like so:

public fun CharSequence.split(vararg delimiters: Char, 
                              ignoreCase: Boolean = false,
                              limit: Int = 0): List<String>

当我按如下所示调用方法时,它可以正常编译:

When I call the method as below, it compiles fine:

fun String.splitRuleSymbol() : String = this.split(':') //ok

但是当我尝试添加ignoreCaselimit参数时,出现问题:

But when I try to add the ignoreCase and limit parameters, I get a problem:

fun String.splitRuleSymbol() : String = this.split(':', true, 2) //compiler error

我得到的错误是...

The error I get is...

使用提供的参数不能调用以下函数:

None of the following functions can be called with the arguments supplied:

公共乐趣CharSequence.split(变量分隔符:字符串,ignoreCase:布尔值= ...,限制:Int = ...):在kotlin.text中定义的列表

public fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = ..., limit: Int = ...): List defined in kotlin.text

公共乐趣CharSequence.split(变量分隔符:Char,ignoreCase:布尔值= ...,限制:Int = ...):在kotlin.text中定义的列表

public fun CharSequence.split(vararg delimiters: Char, ignoreCase: Boolean = ..., limit: Int = ...): List defined in kotlin.text

对我来说,在参数后加上vararg个参数有些奇怪,但这并不重要.如果我按以下方式调用它,则效果很好:

To me, having a vararg parameter followed by other parameters is somewhat odd, but that's beside the point. If I call it as below, it works fine:

 // both of the following compile
 fun String.splitRuleSymbol() : String = 
           this.split(delimiters = ':', ignoreCase = true, limit = 2)
 fun String.splitRuleSymbol2() : String = 
           this.split(';', ignoreCase = true, limit = 2)

是否有一种方法可以传递vararg Char到此方法中,而不必用参数名称ignoreCaselimit限定我的其他两个参数?其余参数不是Char?

Is there a way to pass a vararg Char in to this method without having to qualify my other two parameters with parameter names ignoreCase and limit? Can the compiler not tell that the remaining parameters are not Char?

我尝试了传播算子和一些以下其他方式,均无效:

I have tried the spread operator and a few other ways below , none of which work:

    //compiler errors on all these
    this.split(*':', true, 2) //using the "spread" operator
    this.split(*charArrayOf(':'), true, 2)
    this.split(*mutableListOf(':'), true, 2)
    this.split(*Array<Char>(1) { ':' }, true, 2)

是的,我知道其中一些看起来很荒谬.但是,有没有办法避免冗长的选择呢?

Yes, some of these look ridiculous, I know. But, is there no way to avoid the verbose alternative?

PS 在我提出问题时,我发现了另一个已编译的表达式.

PS As I was formulating my question, I found another expression that compiled.

    this.split(':', limit = 2)

这不太冗长,并且由于我不需要更改默认的ignoreCase参数,因此它更接近我要查找的内容.

This is less verbose and since I don't need to change the default ignoreCase parameter, it's closer to what I am looking for.

推荐答案

您的观察是正确的.在vararg参数后面的参数只能使用命名参数传递,否则会遇到歧义问题(举个简单的例子,假设所有参数的类型都是Any).

Your observations are correct. Arguments that are after a vararg parameter can only ever be passed in by using named arguments, otherwise you'd run into ambiguity issues (for a trivial example, let's say when all arguments are of type Any).

我现在可以为此找到的最佳资源是.

The best source I can find for this right now is this book.

vararg参数通常是最后一个参数,但并非总是必须如此.如果vararg之后还有其他参数,则必须使用命名参数传递参数

The vararg parameter is usually the last parameter, but it does not always have to be. If there are other parameters after vararg, then arguments must be passed in using named parameters

@Les找到了一个很好的消息来源,请参见他们的答案.

@Les found a good source on it, see their answer.

这篇关于以Vararg为第一个参数的Kotlin方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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