3 ms·
I understand that part. But the comparison with Java is still bizarre, or perhaps just wrong. Java has nothing similar to this: impl Iterator for MyType {
by tsimionescu 2y ago
I understand that part. But the comparison with Java is still bizarre, or perhaps just wrong.
Java has nothing similar to this:
impl Iterator for MyType {
type Item = i32;
}
If you defined Iterator like this:
interface Iterator {
interface Item{}
public Item next();
...
}
Then Iterator.Item is the same type for all implementations, each implementation doesn't get to define its own type.
Furthermore, a Java type can't implement both Iterator<Integer> and Iterator<String>: it has to implement a single one. So Java's Iterator<T> is in almost all ways equivalent to Rust's Iter, with its one associated type, and not to a Rust BadIter<T>.
And this has nothing to do with how methods are dispatched, it has everything to do with the semantics of their respective type systems.
- tadfisher 2y agoJava has a pattern that is a superset of this, which is the self-recursive generic type: interface Foo<T extends Foo<T>> class Bar implements Foo<Bar> This is used to implement associated-type and Self-type behavior, at the expense of boilerplate. You can have multiple levels of associated types, though, which is murderous on the pinkies (or whatever finger types the angle bracket keys in your layout).