Hi all,
Sometimes we do not want evaluate some expression eagerly(Maybe
evaluation is expensive), e.g. a table contains a field `records` whose
content is queried from DB, but we do not get the content of `records`
eagerly util we access `records`.
class SomeTable {
def records = ( doQueryFromDB() ) // the expression in the
parentheses will be evaluated lazily
def sum() {
// access the methods or fields of `records`, trigger
evaluation, i.e. the query of DB
}
def getRecords() {
return records // do NOT trigger evaluation
}
}
Lazy evaluation syntax is very simple( just supports 1) and 2) ):
1) def v = ( someExpression() ) // Lazy evaluation for variable declaration
2) v = ( someExpression ) // Lazy evaluation for assignment
3) println(( someExpression )) // NOT lazy evaluation
Any thoughts?
Cheers,
Daniel.Sun
--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html