Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9044
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: generics puzzle |
| References | <9g2f24Fi0vU1@mid.individual.net> <M%hnq.3083$Oz5.1691@newsfe16.iad> <9g81eqFj6eU1@mid.individual.net> <ZqDnq.5673$UK6.3114@newsfe06.iad> <9gaomnFbrlU1@mid.individual.net> |
| Message-ID | <vvZnq.8759$eY3.3933@newsfe15.iad> (permalink) |
| Date | 2011-10-20 11:11 -0700 |
On 10/20/11 7:14 AM, blmblm@myrealbox.com wrote:
> In article<ZqDnq.5673$UK6.3114@newsfe06.iad>,
> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> wrote:
>> On 10/19/11 6:25 AM, blmblm@myrealbox.com wrote:
>>> In article<M%hnq.3083$Oz5.1691@newsfe16.iad>,
>>> Daniel Pitts<newsgroup.nospam@virtualinfinity.net> wrote:
>>>> On 10/18/11 2:45 AM, Steven Simpson wrote:
>>>>> On 17/10/11 16:58, Daniel Pitts wrote:
>>>>>> On 10/17/11 5:14 AM, Steven Simpson wrote:
>>>>>>> On 17/10/11 11:41, blmblm@myrealbox.com wrote:
>>>>>>>> One fix is to just introduce a method setFromModified() in GThing,
>>>>>>>> but that doesn't appeal to me.
>>>>>>>
>>>>>>> Instead of adding it to GThing, create a static method:
>>>>>>>
>>>>>>> private static<T> void setModified(GThing<T> t) {
>>>>>>> t.set(t.modified());
>>>>>>> }
>>>>>>
>>>>>>
>>>>>> What about the simpler solution:
>>>>>>
>>>>>> in the GThing class:
>>>>>>
>>>>>> public void setModified() {
>>>>>> set(modified());
>>>>>> }
>>>>>
>>>>> It "doesn't appeal" to the OP? Nor to me. Not sure I can put my finger
>>>>> on it, but if I were the maintainer of GThing, I'd consider its
>>>>> specification to be complete without this method. It doesn't increase
>>>>> the value of the class as an abstraction. Adding it would certainly just
>>>>> be a convenience for the caller. It solves a problem generated by the
>>>>> design of the call site, not the design of the class's contract. The
>>>>> caller can write his own routine as necessary.
>>>>>
>>>> Possibly, but if it is something that happens frequently, then it seems
>>>> more likely to belong to GThing than externally. Otherwise your
>>>> call-site is suffering from the code smell "feature envy"
>>>>
>>>> One other approach is a "visitor" pattern:
>>>>
>>>
>>> I know the "visitor" pattern (vaguely anyway), but .... I'm not sure
>>> I understand your version of it here, which -- does this compile?
>>> because ....
>>>
>>>>
>>>> public interface GThingVisitor {
>>>> <T> void visit(GThing<T> gThing);
>>>> };
>>>
>>> (Aside: Hm, a generic method in an interface?! well, why not,
>>> maybe, but I'm not sure it would have occurred to me to try!)
>> I've used it in the past, so I know it works.
>>>
>>>> public class SetModifiedGThingVisitor {
>>>> <T> void visit(GThing<T> gThing) {
>>>> gThing.set(gThing.modified());
>>>> }
>>>> }
>>>
>>> Was this meant to implement GThingVisitor?
>> Indeed it was. This entire snippet was typed up in my mail program, so I
>> missed a few pieces.
>>>> public class GThing<T> {
>>>> public void accept(GThingVisitor visitor) {
>>>> visitor.accept(this);
>>>> }
>>>> }
>>>
>>> visitor.visit(this)??
>> Yes, that's what I intended :-)
>
> Typos (thinkos?) happen.
>
>>>> public class CallSite {
>>>> public void setAllModified(Iterable<? extends GThing<?>> things) {
>>>> GThingVisitor visitor = new SetModifiedGThingVisitor();
>>>> for (GThing<?> thing: things) {
>>>> thing.accept(visitor);
>>>> }
>>>> }
>>>> }
>
> I can report that this approach works too, though I put the
> visitor code directly in CallSite as an anonymous inner class
> rather than writing a separate SetModifiedGThingVisitor class.
>
> Overall this approach has for me a certain "can't quite get my head
> around it" quality -- not in the sense that I don't understand how it
> works, but in the sense that I can't quite sort out how it compares
> to the static-method approach. Thoughts, anyone?
>
Frankly in this case it amounts to the same, but when you have more
complicated generics definitions, such as:
class A<T extends B<T, F>, F extends C<F, T>, Q extends D<T,F,B>> {
}
If you have an A<?, ?, ?>, I think the static approach fails due to
being unable to verify the "extends" part. Though I haven't tried it in
a while.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-17 10:41 +0000
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 13:14 +0100
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-17 15:14 +0100
Re: generics puzzle markspace <-@.> - 2011-10-17 07:33 -0700
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-17 16:26 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:48 +0000
Re: generics puzzle Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-10-17 15:36 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-17 08:58 -0700
Re: generics puzzle Steven Simpson <ss@domain.invalid> - 2011-10-18 10:45 +0100
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-18 09:42 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:25 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-19 10:04 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-20 14:14 +0000
Re: generics puzzle Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-20 11:11 -0700
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-17 09:12 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 14:49 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 18:27 +0200
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-18 17:45 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-18 22:15 +0200
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-18 18:59 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:28 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:21 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:27 +0000
Re: generics puzzle Robert Klemme <shortcutter@googlemail.com> - 2011-10-21 20:34 +0200
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-22 18:50 +0000
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-22 21:02 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:04 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:25 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-26 21:56 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-27 08:59 +0000
eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-29 17:05 +0000
Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-29 19:49 -0700
Re: eclipse shortcuts again (was Re: generics puzzle) blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:17 +0000
Re: eclipse shortcuts again (was Re: generics puzzle) Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:39 -0700
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:30 -0700
Re: generics puzzle Lew <lewbloch@gmail.com> - 2011-10-23 08:56 -0700
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-25 07:05 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-25 23:29 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-31 11:14 +0000
Re: generics puzzle Four of Seventeen <fseventeen@gmail.com> - 2011-10-31 05:34 -0700
Re: generics puzzle markspace <-@.> - 2011-10-18 21:21 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:29 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-20 17:22 -0700
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000
Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 06:22 -0300
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:29 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 01:20 -0700
Re: generics puzzle Martin Gregorie <martin@address-in-sig.invalid> - 2011-10-23 09:51 +0000
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-23 03:28 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:59 +0100
Re: generics puzzle Eight of Seventeen <eights17@gmail.com> - 2011-10-24 02:46 -0700
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-23 15:55 +0100
Re: generics puzzle Tom Anderson <twic@urchin.earth.li> - 2011-10-20 21:00 +0100
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-19 13:26 +0000
Re: generics puzzle Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-21 05:57 -0300
Re: generics puzzle blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-10-21 16:28 +0000
csiph-web