Groovy convert from List to var args for method call

Probably the spread operator, *, is what you’re looking for:

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

Notice that the parentheses on the method call to to are mandatory, as otherwise to *emails would be parsed as a multiplication. Bad choice of overloaded grammar symbols IMO =P

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)