New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attach to property #6
Conversation
Part of the proposed functionality can already be accomplished using extension factory functions, e.g.: class Foo {
val delegated by factory()
fun bindToLifecycle(listener: Listener) { ... }
}
fun Foo.factory() = Delegate(this)
class Delegate(foo: Foo) : Listener {
init {
foo.bindToLifecycle(this)
}
operator fun getValue(...)
} Not sure if this is an argument against it. Of course you can't access the Regarding Interface Delegation, this would be immensely useful because currently we have no way whatsoever to communicate between the delegating instance and the delegate instance. |
@cypressious yes, but this doesn't work if you want to delegate to already existing instance, e.g. passed to constructor or otherwise available. You can wrap it with |
I think in most cases, you will never delegate two properties to the same delegate, but maybe I'm wrong. Anyway, I think making this mechanism available to extension functions is the real benefit here because you can delegate to a library class you don't control but can still handle stuff like lifecycle. |
@@ -11,6 +11,7 @@ The proposals themselves are colloquially referred to as KEEPs. | |||
| Coroutines | repo: [kotlin-coroutines](https://github.com/Kotlin/kotlin-coroutines) | [Issues](https://github.com/Kotlin/kotlin-coroutines/issues) | |||
| Type aliases | [type-aliases.md](proposals/type-aliases.md) | [Issue #4](https://github.com/Kotlin/KEEP/issues/4) | |||
| Bound callable references | [bound-callable-references.md](proposals/bound-callable-references.md) | [Issue #5](https://github.com/Kotlin/KEEP/issues/5) | |||
| Attach to property | [attach-to-property.md](proposals/attach-to-property.md) | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd still need an issue for discussion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My impression was that we discuss PRs in PRs, which is also an issue, no?
Can we slightly rethink "delegation by" semantic? Let's say that private val foo$delegate = expression.getDelegate(this, ::foo)
val foo: T
get() = foo$delegate.getValue(this, ::foo) And add to stdlib: operator inline fun <D> D.getDelegate(instance: Any?, property: KProperty<*>): D = this IMHO it slightly more elegant, and allow to avoid mutable state in delegate class. |
@bashor that would eliminate possibility to optimise delegate's storage in the future. We were considering optimising delegate fields when possible, e.g. when delegate is an |
Good point! 👍
|
@bashor yep, I'll add it to alternatives. |
This YouTrack issue should be mentioned somewhere in the proposal: [KT-7925](KT-7925 Notify property delegate if it's attached to an object) Notify property delegate if it's attached to an object |
Can I ask what the expected criteria for inheritance in the implementation of I've got:
this setup results in Is this by design? Should it not be documented in the docs: https://kotlinlang.org/docs/reference/whatsnew11.html#interception-of-delegated-property-binding |
@Groostav A vtable lookup can only happen when some statically known type has a corresponding member declared. In Kotlin all conventions are resolved statically, and your static types don't have I don't think this needs documenting, because this is the way all convention work: if the operator function is not present at compile time, it can't be called at runtime. The only difference here is that the compiler does not complain about the absent operator function, but simply skips the call to |
nor does it complain about an extra operator function --that is, it doesn't complain when you have too few Similarly visibility is important: if the Perhaps then an IDEA "suspicious" check: If IDEA can find a
results in
ps: I love that IDEA is suspicious of my code. |
@orangy Shall we close it now? |
Obsolete |
This proposal enables instances to which properties are delegated using
by
keyword to receive "attach" call so that they can do some useful work before getValue/setValue calls will start happening.