June 2018
Intermediate to advanced
316 pages
6h 34m
English
If you're going to use a delegate object for delegating objects of different types, you should create a separate class for each primitive type. You can create a single delegate class with a generic like this:
class GenericDelegate<T> : ReadOnlyProperty<Any?, T?> { override fun getValue(thisRef: Any?, property: KProperty<*>): T? { TODO() }}
And use it as follows:
class Main { val property by GenericDelegate<Int>()
val another by GenericDelegate<Float>()}
But in this case, the getValue() method of the delegate object will return an instance of the Object type, and a cast to your generic type will occur:
@Nullable public final Integer getProperty() { return (Integer)this.property$delegate.getValue(this, $$delegatedProperties[ ...
Read now
Unlock full access