gpt4 book ai didi

list - Groovy 从 List 转换为 var args 以进行方法调用

转载 作者:行者123 更新时间:2023-12-03 10:49:53 24 4
gpt4 key购买 nike

我正在使用一个提供电子邮件功能的插件,如下所示:

class SendSesMail {

//to
void to(String ... _to) {
this.to?.addAll(_to)
log.debug "Setting 'to' addresses to ${this.to}"
}

}

文档说明该类的调用方式如下:
sesMail {
from "from@a.com"
replyTo "reply@a.com"
to "t@a.com", "t@b.com", "t@c.com"
subject "Subject"
html "Body HTML"
}

在代码中 List的地址已建立,我正在尝试弄清楚如何将此列表转换为该方法预期的 var args。

转换为 String与“,”连接不起作用,因为这是一个无效的电子邮件地址。我需要能够将每个 List 项分成一个单独的参数,以避免遍历 List 并单独发送每封电子邮件。

最佳答案

可能是价差运算符,* , 就是你要找的:

def to(String... emails) {
emails.each { println "Sending email to: $it"}
}

def emails = ["t@a.com", "t@b.com", "t@c.com"]
to(*emails)
// Output:
// Sending email to: t@a.com
// Sending email to: t@b.com
// Sending email to: t@c.com

注意方法调用上的括号 to是强制性的,否则 to *emails将被解析为乘法。重载语法符号的错误选择 IMO =P

关于list - Groovy 从 List 转换为 var args 以进行方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14453449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com