[xbase] Extension functions, inlining and arrays [message #947142] |
Tue, 16 October 2012 21:19 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
Hi,
I've defined a new index operator (# symbol) that is used for accessing
elements of a collection based on a key. The operator is mapped to an
overloaded extension function, similar to how the built-in operators
work. I'm now struggling with two issues: inlining and supporting arrays.
I use the @Inline annotation and it seems to work, but the imported
attribute doesn't seem to have any effect. E.g. the following function
is used, but Iterables isn't imported as I think it should. I have to
use @Inline(value="com.google.common.collect.Iterables.get($1, $2)"
instead, which results in more verbose code.
@Pure
@Inline(value="Iterables.get($1, $2)", imported=Iterables.class,
statementExpression=true)
public static <T> T operator_index(Iterable<T> iterable, int pos) {
return Iterables.get(iterable, pos);
}
The second (and more important) problem is that I'm not able to support
arrays. I define the following extension function (and similar for other
array types), but it never seems to be noticed:
@Pure
@Inline(value="$1[$2]")
public static int operator_index(int [] array, int pos) {
return array[pos];
}
E.g. in a method where i is declared as an int[], i # 0 is translated to
Iterables.get(((Iterable<Integer>)Conversions.doWrapArray(i)), 0) and
not to i[0] as I want.
Is it possible to support arrays like this?
Hallvard
|
|
|
Powered by
FUDForum. Page generated in 0.02704 seconds