Proposal: Indexing access syntax for Lists and Maps
Mark Thornton
mthornton at optrak.co.uk
Mon Mar 30 12:35:47 PDT 2009
Joseph D. Darcy wrote:
> The compiler needs a few pieces of information to perform the indexing
> translating including "Should this type get indexing support?" and "If
> this type gets indexing support, what methods do read and write
> operations get mapped to?"
>
public class java.lang.reflect.IndexedType {
public static boolean isIndexable(Class type);
public static IndexedType get(Class type);
public Class<?> getDeclaringClass();
public Class<?>[] getIndexTypes();
public Method getReadMethod();
// or
public String getReader();
public Method getWriteMethod();
}
The compiler can implement a compile time analog of this. Assuming the
use of annotations to indicate the methods, it will need to scan super
interfaces. A marker interface would be one way of optimising this scan
(only have to scan super interfaces that extend the marker).
Alternatively the runtime could be extended so that the Class object
holds a reference to IndexedType (for indexable types) which is
populated at class load time.
The method getIndexTypes() returns an array to allow n-D access. If this
isn't desired replace by Class<?> getIndexType().
> The most magical way to indicate the indexing support bit is to have a
> marker interface (or even an annotation) and then to have the names of
> the get/set methods indicated as annotation values. As a strawman
> something like
>
> public interface java.lang.Indexable {}
>
> @Documented
> @Target(TYPE)
> @Retention(RUNTIME)
> @Inherited
> public @interface java.lang.IndexableNames {
> String reader() default "get";
> String writer();
> }
>
or perhaps a pair of annotations:
@Documented
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
public @interface java.lang.IndexedRead {
}
@Documented
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
public @interface java.lang.IndexedWrite {
}
> where then, say, java.util.List would be changed from
>
> public interface List<E> extends Collection<E>
>
> to
>
public interface List<E> extends Collection<E> {
@indexedRead public E get(int index);
@indexedWrite public E set(int index, E value);
// ...
}
In addition, I would like to be able to declare something like this:
public interface Matrix2D {
@indexedRead public double get(int i, int j);
@indexedWrite public void set(int i, int j, double value);
}
and then write
Matrix2D m;
double x = m[i,j];
m[i,j] = 2*x;
Other notes:
* Can an class inherit indexable capability more than once?
* Can the read/write methods be overloaded?
Regards,
Mark Thornton
More information about the coin-dev
mailing list