Providing a delegate - seems overly complicated pseudo constructor

The docs for providing a delegate have a

  1. local convenience function that
  2. wraps a constructor of an intermediate Loader class
  3. that provides a secret provideDelegate operator fun (be careful, it is not an interface or part of the constructor!)
  4. That creates an object
  5. 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

  1. open class CloudSyncDoc: A kind of “bucket” that is the parent object, has a
    • fun <T:Any> cloudSync(defaultValue: T): CloudSyncDelegateProvider<T> = CloudSyncDelegateProvider(defaultValue)
  2. open class CloudSyncDelegateProvider: has 1 function: provideDelegate, that returns a
  3. 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.