""" You can refer to a method, rather than invoking it, by using the "ref" keyword before it. Invoking a method is far more popular than referencing/passing a method. So invocation is the syntactically clean case (obj.toString.trim) and method reference the verbose case (ref obj.someMethod). Also, the use of "ref" immediately clues you in to what's going on as you read code left-to-right. See the lines below marked with: # <--- """ class Customer cue init(name as String, totalSpent as decimal) base.init _name = name _totalSpent = totalSpent get name from var as String get totalSpent from var as decimal def toString as String is override # example: Snake Charmers LLC: $5,000.00 return '[.name]: [.totalSpent:C]' class Example def main customers = [ Customer('Yin Yang Inc.', 10_000.00), Customer('Acme Inc.', 100_000.00), Customer('Snake Charmers LLC', 5_000.00), ] print 'Ordered by name:' customers.sort(ref .orderByName) # <--- for cust in customers, print cust print print 'Ordered by total spent:' customers.sort(ref .orderByTotalSpent) # <--- for cust in customers, print cust # you can localize the comparison code with a closure customers.sort(do(a as Customer, b as Customer)) return a.name.toLower.compareTo(b.name.toLower) # or a lambda expression customers.sort(do(a as Customer, b as Customer)=a.name.toLower.compareTo(b.name.toLower)) def orderByName(a as Customer, b as Customer) as int is shared return a.name.toLower.compareTo(b.name.toLower) def orderByTotalSpent(a as Customer, b as Customer) as int is shared return a.totalSpent.compareTo(b.totalSpent) |
How To Pass References To Methods