https://kotlinlang.org logo
Title
n

nyxcode

06/02/2017, 3:57 PM
Hey, can anyone explain to me what the difference is between a getter/setter and a delegate?
a

alexreidy

06/02/2017, 4:32 PM
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

nyxcode

06/02/2017, 4:33 PM
So.. It's just syntactic sugar for a getter/setter?
r

Ruckus

06/02/2017, 4:36 PM
That's a reasonable first order approximation