Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7021
| Newsgroups | comp.lang.java.programmer |
|---|---|
| From | Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> |
| Subject | Re: looping through a list, starting at 1 |
| References | (2 earlier) <62239393-929c-4764-8c8e-9620a03a7b81@c29g2000yqd.googlegroups.com> <j19jcs$fad$1@Gaia.teknon.de> <80346568-647c-4e27-8192-33e1765a09ce@glegroupsg2000goo.googlegroups.com> <slrnj45uke.6gl.avl@gamma.logic.tuwien.ac.at> <edffc0ad-6325-4622-93b4-abff84190e79@glegroupsg2000goo.googlegroups.com> |
| Message-ID | <slrnj47vhe.6gl.avl@gamma.logic.tuwien.ac.at> (permalink) |
| Date | 2011-08-11 16:07 +0000 |
Lew <lewbloch@gmail.com> wrote:
> Andreas Leitgeb wrote:
>> Lew wrote:
>>> Volker Borchert wrote:
>>>> if (l instanceof RandomAccess) {
>>> Tests on type like this are an antipattern.
>> Are marker-interfaces (which RandomAccess is, iirc) already an
>> antipattern, or is there a different way to check for them, [...]
> With generics you can use type intersections so that something has to be
> both 'List' and 'RandomAccess' (again, say) even to reach the call or the
> class or whatever.
Interesting stuff! (but still falls a bit short):
--- snip SSCCE Test.java ---
import java.util.*;
public class Test {
public static void main(String[] args) {
method( new ArrayList(42) );
method( new Vector(42) );
//method( new LinkedList(42) ); // would error: good!
}
public static <T extends List,RandomAccess> T method(T raAcList) {
return raAcList;
}
//Provide a fallback overload for all Lists: doesn't compile
// public static <T extends List> T method(T anyList) {
// return anyList;
// }
//Nor does this compile together with the generic one:
// public static List method(List anyList) {
// return anyList;
// }
//with Object for the param-type it would compile, but not
//help to filter for Lists at compile-time.
}
--- snip ---
So, while I could write a method that takes only objects which are
both List and RandomAccess, I cannot provide a less-picky overload,
that would only filter on List.
(This appears like a shortcoming to me, which may eventually be
fixed, but it doesn't work with 1.6.0_26.)
> It's the run-time type check that one often wishes to avoid.
> Of course, if the logic truly requires it, as it sometimes does,
> then the antipatternness goes away. I just find that use of
> 'instanceof' in the world of types and generics frequently
> signals incompletely thought-out type assertions.
Well, for now, I guess we'll just have to face that it is necessary
more often than it would be in an ideal Java.
> If there were an SSCCE for the particular use case, one might
> be able to discern a less run-timeish way forward or one might not.
The case at hand didn't really suffer from the distinction.
The object is asked at runtime if it is a RandomAccess, and
either way correct (hope so) and appropriate code is executed
for the object. It's not principially different from asking
a (list-)object if it is empty(), and do something appropriate
in each case.
> I only wished to point out a general principle here.
Thanks for pointing out the trick with generics, even though
it doesn't yet fully work. I hadn't thought of that, and might
run into a situation where it could prove useful.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Re: looping through a list, starting at 1 Arne Vajhøj <arne@vajhoej.dk> - 2011-08-01 20:32 -0400
Re: looping through a list, starting at 1 Robert Klemme <shortcutter@googlemail.com> - 2011-08-02 02:42 -0700
Re: looping through a list, starting at 1 v_borchert@despammed.com (Volker Borchert) - 2011-08-02 19:32 +0000
Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-10 09:00 -0700
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-10 21:39 +0000
Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-10 19:10 -0300
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-10 22:50 +0000
Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 07:02 -0300
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 11:37 +0000
Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 07:14 -0700
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 16:18 +0000
Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 17:39 -0300
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 08:09 +0000
Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-11 09:52 -0400
Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-11 17:26 -0300
Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-11 20:11 -0400
Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-10 20:31 -0700
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 16:07 +0000
Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 09:20 -0700
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-11 18:03 +0000
Re: looping through a list, starting at 1 Lew <lewbloch@gmail.com> - 2011-08-11 12:55 -0700
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 08:32 +0000
Re: looping through a list, starting at 1 supercalifragilisticexpialadiamaticonormalizeringelimatisticantations <supercalifragilisticexpialadiamaticonormalizeringelimatisticantations@averylongandannoyingdomainname.com> - 2011-08-12 09:09 -0400
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 14:38 +0000
Re: looping through a list, starting at 1 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-08-12 14:51 +0000
csiph-web