Re: Java type-casting -- Q3
grz01 wrote:
And I also now start to suspect that "generics" is
kind of a "poor man's solution to polymorphism"
What the heck do you mean by that? Are you saying that polymorphism is a
problem to which generics is the solution in Java? Or are you saying that
generics is a substitute in Java for polymorphism? Because neither is even
remotely true.
Are you aware that Java has had polymorphism since its inception, well before
generics, and that generics expands and supplements the power of polymorphism?
Admittedly, my understanding of "generics",
is most likely a bit clouded by the fact that
I worked in the past for many years with
the pure polymorphic type-systems of functional languages,
which I always found totally sound, elegant,
and very easy and intuitive to work with.
I also find it interesting that you compare generics to functional languages.
Generics are not set up to express the same sorts of things that functional
syntaxes do, although they're slightly similar in that both are declarative.
But generics are much more narrow in focus - their only purpose is to declare
proofs about type relationships, and they're strictly compile time.
And again, polymorphism is a fundamental aspect of Java.
I am curious how else one might consider expressing a base-type relationship
like "list of String" in Java were generics not available.
As to your complaint that Pair<A, B> is not yet in the Java API, two posters
have responded to you with implementations of such a class demonstrating how
truly trivial it is to add to your code. It's hardly a show-stopping omission.
markspace's implementation is all of 20 lines long, without Javadocs, and he
implemented it as a nested class to handle your strange objection to writing
more than one top-level class:
public static class Pair<A,B> {
private final A first;
private final B second;
public Pair( A first, B second )
{
this.first = first;
this.second = second;
}
public A getFirst()
{
return first;
}
public B getSecond()
{
return second;
}
}
Maybe it's time to stop whining that someone else didn't write your code for
you and just go ahead and write your own code. Isn't that what you're paid
for? To be able to write code? Or do you want Sun and this newsgroup to do
your job for you? Shouldn't we get paid by your employer if we do?
--
Lew