Content deleted Content added
→Current VB.NET solutions: fix syntaxhighlight error |
add java Fluent plugin alongside Lombok and Manifold |
||
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 [http://manifold.systems/docs.html#the-extension-manifold Manifold]
==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] or [https://github.com/rogerkeays/fluent Fluent], [[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 patch|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 14:
In C# they're implemented as static methods in static classes, with the first argument being of extended class and preceded by "<code>this</code>" keyword.
In Java you add extension methods via [http://manifold.systems/ Manifold], a jar file you add to your project's classpath. Similar to C# a Java extension method is declared static in an [http://manifold.systems/docs.html#extension-classes @Extension] class where the first argument has the same type as the extended class and is annotated with <code>@This</code>. Alternatively, the [https://github.com/rogerkeays/fluent Fluent] plugin allows you to call any static method as an extension method. No annotations necessary.
In Smalltalk, any code can add a method to any class at any time, by sending a method creation message (such as <code>methodsFor:</code>) to the class the user wants to extend. The Smalltalk method category is conventionally named after the package that provides the extension, surrounded by asterisks. For example, when Etoys application code extends classes in the core library, the added methods are put in the <code>*etoys*</code> category.
|