The docs for providing a delegate have a
- local convenience function that
- wraps a constructor of an intermediate Loader class
- that provides a secret provideDelegate operator fun (be careful, it is not an interface or part of the constructor!)
- That creates an object
- which doesn’t really need to implement ReadOnlyProperty, just have the right methods.
This took awhile for my brain to untangle, and I’m guessing might scare people off of using custom Delegated Properties - which is a shame, they are great!
provideDelegate couldn’t somehow be stuffed into a normal constructor?
@benjaminhill You can do so. Just make provideDelegate
an inline function and the contents of provideDelegate
will be inlined into the constructor. I’ll give you that provideDelegate
is a quite esotheric part of Kotlin, but it is very powerful when you need it. Normally you don’t override this one, you just provide a delegate property instead.
I half-understood. (I’m new to inline functions and got VERY lost when the docs started talking about Reified, hopefully I can scrape by!)
operator fun provideDelegate
needs to live somewhere. I could add “inline” to it. But that fights with “open”, so child-classes can’t enhance it. (like I’d extend a constructor in a subclass)
Right now I’ve got
open class CloudSyncDoc
: A kind of “bucket” that is the parent object, has a
fun <T:Any> cloudSync(defaultValue: T): CloudSyncDelegateProvider<T> = CloudSyncDelegateProvider(defaultValue)
open class CloudSyncDelegateProvider
: has 1 function: provideDelegate
, that returns a
open class CloudSyncDelegate<T : Any> : ReadWriteProperty<CloudSyncDoc, T>
which allows me to have
class MyHappySyncClass:CloudSyncDoc {
var motor = cloudSync(0.1)
}
which is pretty ok! And I can extend all 3 CloudSync* classes.
But the class CloudSyncDelegateProvider
with just one function smells funny.