by mcv · 7y ago · 0 comments
I think there are situations where explicit types are good, and situations where they're unnecessary or get in the way.
Java's use of generics is a good example of the latter:
ArrayList<String> list = new ArrayList<String>();
One of those just shouldn't be there. (Edit: Java 10 apparently added type inference, which means this is not necessary anymore.)But in function parameters, it's incredibly useful to be able to ensure that the parameters are of the type you expect (and preferably not null, which most languages don't offer). That prevents a lot of unnecessary boilerplate checking whether you actually have the right type in your hands.
Basically I don't need explicit types for stuff I declare locally, because I can see in the code what type it is. But for parameters and other data I get from other parts of the system (ajax calls, for example), enforced types are incredibly useful.
[0] Except Donald Knuth, who apparently cannot make mistakes.
Still, it doesn't change the fact that some type systems are cumbersome in some places, yet valuable in other places. I think we're currently seeing a movement towards type systems that do more for you while getting in the way less, and that's certainly useful.
Type inference isn't always good, though. Scala's type system can be quite horrible in places, although I think the idea is generally good, particularly for local variables.