Grouping extention methods

Carlos Eduardo Gesser gesser at gmail.com
Fri Dec 11 05:50:27 PST 2009


Hello everybody

What about creating a new kind of extension class, with the pourpose of
grouping extension methods to a single class/interface. E.g.:

super class ListOps<E> implements List<E> {
    void sort() { Collections.sort(this); }
    E first() { return get(0); }
}

super class MyStringOps extends String {
    String reverse() { ... }
    String presuffix(String p, String s) { return p + this + s; }
}

.....

import ListOps;
import MyStringOps;
...

List list = ....
list.sort();
String s = "abc".reverse();
String str = s.presuffix("[", "]")


This "super class" could extend/implement a single class/interface, and can
not have constructors/fields/abstract methods.
This way we can clarify witch class/interface we are giving new methods, and
minimize the namespace pollution that would occur if we let any static
method be used as extension method.

"super class" is only a sugestion, it dispenses a new keyword.

Internally it can be implemented as the original extension method proposal:
a class with static methods taking the extended class/interface as first
parameter, with a jvm level annotation to distinguish it.

-- 
Carlos Eduardo Gesser


More information about the lambda-dev mailing list