Representing the Impractical and Impossible with JDK 10 “var”

Having benefited from “var” for many years when writing c#, I’m delighted that Java is at last getting support for local variable type inference in JDK 10.

From JDK 10 instead of saying

ArrayList < String > foo = new ArrayList < String > ( ) ; ArrayList<String> foo = new ArrayList<String>();

we can say

var foo = new ArrayList < String > ( ) ; var foo = new ArrayList<String>();

and the type of “foo” is inferred as ArrayList<String>

While this is nice in that it removes repetition and reduces boilerplate slightly, the real benefits come from the ability to have variables with types that are impractical or impossible to represent.

Impractical Types

When transforming data it’s easy to be left with intermediary representations of the data that have deeply nested generic types.

Let’s steal an example from a c# linq query, that groups a customer’s orders by year and then by month.

While Java doesn’t have LINQ, we can get fairly close thanks to lambdas.

from ( customerList ) . select ( c -> tuple ( c. companyName ( ) , from ( c. orders ( ) ) . groupBy ( o -> o. orderDate ( ) . year ( ) ) . select ( into ( ( year, orders ) -> tuple ( year, from ( orders ) . groupBy ( o -> o. orderDate ( ) . month ( ) ) ) ) ) ) ) ; from(customerList) .select(c -> tuple( c.companyName(), from(c.orders()) .groupBy(o -> o.orderDate().year()) .select(into((year, orders) -> tuple( year, from(orders) .groupBy(o -> o.orderDate().month()) ))) ));

While not quite as clean as the c# version, it’s relatively similar. But what happens when we try to assign our customer order groupings to a local variable?

CollectionLinq < Tuple < String , CollectionLinq < Tuple < Integer , Group < Integer , Order >>>>> customerOrderGroups = from ( customerList ) . select ( c -> tuple ( c. companyName ( ) , from ( c. orders ( ) ) . groupBy ( o -> o. orderDate ( ) . year ( ) ) . select ( into ( ( year, orders ) -> tuple ( year, from ( orders ) . groupBy ( o -> o. orderDate ( ) . month ( ) ) ) ) ) ) ) ; CollectionLinq<Tuple<String, CollectionLinq<Tuple<Integer, Group<Integer, Order>>>>> customerOrderGroups = from(customerList) .select(c -> tuple( c.companyName(), from(c.orders()) .groupBy(o -> o.orderDate().year()) .select(into((year, orders) -> tuple( year, from(orders) .groupBy(o -> o.orderDate().month()) ))) ));

Oh dear, that type description is rather awkward. The Java solutions to this have tended to be one of

Define custom types for each intermediary stage—perhaps here we’d define a CustomerOrderGroup type.

Chaining many operations together—adding more transformations onto the end of this chain

Lose the type information

Now we don’t have to work around the problem, and can concisely represent our intermediary steps

var customerOrderGroups = from ( customerList ) . select ( c -> tuple ( c. companyName ( ) , from ( c. orders ( ) ) . groupBy ( o -> o. orderDate ( ) . year ( ) ) . select ( into ( ( year, orders ) -> tuple ( year, from ( orders ) . groupBy ( o -> o. orderDate ( ) . month ( ) ) ) ) ) ) ) ; var customerOrderGroups = from(customerList) .select(c -> tuple( c.companyName(), from(c.orders()) .groupBy(o -> o.orderDate().year()) .select(into((year, orders) -> tuple( year, from(orders) .groupBy(o -> o.orderDate().month()) ))) ));

Impossible Types

The above example was impractical to represent due to being excessively long and obscure. Some types are just not possible to represent without type inference as they are anonymous.

The simplest example is an anonymous inner class

var person = new Object ( ) { String name = "bob" ; int age = 5 ; } ; System . out . println ( person. name + " aged " + person. age ) ; var person = new Object() { String name = "bob"; int age = 5; }; System.out.println(person.name + " aged " + person.age);

There’s no type that you could replace “var” with in this example that would enable this code to continue working.

Combining with the previous linq-style query example, this gives us the ability to have named tuple types, with meaningful property names.

var lengthOfNames = from ( customerList ) . select ( c -> new Object ( ) { String companyName = c. companyName ( ) ; int length = c. companyName ( ) . length ( ) ; } ) ; lengthOfNames. forEach ( o -> System . out . println ( o. companyName + " length " + o. length ) ) ; var lengthOfNames = from(customerList) .select(c -> new Object() { String companyName = c.companyName(); int length = c.companyName().length(); }); lengthOfNames.forEach( o -> System.out.println(o.companyName + " length " + o.length) );

This also means it becomes more practical to create and use intersection types by mixing together interfaces and assigning to local variables

Here’s an example mixing together a Quacks and Waddles interface to create an anonymous Duck type.

public static void main ( String ... args ) { var duck = ( Quacks & Waddles ) Mixin :: create ; duck. quack ( ) ; duck. waddle ( ) ; } interface Quacks extends Mixin { default void quack ( ) { System . out . println ( "Quack" ) ; } } interface Waddles extends Mixin { default void waddle ( ) { System . out . println ( "Waddle" ) ; } } interface Mixin { void __noop__ ( ) ; static void create ( ) { } } public static void main(String... args) { var duck = (Quacks & Waddles) Mixin::create; duck.quack(); duck.waddle(); } interface Quacks extends Mixin { default void quack() { System.out.println("Quack"); } } interface Waddles extends Mixin { default void waddle() { System.out.println("Waddle"); } } interface Mixin { void __noop__(); static void create() {} }

This has more practical applications, such as adding behaviours onto existing types, ala extension methods

Encouraging Intermediary Variables

It’s now possible to declare variables with types that were erstwhile impractical or impossible to represent.

I hope that this leads to clearer code as it’s practical to add variables that explain the intermediate steps of transformations, as well as enabling previously impractical techniques such as the above.

A Russian translation of this post has been provided at Softdroid