Note that there are some explanatory texts on larger screens.

plurals
  1. POType inference in Java (à la C#)
    text
    copied!<p>Ever since I heard of type inference (in Haskell), I lived under the impression that Java is the exact opposite, i.e., it has no type inference. Recently though, I had an aha moment and realized that Java employs type inference in its generics implementation.</p> <p>Then, I read two papers by Gilad Bracha (one of the people behind the generics implementation in Java, as far as I understand). The first paper is a <a href="http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf">tutorial about generics</a> (PDF), in which he explicitly says that the compiler will <em>infer</em> the actual type argument used to replace the format type parameter. So, there's type inference in Java, but why only for generics, why not something like C#'s <code>var</code> keyword? And this is my question to you.</p> <p>Why doesn't Java has more type inference built into the compiler?</p> <p>I will suggest an answer though, and this is related to the second paper that I read, <a href="http://bracha.org/pluggableTypesPosition.pdf">Pluggable Type Systems</a> (PDF). It seems, Gilad Bracha believes that the inference part shouldn't be part of the compiler, but an IDE feature or similar (section 4, paragraph 6 in the above mentioned paper):</p> <blockquote> <p>A better engineering approach is to implement type inference as a separate tool, available in the IDE. Programmers who find entering type annotations tiresome can invoke an inferencer on demand.</p> </blockquote> <p>What do you think?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload