简体   繁体   English

Kotlin:如何使用第一个参数的默认值进行函数调用并为第二个参数传递一个值?

[英]Kotlin: how to make a function call using the first argument's default value and passing a value for the second?

I'd like to know how to call a function with default arguments when you want to specify the value of the second argument. 我想知道如果要指定第二个参数的值,如何使用默认参数调用函数。 In the simple example below I am showing that the addTwo() takes two arguments. 在下面的简单示例中,我展示了addTwo()接受两个参数。 The 'first' argument has a default value but the 'second does not. 'first'参数有一个默认值,但'second'没有。 How would I call this function specifying that I want to use the default value for 'first' with the given value of 2 for 'second'? 如何调用此函数指定我要使用'first'的默认值,给定值为2的'second'?

Calling addTwo(2) throws an error. 调用addTwo(2)会抛出错误。

fun main(args: Array<String>) {
    var sum = addTwo(1,2)    // works fine
    var nextSum = addTwo(2)  // ERROR: No value passed for parameter second 
}

fun addTwo(first: Int = 0, second: Int): Int {
    return first + second
}

This is an error because Kotlin does not know why you omitted a second parameter. 这是一个错误,因为Kotlin不知道你为什么省略了第二个参数。 The first can be defaulted, but not second . first可以是默认的,但不是second Therefore when you only passed in one argument it was for parameter first . 因此,当您只传入一个参数时,它first是参数。 The error message says exactly what the problem is: 错误消息确切地说明了问题所在:

no value passed for parameter second 没有为参数second传递值

You must call using named arguments for anything after the first defaulted parameter that you want to leave blank. 在第一个要保留为空的默认参数之后,必须使用命名参数调用任何内容。 For your case, this would be: 对于您的情况,这将是:

addTwo(second = 2) // first defaulted to 0

Had your defaulted parameters been in the other order, your call would be fine. 如果你的默认参数是在另一个顺序,你的电话会很好。

fun addTwo(first: Int, second: Int = 0): Int{
    return first + second
}

addTow(2)  // no error, second defaulted to 0

Had your data types been different for the two parameters, you would have seen a different less clear error letting you know the type of the first parameter ( Int ) was expected, not the type of the second ( Date in the following example): 如果两个参数的数据类型不同,您会看到一个不同的不太清楚的错误,让您知道第一个参数( Int )的类型,而不是第二个的类型(以下示例中的Date ):

fun something(first: Int = 0, second: Date): Date { ... }

something(Date()) // ERROR: Type mismatch: inferred type is java.util.Date but kotlin.Int was expected

Note: You can run into the same issue with vararg : 注意: 您可能会遇到与vararg相同的问题:

Only one parameter may be marked as vararg. 只有一个参数可以标记为vararg。 If a vararg parameter is not the last one in the list, values for the following parameters can be passed using the named argument syntax 如果vararg参数不是列表中的最后一个参数,则可以使用命名参数语法传递以下参数的值

Additional note: because of what you experienced, it is generally a best practice to make default arguments the last parameters in the list. 附加说明:由于您的经验,通常最好将默认参数作为列表中的最后一个参数。 Some languages even force this, such as Python (except that Python allows forced named arguments after defaults, since forcong you to call them by name is how you'd get around having parameters after default arguments anyway). 有些语言甚至会强制使用它,例如Python(除了Python允许在默认情况下强制命名参数之外,因为你要通过名称调用它们是你在默认参数之后得到参数的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM