1

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()
                  ^
Community
  • 1
  • 1
Saba Ahang
  • 578
  • 9
  • 24

2 Answers2

2

Try this instead

def (rate, time) = getDateAndRate()
currency.rate_one = rate
currency.time_one = time
Dónal
  • 185,044
  • 174
  • 569
  • 824
  • It works! but why on earth it doesn't work like above? So annoying – Saba Ahang Jul 24 '14 at 09:20
  • 1
    if you want to make this code less verbose you could change `getDateAndRate` to accept a `Currency` argument, update it within the method, and return it – Dónal Jul 24 '14 at 09:39
0

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()
}
blackdrag
  • 6,413
  • 2
  • 26
  • 38