0

I find the way Generics are implemented in Java to be way too complex and very ugly(syntax).

I think the concept of generics is alright, but implementation kinda sucks.

Was wondering which other language might have the best/simplest/cleanest generics implementation. .... or maybe an alternative to generics per say with the same purpose.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
Thibaut Colar
  • 919
  • 2
  • 9
  • 19
  • 1
    Would you chose a language on the basis of how nice the generics is in that language? Generics is only ugly if you have to write libraries for it. If use just have to use those libraries its pretty simple. – Peter Lawrey Jan 20 '11 at 21:09
  • 1
    This is not subjective or argumentative in any way. This is merely comparing and contrasting different programing styles. It is very pathetic when people banning topics just because they point out flaws in their favorite programming language. Just because you think Java is the greatest programing language ever invented, doesn't mean everyone else has to agree with you. Please get a life and stop censoring points of view that you don't like. – Jay Jan 23 '11 at 20:58
  • Besides I'm not Java hater, I've been employed as a Java programmer since 1998 as a matter of fact(Java 1.1)..... I'm just wondering which language might have a better implementation of the generics concept (examples) and got some good answers here ... so I don't know why this is being banned .. but whatever. As I posted before I asked the question because a generic implementation is being considered for a language that doe NOT have them yet, hence the question. – Thibaut Colar Jan 29 '11 at 18:08

4 Answers4

3

There are two options to simplify the type system in the fashion you ask for.

Type Inference and Dynamic Typing.


Type Inference

The idea behind type inference is to have the compiler to to figure out the type of your objects for you so your code can be simplified by omitting the type information. It has other advantages as well.

Some popular languages with type inference:


Dynamic Typing

In a dynamically typed language, generics are not needed because you get it for free with the dynamic typing. If you want to understand more about how this simplifies the type system, study up on duck typing. Here's a short intro. to the rational behind duck typing.

Some popular languages with dynamic typing are:


You're right that generics in Java was implemented very poorly. This was by design. Why a crappy generics implementation? When they were doing the designs for Java generics, there were compatibility issues with old code, and extensive changes were needed to existing VMs to implement it. Eventually they gave up and gave Java the deficient implementation is has today.

http://code.stephenmorley.org/articles/java-generics-type-erasure/
http://www.ibm.com/developerworks/java/library/j-jtp01255.html
Why do some claim that Java's implementation of generics is bad?

C++ and C# have a much better generics implementation. Check them out if you want a Java-like language and a similar generics implementation.

Community
  • 1
  • 1
Jay
  • 9,314
  • 7
  • 33
  • 40
  • 1
    If you're going to down vote, you could at least enlighten us with the points you disagree with and why. – Jay Jan 23 '11 at 21:03
2

If you are looking for something more concise and elegant I would suggest you to look forward functional languages like OCaml or Haskell (maybe F# if you want an API similar to the Java one) that have the so called type reconstruction which is how the real generics thing should be implemented.. maybe it's a little bit more strict but you don't lose expressivity, you just gain in syntax.

In any case your question is quite silly, you don't choose a language according to just "generics", it's a long and complex tale which usually includes what are you going to develop. Every program has its story and its needs.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

I think that the generics within Eiffel may be the cleanest.

Darron
  • 21,309
  • 5
  • 49
  • 53
0

The creator of Java Generics created the Scala language. "Generics" in Scala (Type Parameters) are much cleaner comparing to Java, as almost any other thing on it.

adrianboimvaser
  • 2,651
  • 1
  • 22
  • 30