The inferred type will not be List<String>, it'll be ArrayList<String>, but thanks to polymorphism you can use that anywhere you'd want to use a List<String> anyway.
I remember seeing an SPJ talk where he mentioned that HM cannot be used in OO languages because of subtyping (he didn't explain this). But that obviously will never happen in Java. Someone correct me if I'm wrong. I don't know a lot about type inference.
The video was something like "classes but not as you know them" on channel 9 and was about type classes. The above was briefly mentioned in the last few minutes.
Edit: title was "classes, jim, but not as we know them"
Subtyping does wreck full inference, but limited (still somewhat useful) inference is still possible. Edward Kmett once summarised it as "it works great until the point where you really need it, when it breaks down."
I'm not convinced that's even a good idea in the first place, honestly. I would be rather confusing to have the type of a local inferred by future uses of the local.
public List<String> greet() {
var list = new ArrayList<>();
list.add("Hello, ");
list.add("World");
return list;
}
Let's just look at the expression return list here. Its abstract syntax tree will be a Return node with list as the expression to return, and the type of the return expression must be assignable to List<String>. Whatever type list has, it must now satisfy assignability to List<String> for the types to unify.
If we also look at var list = new ArrayList<>();, we know the right-hand side of the expression has a type of ArrayList<a> where a is an unresolved type, and thus the left-hand side must be of a type which is assignable from ArrayList<a>.
At this point the compiler can reconcile that List<String> must be assignable from ArrayList<a>; a List is assignable from an ArrayList, so that's fine, but the only thing which a can be for this to compile now is String: the signature for List<String> is not variant in its generic type.
A type inferencing algorithm has some choice here: it can work out the most general type which suits, or the most specific type, or any type which satisfies all known conditions. AFAIK most algorithms go for the most general type, and narrow it as needed, so let's go for that.
What's the most general type which can be assignable to both a List<String> and an ArrayList<String> ? Well, it's just List<String>, so therefore the type of list should be List<String>.
We check that against the remainder of the AST, notably the expressions list.add("Hello, ") and list.add("World"). These require a method add(=String) on list, using made-up syntax to express that the method needs an argument type assignable from String. This checks fine, as String is assignable from String in the signature add(E ...) where E is String.
All that magic happens under the hood, but when you read that, it's pretty obvious that the type of list must be something which can hold an ArrayList, and the ArrayList's generic argument must be String, because you've gotta be able to assign the ArrayList in, add some Strings to it, and return it as a List<String>.
It's my opinion that the code above has all the information a programmer needs to understand what's going on. Whether list is typed as List<String> or ArrayList<String> doesn't really matter, because (a) the object it refers to is definitely going to be an ArrayList<String> no matter the variable's type, and (b) it'll be returned as a List<String> no matter the variable's type. There's no ambiguity in the meaning of the code, and some boilerplate declaration of "obvious" types is removed.
Static typing really shines when it gives you all the compile-time guarantees of correctness that a static type system allows, yet doesn't require wads of tedious boilerplate when it's "obvious by looking at it".
Being freed from worrying about intermediate types is already a great win for Java 8's expressivity, a similar freedom from worrying about the type of transient variables is something I welcome :-)
This is a great explanation, but it also demonstrates a huge reason why it won't happen: the Java designers assume most Java programmers are too dumb to understand.
some users imagine type inference to be a form of mind reading, rather than an algorithm for constraint solving
Yeah, and I think that example is awful. Okay, it looks cute for a very small, unrealistic function with a single local and only adding literals, but once you get 20-30 lines, throw in more than one local, and add things to the list that are returned from other functions, it quickly becomes very, very difficult to reason about.
Yes, and that's why, in languages with full type inference, it's recommended to write out the full type for things that are in large scopes. You really only skip it for things that are in scope for up to 8 something lines.
They explicitly mention, that they thought about widening the scope to find more matches (i.e.,what you describe), but don't care for the action at a distance problem where the type is defined 30 lines down - or not resulting in compilation problems that aren't easy to find and understand.
I don't know why you would even want to do that except to confuse future maintainers by reusing a variable when you clearly need it for a different purpose and even if your code was perfectly acceptable it would require full type inference, otherwise it wouldn't be possible to find the common super type for all assignments.
I can claim that this code sucks because the original instance should not be created unless it is needed but instead I am just going to point out the fact that you cannot use var everywhere. The old syntax will still work and you should fallback to it on occasion.
9
u/codebje Mar 09 '16
The proposal has an example for just such a case:
The inferred type will not be
List<String>, it'll beArrayList<String>, but thanks to polymorphism you can use that anywhere you'd want to use aList<String>anyway.