[英]How to define scala function with undefined number and type of arguments
我的函数必须将值列表作为参数,将每个值转换为字符串并将它们添加到列表中。 值可以是String
, Int
或Boolean
。
如何灵活定义一个函数以处理不同类型? 这是我的初始函数,但是按预期,当我将int作为参数时,出现类型不匹配错误
def test (s: String*): List[String] = {
s.toList
}
def test(s: Any*): List[String] = s.map(_.toString).toList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.