Skip to content

Conversation

@tobiberger
Copy link

There is something to consider when using extension properties in combination with delegates.
I added 2 different versions of this puzzler that show the issue in slightly different ways. The custom delegate implementation might be a failed try to fix the problem of the other version.

* Extension functions and properties are independent of the receiver instance (like static Java methods)
* The delegate is an instance that handles getter/setter calls.
* For extension properties, this is a single shared delegate instance
* The extension is initialized independently of any instance, so the delegate can't use any instance properties
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some variation on this info?

During normal usage the initialiser block is scoped to the class body, so it can capture this if necessary.

@@ -0,0 +1,6 @@
Correct answer: **will not compile**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation doesn't really point out why. Is it because the signature of lazy takes a ()->T, which makes MyClass instance not accessible, which is kind of implied in the last point?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, having the instance available in the lambda wouldn't really help - that's actually the point of my second example.
I'll try to rephrase things a bit to make the issue clearer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants