-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Extension properties setters does not work as expected #5588
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
Comments
According to the current spec, So this would require a spec change, and implementing it would be a considerable complication of the compiler. I am not sure it's worth it, honestly. What's the point to have extension method setters? In particular if you want to encourage a functional style? |
Well, my thought was that extension method should be as much "indistinguishable" to regular methods as it ever possible. At least this idea looks quite natural for me. But in general I think that all that:
- all that features of regular member methods could be "expect-able" for extension methods. Regarding this particular case (extension property with setters), one may admit, probably there are not so much use case:
No other use cases I could imagine for now. More interesting could be use case with indirection (highlighted later) But overall, I would say that major motivation for supporting extension properties could be "political". In this case willing to close/fill that gaps in Scala extensions "stuff" implementation may send clearly good signal about Scala bright future (for all who may perform that Scala vs Kotlin comparision). And vise versa - willing to not fill that gabs may send bad signal for all who consider different language alternatives (sometimes people may make their decisions basing on minor things treating them as signs of overall altitude). From this perspective filling even minor gaps may have good effect (especially when that gaps are already filled in competitors implementation). However this is only my personal feelings - it might be not objective (or wrong). |
From the other perspective Kotlin's extensions-related stuff is much close to C++'s Pointers to members
But what is also missing in Kotlin - is something similar to C++'s pointer to fields (not only to methods). This feature (I guess) could be even more useful then regular extensions properties (effectively it should be closer to extensions functions type, but those "extensions assignable functions (properties?) types" should be something like regular extension functions types, also equipped with Although (any way) it is quite separate feature. |
FYI: comparison of Kotlin's extensions related features vs Scala's extensions related features could be summarized in following table:
|
FWIW this works with scala2-style extension methods, through implicit classes or implicit conversions. I'm not sure why the current spec would allow it to work for implicit conversions but not extension methods? |
Fixed by #9552 |
After recently merged extension methods pull request extension properties definition (getters and setters) become available.
Actually it is possible to define extension setter (together with extension getter), but on use-site only extension getter works as expected, attempt to use extension setter leads to compilation error.
Demonstrating examples (extension activation via implicits or via import - same result)
Example 1 (using import)
Example 2 (using implicits)
Expected behaviour - code snippets should be compilable, asserts should be passed successfully
Actual behavior - code compilation fails with error
FYI
Kotlin extension properties works as expected (with both getters and setters):
Same (working) example in Kotlin may look like this:
The text was updated successfully, but these errors were encountered: