开发者

Language with the least ugly generics implementation (or alternative) [closed]

开发者_运维技巧 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 12 years ago.

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.


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:

  • Haskell
  • ML
  • OCaml

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:

  • Ruby
  • Python
  • Objectiv-C
  • Javascript

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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜