"""
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
var _name as String
var _totalSpent as decimal
def init(name as String, totalSpent as decimal)
_name = name
_totalSpent = totalSpent
get name from var
get totalSpent from var
def toString as String is override
# example: Snake Charmers LLC: $5,000.00
return '[.name]: [.totalSpent:C]'
class Example
def main is shared
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
# Yes, there will be lambdas in a future version so
# the comparison can be inlined in the sort() call.
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) |