Saba Ahang
Saba Ahang

Reputation: 598

Why can't I directly assign to object properties using multiple return values from a method?

Based on this question I've coded the following which throws a compilation time error:

Here is the code:

43. Currency currency = new Currency()
44. (currency.rate_one, currency.time_one) = getDateAndRate()

My method with two return values:

def getDateAndRate(){
    Date date = new Date()
    double rate = getRate();
    return [rate, date]
}

Error thrown

expecting '}', found ',' @ line 44, column 26.
(currency.rate_one, currency.time_one) = getDateAndRate()
                  ^

Upvotes: 1

Views: 99

Answers (2)

blackdrag
blackdrag

Reputation: 6508

There is a trick I learned only recently myself and that is to combine multiple assignment and with:

with (currency) {
    (rate_one, time_one) = getDateAndTime()
}

Upvotes: 0

Dónal
Dónal

Reputation: 187399

Try this instead

def (rate, time) = getDateAndRate()
currency.rate_one = rate
currency.time_one = time

Upvotes: 2

Related Questions