Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Java definitely supports generics as of 1.5 - did you mean that it doesn't support generics to the extent of C++? Most basic uses (containers) are identical between the languages, and while Java doesn't allow compile-time "duck typing" that C++ does, you can fake it by using the "? extends SomeInterface" syntax.


Java supports type erasure, which it idiosyncratically calls "generics", even though it does not allow generic programming. A generic function in a language with actual generics can work generically, on more than one type. A "generic" function in Java can work on exactly one type, namely the type which the "generic" type becomes after type erasure.

You can use "? extends SomeInterface" only if you write SomeInterface wrappers for every type which you want to use "generically." This is boilerplate relative to most other languages.


Personally, I find this boilerplate equivalent to the comments one has to write documenting the requirements of the type, with the added benefit of readable error messages when you pass the wrong type. In other words, I find:

  // T here is a type that must support these two operations:
  //   bool froznit();
  //   void frobnob(Foo* foo);
  template <class T> class Frozner { ... }
And

  public interface SomeStupidName {
    public bool froznit();
    public void frobnob(Foo foo);
  }
  public class Frozner<? extends SomeStupidName> { ... }
To be just as much work to type.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: