Hacker News new | ask | show | jobs
by kentosi 3335 days ago
The examples you've given have direct values. Try:

  var index = getIndexFromSomewhere();
  var name = getHandleFromUser("peter", "waivers")
The type isn't as clear anymore.

*Edit: I'm absolutely a fan of the var syntax, having dabbled with scala. I'm just expressing what I think the original author's complaint is.

4 comments

I think the code is much more readable and less annoying with inference. And as for your examples, you don't need to know the types - the compiler will complain if you try and use them inappropriately whether the type was explicit or not.

But the issue you have glossed over is that sometimes there is no nominal type - there is only an anonymous type, as for example returned by a LINQ query using new{}, or via monad-style combinators where you really don't want to see the type for fear of your eyes bleeding.

> I think the code is much more readable and less annoying with inference.

I concur. I've been dabbling with Java a bit again (mainly because of a hobby project in order to learn Clojure) after a pause of multiple years.

It boggles my mind that compiler inference didn't make it into the language yet. Everytime I have to declare a totally obvious type, I cringe.

Isn't Java generally considered to be unusable without a decent IDE anyway? I feel like if you need to know what the return type is, the editor should be able to tell you that in these cases.
a) You can optionally include the type if you want. I do this in some rare circumstances where it does create more readable code.

  string name = getHandleFromUser("peter", "waivers")
b) It is usually not necessary to know what the type is while reading code, and if you need to know you can just hover over the variable.
>The type isn't as clear anymore.

It doesn't have to be, as the compiler (and linter) will inform of any misuse of the type later on. And any editor worth its salt could even annotate the type right there.