Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed01.chello.at!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Newsgroups: comp.lang.java.programmer From: Andreas Leitgeb Subject: Re: looping through a list, starting at 1 References: <4e3745a2$0$305$14726298@news.sunsite.dk> <62239393-929c-4764-8c8e-9620a03a7b81@c29g2000yqd.googlegroups.com> <80346568-647c-4e27-8192-33e1765a09ce@glegroupsg2000goo.googlegroups.com> Reply-To: avl@logic.at User-Agent: slrn/pre0.9.9-111 (Linux) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: Date: 11 Aug 2011 16:18:03 GMT Lines: 34 NNTP-Posting-Host: gamma.logic.tuwien.ac.at X-Trace: 1313079483 tunews.univie.ac.at 71616 128.130.175.3 X-Complaints-To: abuse@tuwien.ac.at Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7022 Lew wrote: > On Thursday, August 11, 2011 4:37:02 AM UTC-7, Andreas Leitgeb wrote: >> Arved Sandstrom wrote: >> > On 11-08-10 07:50 PM, Andreas Leitgeb wrote: >> >> How would I write a method (overload) that would only take an >> >> object if it is both List and RandomAccess? (Afaik: no way, but >> >> I might perhaps miss something.) >> > Pretty much in the standard way, which would take advantage of the >> > marker interface in a way suggested by Bloch: you create an interface >> > that extends List and RandomAccess, and concrete classes implement that. >> That's just not applicable to the Collections framework as it is: >> We've all been taught to use interface-names for the static type >> of variables to hold whatever concrete implementation: >> ArrayList l = new ArrayList(); // discouraged >> List l = new ArrayList(); // encouraged > No, that's not what we've been taught. > We've been taught to use the type that most generally allows the compiler > to enforce the specific contract we need. Fair enough. If I was to write code that was supposed to work for both types (with or without a particular marker interface), then in the part for "no marker" (as statically determined) I'd better still add a runtime- check for it. In theory, theory and practise are the same, in practise they aren't. :-) >> interfaces (such as RandomAccess) are an anti-pattern themselves, or >> that pattern is one declared exception to the "instanceof"-red flag. > Marker interfaces are not an antipattern. Use of 'instanceof' is, often. > Use marker interfaces for *compile time* safety. I wonder, if that is done in practise. E.g.: do you happen to know code in the Java Standard Library or other renowned project that does this kind of static check for a marker-interface?