3 ms·
In C# you can often avoid reflection using dynamic. Java generics problems: "primitive types" cannot be used as type parameters. Type erasure makes type parame
by kodfodrasz 10y ago
In C# you can often avoid reflection using dynamic.
Java generics problems: "primitive types" cannot be used as type parameters. Type erasure makes type parameters unavailable at runtime, which sometimes causes many problems.
When you see java code passing a .class as a constructor/method parameter, that is often a workaround.
Try Stream.toArray(). Type erasure makes it a pain and forces you to provide an array Supplier.
Other Example: you cannot have overloads of a method one taking Function<T,U>, other taking Supplier<T> because their erased signatures are equivalent.
Disclaimer: I still prefer Java when C# is not an option, yet I reserve the right to criticize its shortcomings.
- olavgg 10y agoCould you not use Groovy for this? Using Groovy isn't harder than using another third-party library.
- kodfodrasz 10y agoYeah, if I wouldn't care for type safety, and I'd want to write validation code for stuff currently handled by GSON instead of me, by the class definitions. We use Groovy for some things, but here I'd stick to Java. Also Groovy has limited IDE support.
- vorg 10y agoApache Groovy's dynamic typing, closures, and shorthand collection syntax (from v. 1.0) are great for testing and playing with Java classes, and I'm told Groovy's Ruby-like MOP is useful for Grails developers, but the stuff added later (like generics in v. 1.5) made the language klunky and overdone. Best switch to Java (or Scala/Kotlin/etc) once you need anything to be typed, let alone with generics.
- vvanders 10y agoOr even better, you can't use an array of generic types, which is just madness. Why even have a generic system if it's so half-baked. Yes you can use ArrayList but you'd think that something as fundamental as an array would work with generics. https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays https://docs.oracle.com/javase/tutorial/java/generics/restri...
- oldmanjay 10y agoI'm not sure if you articulated your overload beef correctly. What you wrote is trivially possible so charitably, you wrote it wrong.