How is generic information used by the compiler if generic static methods are called?

kap

I have a question regarding generic types. I have written my own iterator type and would like to return an empty iterator if there is nothing to iterate over.

The following code works,

@Override
public Iterator<MyType> iterator() {
    final Iterator<MyType> iter = Collections.emptyIterator();
    return count > 0 ? new MyIterator() : iter;
}

however, it does not work, if written in one line like this:

@Override
public Iterator<MyType> iterator() {
    return count > 0 ? new MyIterator() : Collections.emptyIterator();
}

I get an error and have to cast the type to solve it:

@Override
public Iterator<MyType> iterator() {
    return (Iterator<MyType>)(count > 0 ? new MyIterator() : Collections.emptyIterator());
}

This cast leads then to a unchecked warning, in the first example there is no warning.

When I move the cast to the inside, it does not work either:

@Override
public Iterator<MyType> iterator() {
    return count > 0 ? new EdgeIterator() : (Iterator<MyType>) Collections.emptyIterator();
}

My question now is: why can't the compiler decide that the types are correct in the latter examples, but in the first. Why does the cast in variant 3 work, but not in the last one? The variants are all are basically the same in my impression.

Arnaud Denoyelle

If you want it to work, use this syntax :

 return Collections.<MyType>emptyIterator();

This is how to give the generic to a static method.

If you call Collections.emptyIterator(); then it returns a Iterator<Object> which is incompatible with your return type.

You also cannot cast because Iterator<MyType> does not extends Iterator<Object>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How is generic information used by the compiler if generic static methods are called?

From Dev

Static generic methods

From Dev

Why can't Java generic be used for static methods?

From Dev

Generic static methods within a generic class

From Dev

How can I use the same generic type in multiple methods in a non-generic static class

From Dev

Is there a workaround to use static methods by a generic class?

From Dev

Typescript Generic Class Constructor Argument with static methods

From Dev

Is there a workaround to use static methods by a generic class?

From Dev

how does compiler know that the class being used is a generic one although it has been compiled and its type erased to pre-generic code

From Dev

How does C# compiler determine reference equality in generic methods when operator overloads for a specified type exists?

From Dev

How does C# compiler determine reference equality in generic methods when operator overloads for a specified type exists?

From Dev

How to get class instances of generic types in generic methods

From Dev

How to use Swift Protocols with Generic methods and Generic Types

From Dev

Can I use Expression.Call on Generic and Static Methods?

From Dev

Why type parameter required before return type for static generic methods

From Dev

Generic static methods without brackets, e.g. in JavaFX 8

From Dev

Compiler crashes on generic lambda

From Java

How, when and where are generic methods made concrete?

From Dev

How to resolve this ambiguity in generic extension methods?

From Dev

How to add type methods to generic types in Swift?

From Dev

How to use generic EnumMap as parameter in abstract methods

From Dev

How can I compose generic methods in Scala?

From Dev

How to restrict parameter types in generic methods

From Dev

How to call Generic Portlet methods from servlet?

From Dev

Java: How do generic methods work?

From Dev

Java, Hibernate - How to create generic getByColumnValue() methods?

From Dev

How to add type methods to generic types in Swift?

From Dev

How to make these two java methods generic

From Dev

How to get the generic types used in a method in an array?

Related Related

  1. 1

    How is generic information used by the compiler if generic static methods are called?

  2. 2

    Static generic methods

  3. 3

    Why can't Java generic be used for static methods?

  4. 4

    Generic static methods within a generic class

  5. 5

    How can I use the same generic type in multiple methods in a non-generic static class

  6. 6

    Is there a workaround to use static methods by a generic class?

  7. 7

    Typescript Generic Class Constructor Argument with static methods

  8. 8

    Is there a workaround to use static methods by a generic class?

  9. 9

    how does compiler know that the class being used is a generic one although it has been compiled and its type erased to pre-generic code

  10. 10

    How does C# compiler determine reference equality in generic methods when operator overloads for a specified type exists?

  11. 11

    How does C# compiler determine reference equality in generic methods when operator overloads for a specified type exists?

  12. 12

    How to get class instances of generic types in generic methods

  13. 13

    How to use Swift Protocols with Generic methods and Generic Types

  14. 14

    Can I use Expression.Call on Generic and Static Methods?

  15. 15

    Why type parameter required before return type for static generic methods

  16. 16

    Generic static methods without brackets, e.g. in JavaFX 8

  17. 17

    Compiler crashes on generic lambda

  18. 18

    How, when and where are generic methods made concrete?

  19. 19

    How to resolve this ambiguity in generic extension methods?

  20. 20

    How to add type methods to generic types in Swift?

  21. 21

    How to use generic EnumMap as parameter in abstract methods

  22. 22

    How can I compose generic methods in Scala?

  23. 23

    How to restrict parameter types in generic methods

  24. 24

    How to call Generic Portlet methods from servlet?

  25. 25

    Java: How do generic methods work?

  26. 26

    Java, Hibernate - How to create generic getByColumnValue() methods?

  27. 27

    How to add type methods to generic types in Swift?

  28. 28

    How to make these two java methods generic

  29. 29

    How to get the generic types used in a method in an array?

HotTag

Archive