M
markspace
So we've gone around about bounded wildcard types here on cljp. I've
got a method where it seems natural to return a references type like this:
(Note: none of this code was syntax checked.)
Type<? extends Bound> someMethod() {....
Normally, this would be bad, or at least dubious. However, what I've
got is a situation where "Bound" is little more than a marker type (for
now) and Bound is designed to be extended. To be a bit more clear:
public interface MyMap {
class Node {}
List<? extends Node> getNodes();
}
And what I'm actually doing is:
public class MyMapImpl implements MyMap {
List<MyNode> nodes;
List<? extends Nodes> getNodes() {
return Collections.unmodifiableList(
nodes );
}
private static class MyNode extends MyMap.Node {}
}
So, the return type of getNodes() appears to match pretty well with it's
intended purpose. Not only does the compiler prevent you from adding
types to the List returned by getNodes(), but the runtime does as well.
I'm just wondering if anyone sees problems here. Maybe some needed
flexibility in the return type of getNodes() that'll bite me later?
I haven't gone any further than this with the design, I just thought I'd
put this up for discussion. Have fun!
got a method where it seems natural to return a references type like this:
(Note: none of this code was syntax checked.)
Type<? extends Bound> someMethod() {....
Normally, this would be bad, or at least dubious. However, what I've
got is a situation where "Bound" is little more than a marker type (for
now) and Bound is designed to be extended. To be a bit more clear:
public interface MyMap {
class Node {}
List<? extends Node> getNodes();
}
And what I'm actually doing is:
public class MyMapImpl implements MyMap {
List<MyNode> nodes;
List<? extends Nodes> getNodes() {
return Collections.unmodifiableList(
nodes );
}
private static class MyNode extends MyMap.Node {}
}
So, the return type of getNodes() appears to match pretty well with it's
intended purpose. Not only does the compiler prevent you from adding
types to the List returned by getNodes(), but the runtime does as well.
I'm just wondering if anyone sees problems here. Maybe some needed
flexibility in the return type of getNodes() that'll bite me later?
I haven't gone any further than this with the design, I just thought I'd
put this up for discussion. Have fun!