Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7030
| From | Arved Sandstrom <asandstrom3minus1@eastlink.ca> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: looping through a list, starting at 1 |
| References | (7 earlier) <slrnj462qe.6gl.avl@gamma.logic.tuwien.ac.at> <fNN0q.575745$SG4.165292@newsfe03.iad> <slrnj47fmu.6gl.avl@gamma.logic.tuwien.ac.at> <ad5e70fc-8d7b-47a7-83ff-50ec72b71faa@glegroupsg2000goo.googlegroups.com> <slrnj4805r.6gl.avl@gamma.logic.tuwien.ac.at> |
| Message-ID | <76X0q.575768$SG4.251613@newsfe03.iad> (permalink) |
| Organization | Public Usenet Newsgroup Access |
| Date | 2011-08-11 17:39 -0300 |
On 11-08-11 01:18 PM, Andreas Leitgeb wrote: > Lew <lewbloch@gmail.com> wrote: >> On Thursday, August 11, 2011 4:37:02 AM UTC-7, Andreas Leitgeb wrote: >>> Arved Sandstrom <asandstr...@eastlink.ca> 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? > Bloch, in the previously mentioned Item 37 of Effective Java, calls out Set as a so-called "restricted marker interface". Restricted because it extends Collection, so isn't a "free" marker interface. Nevertheless, it exhibits the compile-time features of a marker interface "as new type". Since you insist on JDK examples, you might also want to read the bit in Item 37 where Bloch explains how Serializable _could have been_ written so as to take advantage of that marker interface statically rather than at runtime. Sometimes an example of something done wrong carries more weight than an example of something done right. I don't think that you'll find a whole bunch of examples, let alone examples in "renowned" projects...whatever those are exactly. Most Java programmers have always equated "marker interface" in Java with java.io.Serializable: that single example has effectively squelched wider proper use of marker interfaces in the language. AHS
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