Alex
Alex

Reputation: 3

(java) incompatible types?

I'm getting an error in compilation:

'incompatible types - found unit but expected unit'

Why is this? This is when returning from a method, I can even parse the returning data into 'unit' and it works fine, why is this error occurring? Does the fact that I am using an iterator within a collection I have made have anything to do with it? I haven't left the iterator type E as I specifically want this collection for one type of object.

private class unitHashIterator<unit> implements Iterator<unit> {
  private unitHash hash;
  private int nextIndex;
  private unitHashIterator(unitHash u) {
    hash = u; nextIndex = 0;
  }
  public unit next() {
    if(!hasNext())
      throw new NoSuchElementException();
    return hash.data[nextIndex++]; << HERE OCCURS THE ERROR
  }
}

This class is contained within a collection.

'incompatible types - found unit but expected unit'

Upvotes: 0

Views: 162

Answers (1)

jacobm
jacobm

Reputation: 14025

Regardless of the definition of unitHash, it can't possibly be guaranteed contain elements of type unit, because unitHash isn't parameterized over types but unitHashIterator is. I suspect you either meant to write class unitHashIterator rather than class unitHashIterator<unit>, or private unitHash<unit> hash; rather than private unitHash<unit> hash;.

The error message you're getting is probably because the compiler expects to return something of the same type as the type variable unit, but hash returns something of some concrete type (not type variable) that happens to also be named unit.

Upvotes: 2

Related Questions