Java context:
Class: Channel implements ICleanable
Variable: Set<Channel> channels = new HashSet<>()
I need to call this method with an array of Iterable
as parameter:
void unexport(Iterable<ICleanable>[] cleanables)
But I cannot devise how to write the call. This version doesn't work:
unexport(new Iterable<ICleanable>[] { channels })
The compiler says: "Type mismatch: cannot convert from Set<Channel> to Iterable<ICleanable>
"
Help would be appreciated (and/or reference to learning material). Thanks.
EDIT. Now that I've got the answer, I can summarize where I was wrong.
I had a problem with generics and subtypping. This is a beginner error (which is exactly what I'm). Clue:
List<String> ls = new ArrayList<String>();
List<Object> lo = ls; // Illegal, a List<String> is not a List<Object>!
lo.add(new Object());
String s = ls.get(0); // Attempts to assign an Object to a String!
A List<String>
cannot be a List<Object>
, otherwise the type safety void be void.
This is explained here.
I had a second problem with array of generics.
unexport(new Iterable<ICleanable>[] { channels })
It is not possible to create an array of Iterable. This is explained here. The error on type conversion prevented this second error to be detected by the compiler before correction of the former.
In my case I ended up with this:
void unexport(List<List<? extends ICleanable>> cleanables) {...}
Channel implements ICleanable;
List<Channel> channels = new ArrayList<>();
List<List<? extends ITVCleanable>> list = new ArrayList<>();
list.add(channels);
unexport(list);