-
Notifications
You must be signed in to change notification settings - Fork 59
add extension property delegate puzzlers #72
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
Open
tobiberger
wants to merge
2
commits into
angryziber:master
Choose a base branch
from
tobiberger:extension-property-delegate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/delegates/extension/lazyExtension/LazyExtensionProperty.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // by Tobias Berger @tobiberger | ||
| package delegates.extension.lazyExtension | ||
|
|
||
| class MyClass(val foo: String) | ||
|
|
||
| val MyClass.length by lazy { foo.length.toString() } | ||
|
|
||
| println(MyClass("bar").length + MyClass("kotlin").length) | ||
|
|
||
| // What will it print? | ||
| // a) 33 | ||
| // b) 36 | ||
| // b) Lazy value not initialized yet.Lazy value not initialized yet. | ||
| // d) will not compile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| Correct answer: **will not compile** | ||
|
|
||
| * Extension functions and properties are independent of the receiver object (like static Java methods) | ||
| * The delegate is just an object that handles getter/setter calls. | ||
| * For extension properties, this is a single shared delegate istance | ||
| * When defining a delegated property inside a class body, it will be initialized as part of the constructor, giving it access to `this` as well as constructor parameters. | ||
| * When it is defined as an extension property it will be initialized just once while it is loaded. No receiver object is present at that point, so `foo` doesn't exist in that scope. | ||
|
|
||
| The issue may become clearer when looking at how delegates work. | ||
|
|
||
| This setup | ||
| ``` | ||
| val MyClass.length by lazy { foo.length.toString() } | ||
| ``` | ||
| is basically equivalent to this (almost valid syntax) | ||
| ``` | ||
| val delegate = lazy { foo.length.toString() } | ||
| val MyClass.length | ||
| get() = delegate.getValue(this, MyClass::length) | ||
| ``` | ||
| Here you can clearly see that there is no way for the lambda to know what `foo` is. | ||
|
|
||
| The `getValue` implementation of `Lazy` ignores the input parameters. It could theoretically use the reference that is passed here to access a receiver object. But you still need to consider that you only have a single delegate object for all receivers (also see [ThisRefExtensionProperty.kts](../thisRef/ThisRefExtensionProperty.kts)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Correct answer: **33** | ||
|
|
||
| * 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 value in this single instance is only initialized once and then returns the same result for all receivers |
29 changes: 29 additions & 0 deletions
29
src/delegates/extension/thisRef/ThisRefExtensionProperty.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // by Tobias Berger @tobiberger | ||
| package delegates.extension.thisRef | ||
|
|
||
| import kotlin.reflect.KProperty | ||
|
|
||
| class CustomLazy<R: Any, T: Any>(private val initializer: R.() -> T) { | ||
| private lateinit var value: T | ||
| private var initialized = false | ||
|
|
||
| operator fun getValue(thisRef: R, property: KProperty<*>): T { | ||
| if (!initialized) { | ||
| value = initializer(thisRef) | ||
| initialized = true | ||
| } | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| class MyClass(val foo: String) | ||
|
|
||
| val MyClass.length by CustomLazy<MyClass, String> { foo.length.toString() } | ||
|
|
||
| println(MyClass("bar").length + MyClass("kotlin").length) | ||
|
|
||
| // What will it print? | ||
| // a) 33 | ||
| // b) 36 | ||
| // b) UninitializedPropertyAccessException | ||
| // d) will not compile |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The explanation doesn't really point out why. Is it because the signature of
lazytakes a()->T, which makesMyClassinstance not accessible, which is kind of implied in the last point?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.
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