Open gavinking opened 10 years ago
P.S. we might be able to take the same approach with Comparable
.
XIter
class implements Iterator but has an iterator()
method. You meant next()
. And I assume we just throw UnsupportedOperationException
for XIter.remove()
.next()
function did you mean to call next$()
(you wrote next()
) in the ceylon.language.Iterator
branch? In that case it would need to be a hidden method declared on c.l.Iterator
, and not merely on subclasses.Util.next()
.Java's Iterable<T>
is invariant in T
(specifically it declares Iterator<T> iterator()
rather than Iterator<? extends T> iterator()
), but Loopable<T>
needs to be out T
if it's to serve as a supertype of Ceylon's Iterable
.
It would still be legal to write:
Loopable l = nothing;
value done = l.iterator().next() is Finished;
Since the Ceylon Iterator
gets erased to j.u.Iterator
we ought to translate that as
l.iterator().$next()
(because we mean Ceylon's next()
), but j.u.Iterator
doesn't have such a method.
Java's
Iterable<T>
is invariant inT
(specifically it declaresIterator<T> iterator()
rather thanIterator<? extends T> iterator()
), butLoopable<T>
needs to beout T
if it's to serve as a supertype of Ceylon'sIterable
.
Oh, oops, that's annoying. It might be OK, since Loopable
is going to be erased anyway.
It would still be legal to write:
@tombentley note that I say above:
Finally, we would replace all calls to
Iterator.next()
with calls to the following clever utility function:
So in light of that, the following solution might be better than the one above. Given:
class MyIterable() satisfies Iterable<String> {
iterator() => MyIterator();
}
We generate:
class MyIterable() implements ceylon.language.Iterable<String>, java.lang.Iterable<String> {
public final ceylon.language.Iterable<? extends String> iterator$() { return MyIterator(); }
public final java.lang.Iterable<String> iterator() { return JavaIterator(iterator$()); }
}
And we transform all calls to iterator()
to iterator$()
.
This is not for 1.1.5, since it would require a break to binary compatibility. Rescheduling to 1.2.
To my mind, the biggest remaining point of discomfort with Java interop is the fact that a Ceylon
for
can't iterate a Java collection, and a Javafor
can't iterate a CeylonIterable
. So I've put some thought into this today and come up with what looks like a reasonable solution. First, we add the following interface toceylon.language
:Now, our existing
Iterable<T,N>
would inheritLoopable<T>
.The backend should:
Loopable
withjava.lang.Iterable
,Iterator
withjava.lang.Iterator
, andIterator
interface:Given:
The backend would produce the following code:
Finally, we would replace all calls to
Iterator.next()
with calls to the following clever utility function:WDYT? I don't see any real holes in this. The worse thing about it is the addition of the
Loopable
interface. But that might even eventually turn out to be useful in some other contexts. And a trivial sealed SMI is not really hurting anything.