August 2017
Intermediate to advanced
440 pages
10h
English
Let's start with an explanation of what property delegates are. Here is an example of the use of property delegation:
class User(val name: String, val surname: String)
var user: User by UserDelegate() // 1
println(user.name)
user = User("Marcin","Moskala")
Property delegation is similar to class delegation. We delegate to an object using the same keyword (by). Each call to a property (set/get) will be delegated to another object (UserDelegate). This way, we can reuse the same behavior for multiple properties, for example, setting a property value only when some criteria are met, or adding a log entry ...
Read now
Unlock full access