thekid/rfc — Issue #189: RFC 0189: Extension methods
Author: thekid
Created: 2011-10-09 15:41:24+00:00
State: open

Scope of Change
====
Extension methods will be added to the XP Framework.

Rationale
====
* Provide an easy way to add functionality to existing classes
* Provide BC layers
* Get rid of "Helper" and "Util" classes

Functionality
====
Extension methods let you "add" methods to existing types without having
to change the respective type itself. They are declared in a regular
class as static methods, albeit being called as if they were instance
methods on objects of the type they extend. Seen from the outside, using
extension methods does not differ from calling methods actually defined
inside that type.

Declaration
----
Given this, to implement an extension method "sorted" for the ArrayList
class so that it can be used as in the example from the beginning, we
declare the following:

```php

```

There are two essential parts in this declaration: First of all, the
static initializer, which registers this class as a provider for extension
methods for the lang.types.ArrayList class. Second, the extension method
itself, which is declared `public static` and receives the ArrayList
instance to work on as its first parameter.

Using it
----
Although extension methods are defined static, they are called using
instance method syntax. However, extension methods are only available
if they're explicitly imported! This can be done by adding the extension
method class to the `uses()` list:

```php

```

Once this is achieved our example will work and the `sorted()`
call will yield an ArrayList containing the sorted values `1, 2, 3`.

Restrictions
----
Because of the way they're implemented, extension methods can only operate
on a type's public API. Extension methods are really only syntactic sugar,
backed by static method calls to an extension class. Thus, the following
two calls are equivalent from a protection level point of view.

```php
sorted();
$sorted= ArraySortingExtension::sorted($array);
?>
```

If you look at the second it becomes clear why only public methods can
be called!

Furthermore, extension methods can only add functionality. Extension
methods cannot overwrite existing methods. Thus, the following extension
method will never be executed because the `XPClass` class already
has a `getName()` method:

```php

```

The same applies for the methods defined in `lang.Object` itself,
every other object inherits these: `hashCode()`, `equals()`,
`getClassName()`, `getClass()` and `toString()`.

Security considerations
====
n/a

Speed impact
====
Extension methods are around 7 times slower than regular methods. If not
used, they don't impose a performance implication.

Dependencies
====
A general hook for classes that gets called for scope-"import" via uses(),
`static void __import(string $scope)`

Related documents
====
Internal sources:
* http://xp-framework.net/rfc/contrib/rfc0189.diff
Implementing patch
* http://news.xp-framework.net/article/313/2009/08/29/
Extension methods: Swiss army knives without the anti-pattern

Other sources:
* http://cr.openjdk.java.net/~briangoetz/lambda/Defender%20Methods%20v3.pdf
Interface evolution via "public defender" methods
* http://msdn.microsoft.com/en-us/library/bb383977.aspx
Extension Methods (C# Programming Guide)
* http://groovy.codehaus.org/Pimp+my+Library+Pattern
Pimp my Library Pattern
* http://www.oreillynet.com/onjava/blog/2007/12/extension_methods_proposals.html
Extension methods proposals
* http://weblogs.java.net/blog/forax/archive/2007/11/java_7_extensio.html
Java 7 - Extension methods
* http://journal.stuffwithstuff.com/2008/02/09/c-extension-methods-not-just-for-breakfast/
C# Extension Methods: Not Just for Breakfast
* http://www.codeproject.com/KB/dotnet/ExtensionUnitTests.aspx
Extension Methods and Unit Testing in VS2008
* http://blog.getpaint.net/2008/07/03/c-extension-methods-portability-aid/
C# Extension Methods ? Portability Aid?
* http://www.zorched.net/2008/01/03/implementing-mixins-with-c-extension-methods/
Implementing Mixins with C# Extension Methods
* http://stackoverflow.com/questions/299515/c-reflection-to-identify-extension-methods
C# Reflection to Identify Extension Methods
* http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.extensionattribute.aspx
ExtensionAttribute Class

Comments
====
friebe, Wed Aug 18 16:12:33 2010
----
Rewrote original implementation from scratch

friebe, Sat Aug 29 13:52:34 2009
----
Extension methods have been implemented in the 5_8 branch.