简体   繁体   English

从元组参数设置元素参数的默认值

[英]Set up default value of the the parameter of the element from the tuple argument

def found( names(String, String) ): Unit {
  // Do something
}

The parameter list of the function found is a string tuple (string, string) 找到的函数的参数列表是一个字符串元组(string,string)

How to set up the default value of the second tuple element as the first one like names(String, String=names._1) Or does this even allowed? 如何将第二个元组元素的默认值设置为第一个元组元素,如names(String, String=names._1)还是这样呢? if so how can I passing the argument? 如果是这样,我该如何传递论点? found( ("FirstName", ???) ) or found("FirstName") found( ("FirstName", ???) )found("FirstName")

If you have a tuple (String, String) then you have both strings. 如果您有一个元组(String, String)那么您将同时拥有两个字符串。 If you have not both strings then this is not a tuple. 如果您没有两个字符串,那么这不是一个元组。 The whole parameter can have default value, part of a tuple can't. 整个参数可以具有默认值,而部分元组则不能。 If default value uses other parameter then parameters should be in different parameter lists. 如果默认值使用其他参数,则参数应位于不同的参数列表中。

Try 尝试

def found(name1: String)(name2: String = name1): Unit = ???

found("FirstName")()
found("FirstName")("LastName")

def foundTuple(names: (String, String)): Unit = found(names._1)(names._2)

foundTuple(("FirstName", "LastName"))
foundTuple("FirstName", "LastName")

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

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