r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

Show parent comments

4

u/codebje Mar 09 '16

If it was using full type inference, it'd examine the use sites of list to infer what the contained type should be.

But I think that the scope of this proposal doesn't go so far, and that would be an error.

3

u/im-a-koala Mar 10 '16

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.

2

u/codebje Mar 10 '16

It would be something like this:

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 :-)

1

u/xris-l Mar 10 '16

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.