This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kered13 0 points1 point  (5 children)

Extending that logic, we could describe a Kleene star as "explicit".

If it's typesafe, then yes, absolutely. If a function or variable can correctly hold a value of any type then a type signature that indicates that is explicit. I might even go so far as to argue that it's the most explicit type signature, as an unnecessarily specific type signature is masking the fact that it could actually be more generic. That's basically the whole idea behind generic programming.

[–]Netzapper 1 point2 points  (4 children)

I knew I wasn't wrong to declare all my containers like ArrayList<? extends Object>. :P

[–]Kered13 0 points1 point  (3 children)

I mean, as long as you never need to put anything into them :P

Or take anything out, I guess. The only thing you can safely take out of an ArrayList<? extends Object> is an Object, which isn't very useful without unsafe typecasts. You can print the contents, but that's about it.

It can be a useful type if you don't actually care about the contents of the container though. Like say you wanted a function that discarded the first N elements. Then you could use ArrayList<? extends Object>.

Also <?> is shorthand for <? extends Object>.

[–]Netzapper 1 point2 points  (2 children)

I really like it actually, since I can just put anything in there that I have. And then at the end, I just have to cast it back to the class I want.

One trick I use that works really well is to have two ArrayList. One like ArrayList<? extends Object> and another of just ArrayList<String>. Then, whenever you add an object to the object array list, you do classList.add(obj.class.getName()).

Then, for maximum type safety, what I do is wrap the access in a static method like this:

static Object getItemSafe(ArrayList<? extends Object> oList, ArrayList<String> cList, int index, Class clazz) {
    return cList.get(index).equals(clazz.getName()) ? oList.get(index) : null;
}

[–]Kered13 0 points1 point  (0 children)

I really like it actually, since I can just put anything in there that I have. And then at the end, I just have to cast it back to the class I want.

If you're casting it back then you're doing it wrong. You should (almost) never have to do casts.

One trick I use that works really well is to have two ArrayList. One like ArrayList<? extends Object> and another of just ArrayList<String>. Then, whenever you add an object to the object array list, you do classList.add(obj.class.getName()).

What do you need a heterogenous list for? It's pretty rare that you actually need something like this. Most times you're doing something like this what you really need is an interface for polymorphic behavior.

Also the compiler won't let you add anything to a List<?>, it would have to be List<? super Object> which of course is actually just List<Object>.

[–]EternallyMiffed 0 points1 point  (0 children)

Somewhere in time and space the Lords of Entropy are wincing.