Hey, can anyone explain to me what the difference ...
# announcements
n
Hey, can anyone explain to me what the difference is between a getter/setter and a delegate?
a
nyxcode: You might have a Person with a
money
property which has a getter and setter function. The setter indirectly sets the money using a Wallet instance. (
person.setMoney(99)
calls
person.wallet.setMoney(99)
for example.) Wallet in this case is the delegate -- Person delegates money storage to Wallet. Kotlin has a special notion of a delegate: You can alternatively give Wallet special operator functions
getValue()
and
setValue()
which can make the code a little prettier in some cases:
var money: Double by Wallet()
n
So.. It's just syntactic sugar for a getter/setter?
r
That's a reasonable first order approximation