Content deleted Content added
→Centralize common behavior: use file-scoped namespace |
DST~dewiki (talk | contribs) m Added Project Lombok as implementation possibility for Java |
||
Line 5:
In [[object-oriented computer programming]], an '''extension method ''' is a [[Method (computer programming)|method]] added to an object after the original object was [[Compiler|compiled]]. The modified object is often a class, a prototype or a type. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method declared in the type definition.<ref name="ms_ext">{{cite web|url=http://msdn.microsoft.com/en-us/library/bb383977.aspx|title=Extension Methods|publisher=Microsoft|accessdate=2008-11-23}}</ref>
Not all languages implement extension methods in an equally safe manner, however. For instance, languages such as C#, Java (via Manifold or [https://projectlombok.org/features/experimental/ExtensionMethod Lombok]), and Kotlin don't alter the extended class in any way, because doing so may break class hierarchies and interfere with virtual method dispatching. This is why these languages strictly implement extension methods statically and use [[static dispatch]]ing to invoke them.
==Support in programming languages==
Extension methods are features of numerous languages including [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]] via [http://manifold.systems/docs.html#the-extension-manifold Manifold] or [https://projectlombok.org/features/experimental/ExtensionMethod Lombok], [[Gosu (programming language)|Gosu]], [[JavaScript]], [[Oxygene (programming language)|Oxygene]], [[Ruby (programming language)|Ruby]], [[Smalltalk]], [[Kotlin (programming language)|Kotlin]], [[Dart (programming language)|Dart]], [[VB.NET|Visual Basic.NET]] and [[Xojo]]. In dynamic languages like [[Python (programming language)|Python]], the concept of an extension method is unnecessary because classes can be extended without any special syntax (an approach known as "monkey-patching", employed in libraries such as [[gevent]]).
In VB.NET and Oxygene, they are recognized by the presence of the "<code>extension</code>" keyword or attribute. In Xojo the "<code>Extends</code>" keyword is used with global methods.
Line 228:
*[http://www.drdobbs.com/cpp/uniform-function-call-syntax/232700394 Uniform function call syntax]
*[http://csharp-software-development.blogspot.com/2014/09/c-extension-methods.html Extension methods in C#]
*[http://manifold.systems/docs.html#the-extension-manifold Extension Methods in Java with Manifold]
*[https://projectlombok.org/features/experimental/ExtensionMethod Extension Methods in Java with Lombok]
*[https://kotlinlang.org/docs/reference/extensions.html Extension functions in Kotlin]
|