Java generics capture inner class -
i have code:
public class undirectedgraphimpl<n> { [...] public iterator<edge<n>> adj(n v) { return new adjiterator(v); } private class adjiterator implements iterator<edge<n>> { [...] } public static void main(string[]args) { graph<integer> g = new undirectedgraphimpl<integer>(); [...] iterator<edge<integer>> = g.adj(4); } }
at compile time error:
error: incompatible types iterator<edge<integer>> = g.adj(4); ^ required: iterator<edge<integer>> found: iterator<cap#1> cap#1 fresh type-variable: cap#1 extends edge<integer> capture of ? extends edge<integer>
if replace line with
iterator<edge<integer>> = (iterator<edge<integer>>)g.adj(4);
then unchecked cast warning, don't understand why compiler capture "? extends edge<integer>". con explain me happens , how fix this?
edit: graph interface implemented undirectedgraphimpl class
public interface graph<n> extends iterable<n> { [...] iterator<? extends edge<n>> adj(n v); [...] }
the problem you're returning type iterator<? extends edge<integer>>
, you're trying assign variable of type iterator<edge<integer>>
.
the cap thing see comes process of capture conversion, tries remove wildcards returned types. in case they're still incompatible.
capture conversion defined in section 5.1.10 of jls in case you're interested in seeing details.
Comments
Post a Comment