我正在使用一个提供电子邮件功能的插件,如下所示:
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" }
在代码中,建立了一个地址列表,我正在试图找出如何将该列表转换为该方法所期望的var args。
转换为与“,”连接的字符串不起作用,因为这是无效的电子邮件地址。我需要能够将每个List项分成一个单独的参数,以避免不必迭代列表并分别发送每个电子邮件。
解决方法
可能的传播者*,是你正在寻找的:
def to(String... emails) { emails.each { println "Sending email to: $it"} } def emails = ["t@a.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
请注意,方法调用的括号是强制性的,否则*电子邮件将被解析为乘法。过载语法符号选择不合适IMO = P