<div dir="ltr">This subject has been addressed publicly by the language team at various points. These are just a few I found, I'm sure there is more elaboration out there. <br><br>Implementation choices aside, the API of "add a static method and mark it as an "extension method", call like instance method" seems to have been rejected.<br><br>> C# extension methods have some very serious drawbacks compared to Java's default methods (for example, poor reflective discoverability, poor discoverability through documentation, not overrideable, require ad-hoc conflict-management rules)<br><br><a href="https://stackoverflow.com/questions/29466427/what-was-the-design-consideration-of-not-allowing-use-site-injection-of-extensio">https://stackoverflow.com/questions/29466427/what-was-the-design-consideration-of-not-allowing-use-site-injection-of-extensio</a><br><br>> What I believe you are hoping for is the ability to "monkey-patch" a method into a class you do not control, but Java does not give you that (by design; it was considered and rejected.)<br><br><a href="https://stackoverflow.com/questions/24096421/java-8-add-extension-default-method-to-class">https://stackoverflow.com/questions/24096421/java-8-add-extension-default-method-to-class</a><br><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Apr 22, 2024 at 7:24 PM ІП-24 Олександр Ротань <<a href="mailto:rotan.olexandr@gmail.com">rotan.olexandr@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Subject: Proposal for Introducing Extension Methods to Java<br><br>Dear Java Development Team,<br><br>I hope this email finds you all in good spirits. I am writing to propose the integration of extension methods into the Java programming language, a feature that I believe holds considerable promise in enhancing code readability and maintainability.<br><br>Extension methods offer a means to extend the functionality of existing classes in a manner that aligns with Java's principles of static typing and object-oriented design. The proposed syntax, exemplified as follows:<br><br>public static void extensionMethod(extends String s) { ... }<br><br>adheres to established conventions while providing a concise and intuitive means of extending class behavior. Notably, the use of the `extends` keyword preceding the type parameter clearly denotes the class to be extended, while the method itself is declared as a static member of a class.<br><br>I wish to emphasize several advantages of extension methods over traditional utility functions. Firstly, extension methods offer a more cohesive approach to code organization by associating functionality directly with the class it extends. This promotes code clarity and reduces cognitive overhead for developers, particularly when working with complex codebases.<br><br>Secondly, extension methods enhance code discoverability and usability by integrating seamlessly into the class they extend. This integration allows developers to leverage IDE features such as auto-completion and documentation tooltips, thereby facilitating more efficient code exploration and utilization.<br><br>Lastly, extension methods promote code reusability without the need for subclassing or inheritance, thereby mitigating the risks associated with tight coupling and inheritance hierarchies. This modularity encourages a more flexible and adaptable codebase, conducive to long-term maintainability and scalability.<br><br>In light of these benefits, I believe that the integration of extension methods into Java would represent a significant step forward for the language, aligning it more closely with modern programming paradigms while retaining its core strengths.<br><br>I am eager to discuss this proposal further and collaborate with you all on its implementation. Your insights and feedback would be invaluable in shaping the future direction of Java development.<br><br>Thank you for considering this proposal. I look forward to our discussion.<br><br>The draft implementation can be found in the following branch of the repository: <a href="https://github.com/Evemose/jdk/tree/extension-methods" target="_blank">https://github.com/Evemose/jdk/tree/extension-methods</a>. I am new to Java compiler development, so any tips or remarks about what I have done in the wrong way or in the wrong place. I will add complete test coverage a bit later, but for now, there is "jdk" archive in the root directory of repo, which contains built in jdk for windows x86-64. If someone is willing to participate in testing as user, I would appreciate any help.<br><br>Best regards<br><div><br></div><div>PS: Note about internal implementation: it introduces a new flag - EXTENSION, that is equal to 1L<<32. It seems like it takes the last vacant bit in a long value type that has not been taken by flags. Not sure what the compiler development community should do about this, but it feels like it could be an obstacle to new features that might be introduced later.</div></div>
</blockquote></div>