C# 3.0 introduced extension methods to "enhance" behavior of existing types without interface bloating to avoid modifying/breaking existing interfaces. This was the first time I learned of such a concept. If I'm not mistaken after 15 years of not working with C#, extension methods are just static methods with their first parameters marked with this
(so that it makes extension methods null-safe in terms of calling a method on "this", but obviously static methods obey visibility rules).
So are Kotlin extensions (have never worked with Kotlin, but Kotlin declarations look more natural to me though).
In Java there is no such thing as extension methods (Java 8+ interface default methods are not merely syntax sugar), however javac
plugins like Lombok or Manifold allow using extension methods right in Java.
C#, Kotlin, and the Manifold plugin uses the declaration-site approach: the extension method declarations must declare they can be used as extension methods (Manifold, if I remember well, allows declare a whole class of extension methods at once). Lombok uses a call-site approach: the class using extension methods can inject all static methods from specific classes, as if all the injected methods were declared extension methods.
I'm wondering, if all of these implementations use static methods, how would it affect if any scope-visible static methods could be merely used as extension methods not requiring extensions to be declared at declaration-site or call-site at all (staticMethod(obj)
-> obj.staticMethod()
, or whatever non-dot like obj.:staticMethod()
)? What implications might this idea have?
I've just came across this question and the idea above seems to be equal to UFCS, so let me rephrase the question above: how would it affect Java and C# that both have mature codebases?
My question does not seem to be a duplicate of this question.