Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!easy.in-chemnitz.de!feeder.news-service.com!feeder3.cambriumusenet.nl!feed.tweaknews.nl!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: looping through a list, starting at 1 Date: Thu, 11 Aug 2011 07:14:09 -0700 (PDT) Organization: http://groups.google.com Lines: 43 Message-ID: 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: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 108.89.33.208 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1313072135 29786 127.0.0.1 (11 Aug 2011 14:15:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 11 Aug 2011 14:15:35 +0000 (UTC) Cc: avl@logic.at In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=108.89.33.208; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7020 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= . >=20 > 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 =3D new ArrayList(); // discouraged > List l =3D new ArrayList(); // encouraged No, that's not what we've been taught. We've been taught to use the type t= hat most generally allows the compiler to enforce the specific contract we = need. That does not always mean use the interface (not "names"), nor the m= ost general interface. Sometimes you do want to peg a variable or return t= ype to a specific implementation, or a specific interface, or an interface = that mixes in two parent interfaces per Bloch. Don't make a religion of "u= se the (most general) interface" - the actual principle is "use the type th= at guarantees your contract". So 'RandomList extends List, RandomAccess' i= s perfectly acceptable. ...=20 > That's of course just rambling. Marker-interfaces just aren't used that > way, but instead they are always checked at runtime. So, either Marker- Nope. Their value is when they're checked *at compile time*. It's the use at run time that is suspect. > 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. --=20 Lew