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
[FEATURE] Inversion of control for @ExtensionMethod #3483
Comments
A possible problem of the 'static import only' approach is a change of the semantics of the import: by simply adding the import you propose some there's also a usage implied, not only a name resolution. Setting philosophical aspects aside, the practical implication is that you'll run into trouble with all sorts of auto-format-tools / organize import functions: they will not detect any usage of the said static method and probably remove the import, unless they know about such a lombok feature. |
A lot of IDEs already support the concept of extension methods for other languages. You might be able to hook into that. For instance, IntelliJ IDEA supports extension methods for Kotlin. It even colors the methods differently to signal to the user that this is an extension method, to avoid confusion. The Lombok plugin would probably be able to mark the method as an extension method in a similar way, with similar syntax highlighting. |
Feature
Describe the feature
Right now
@ExtensionMethod
is defined where the method is used, like so:Which results in:
Here the extension method is used inside the
ExtensionMethodExample
class.In a lot of other languages like Kotlin, The fact that a method is to be used as extension method is specified where the method is defined, rather than where it's used, Like so:
Here the
ArrayDeque<T>.
specifies that this is an extension method for theArrayDeque<T>
class, withthis
referring to that receiver object.Obviously this can't be done this way in java, but Lombok has already solved that part by making the first parameter specify the "receiver" object.
Right now there is no way of knowing that this method is an extension method though, which makes it necessary to mark the class where it is used with the
@ExtensionMethod
annotation.To be able to make that unnecessary and to specify that a method is meant to be used as an extension method, it would be nice if the method could be marked explicitly as an extension method with the
@ExtensionMethod
annotation like so:or with a new
@Receiver
annotation like so:Describe the target audience
This would benefit people who come from programming languages where extension methods are specified where the method is defined.
Additional context
In the case that the usage of the method is in a different file than the definition of that method, a simple static import of the method should be enough for the preprocessor to find and rewrite all usages of the extension method, similar to how an import of the extension method is enough in Kotlin.
It should also still be possible to use that extension method as a static method. It could be made so that this is restricted to only allow usage as extension method, with an annotation parameter like
@ExtensionMethod(allowStaticCalls = true/false)
or@Receiver(strict = true/false)
. It's up for debate if it should allow static calls by default or not, if the parameter isn't specified. Since the current implementation of extension methods does allow static calls to the method, I'm of the opinion to make this the default too for this extension method implementation, but I'm open for arguments against that reasoning.If it was decided to use the
@ExtensionMethod
for this implementation, I think that it would be more clear if the original use would be renamed to@UseExtensionMethods
, since using the same annotation for both usecases could be confusing for the user.The text was updated successfully, but these errors were encountered: