You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Gordon Ross <G....@ccw.gov.uk> on 2007/03/11 03:34:39 UTC

Does iBatis use or support Generics ?

In the methods such as queryForList, does iBatis support generic Lists ?
I couldn't see any reference anywhere to say yes or no...

GTG

Re: Does iBatis use or support Generics ?

Posted by Clinton Begin <cl...@gmail.com>.
I guess we'll have to disagree.  I'm not against generics entirely, just
Java's implementation is screwy.   I know it's for backward compatibility,
but I really wonder....why bother.

>> i.e. allows the compiler to perform stricter static type cheking?

VERY VERY minimal.

List<String> strings = new ArrayList<String>();
List unknown = strings; //it only takes one to break the chain
List<Integer> integers = unknown;

>> i.e. is safer?

No, all the errors are in the exact same place.  Using the code above, if I
use

Integer i = integers.get(0);

I get the same ClassCastException as if I had:

Integer i = (Integer) unknown.get(0);

>> i.e. is more (human) readable?

Well, using your example it's only more readable because the variable name
wasn't descriptive.  If you had rename "myFactory"  to "stringList", they
are both descriptive and readable.  The nice thing is that the variable name
is more readable everywhere, not just on the declaration.

Also, in my example above, I think the cast is more readable, only because
that's where the error will happen.  With the generic list it makes me
wonder "WTF happened?  I thought this was a typesafe generic list that could
only have integers!".  Which of course is not the case at all.

One old API will completely eliminate the type safety.   And it will be
years before all APIs give full respect to generic types.

Don't get me wrong, iBATIS will support them.  And as syntactic sugar
they're nice in some ways.  But nobody should be fooled.  They are not
safer.

If you get a chance to attend NFJS, Venkat explains it best:

http://www.nofluffjuststuff.com/speaker_topic_view.jsp?topicId=78

Cheers,
Clinton


On 3/13/07, c.zecca@ads.it <c....@ads.it> wrote:
>
> "Clinton Begin" <cl...@gmail.com> scritti il 13/03/2007 14:42:04
>
> > How is it safer?
>
> Let's assume to read the interface for the our factory (object loader)
>
> List
> myFactory( ... );
>
> List<String>
> myFactory(...);
>
>
> Which of the two signatures provides better information?
> i.e. allows the compiler to perform stricter static type cheking?
> i.e. is safer?
> i.e. is more (human) readable?
>
> Of course internally (within the factory) we have to perform one cast.
> List<Strings> allows the client code use the list of looaded strings with
> its correct type
> without any (dangerous) casting.
>
>
> > In both cases the error occurs at runtime at the exact same point in
> > the code.  And at runtime both are turned into a cast anyway.
> That's right.
> Exactly the minimum, i.e. one and only one casting, no more.
> A cast in necessary somewhere internally to the factory/loader.
> Externally is much better to use a generic container along with its actual
> type.
> I think that we agree.
>
> ciao
>

Re: Does iBatis use or support Generics ?

Posted by c....@ads.it.
"Clinton Begin" <cl...@gmail.com> scritti il 13/03/2007 14:42:04

> How is it safer?

Let's assume to read the interface for the our factory (object loader)

List
myFactory( ... );

List<String>
myFactory(...);


Which of the two signatures provides better information?
i.e. allows the compiler to perform stricter static type cheking?
i.e. is safer?
i.e. is more (human) readable?

Of course internally (within the factory) we have to perform one cast.
List<Strings> allows the client code use the list of looaded strings with
its correct type
without any (dangerous) casting.


> In both cases the error occurs at runtime at the exact same point in
> the code.  And at runtime both are turned into a cast anyway.
That's right.
Exactly the minimum, i.e. one and only one casting, no more.
A cast in necessary somewhere internally to the factory/loader.
Externally is much better to use a generic container along with its actual
type.
I think that we agree.

ciao

Re: Does iBatis use or support Generics ?

Posted by Clinton Begin <cl...@gmail.com>.
How is it safer?

In both cases the error occurs at runtime at the exact same point in the
code.  And at runtime both are turned into a cast anyway.

I don't think it's safer, it just makes it less clear where the cast is
actually happening, which is bad for code readability.

Clinton

On 3/13/07, c.zecca@ads.it <c....@ads.it> wrote:
>
> "Clinton Begin" <cl...@gmail.com> scritti il 11/03/2007 04:24:15
>
> > Well, since Java 5 generics are kind of half baked, we don't really
> > need to do anything to support them.  Sure, we could make it a
> > little more "warning friendly", but at the end of the day you'll be
> > no more type safe....
> >
> > You can do this in Java 5:
> >
> >     List untyped = new ArrayList();
> >     untyped.add(1);
> >     untyped.add(2);
> >     untyped.add(3);
> >
> >     // WOW!
> >     List<String> strings = untyped;  // [*]
> >
> >    // Error doesn't happen until you attempt to use the array,
> >    // no different than old-school casting, but less clear code.
> >     for(String s : strings) {
> >       ///...
> >     }
>
> Genericity is heavily based on a continuum of the static type checking
> that can in no way be ensured when you retrieve objects from a non o-o
> database, for sure not within the factory itself.
> Sooner or later (as quoted above) the type checking has a discontinuity.
> That is not a reason to disqualify genericity.
> The code based on List<String> that follows [*] is *safer* than the
> corresponding code based on List (old school).
>
> The casting is always an act of ... faith.
> Better the less than the more.
> ;)
>
> ciao
>

Re: Does iBatis use or support Generics ?

Posted by c....@ads.it.

"Clinton Begin" <cl...@gmail.com> scritti il 11/03/2007 04:24:15

> Well, since Java 5 generics are kind of half baked, we don't really
> need to do anything to support them.  Sure, we could make it a
> little more "warning friendly", but at the end of the day you'll be
> no more type safe....
>
> You can do this in Java 5:
>
>     List untyped = new ArrayList();
>     untyped.add(1);
>     untyped.add(2);
>     untyped.add(3);
>
>     // WOW!
>     List<String> strings = untyped;  // [*]
>
>    // Error doesn't happen until you attempt to use the array,
>    // no different than old-school casting, but less clear code.
>     for(String s : strings) {
>       ///...
>     }

Genericity is heavily based on a continuum of the static type checking that
can in no way be ensured when you retrieve objects from a non o-o database,
for sure not within the factory itself.
Sooner or later (as quoted above) the type checking has a discontinuity.
That is not a reason to disqualify genericity.
The code based on List<String> that follows [*] is safer than the
corresponding code based on List (old school).

The casting is always an act of ... faith.
Better the less than the more.
;)

ciao

Re: Does iBatis use or support Generics ?

Posted by Clinton Begin <cl...@gmail.com>.
Well, since Java 5 generics are kind of half baked, we don't really need to
do anything to support them.  Sure, we could make it a little more "warning
friendly", but at the end of the day you'll be no more type safe....

You can do this in Java 5:

    List untyped = new ArrayList();
    untyped.add(1);
    untyped.add(2);
    untyped.add(3);

    // WOW!
    List<String> strings = untyped;

   // Error doesn't happen until you attempt to use the array,
   // no different than old-school casting, but less clear code.
    for(String s : strings) {
      ///...
    }

Clinton



On 3/10/07, Gordon Ross <G....@ccw.gov.uk> wrote:
>
> In the methods such as queryForList, does iBatis support generic Lists ?
> I couldn't see any reference anywhere to say yes or no...
>
> GTG
>