{"id":1143,"date":"2018-03-03T11:44:27","date_gmt":"2018-03-03T10:44:27","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=1143"},"modified":"2018-09-04T20:05:10","modified_gmt":"2018-09-04T19:05:10","slug":"representing-the-impractical-and-impossible-with-jdk-10-var","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/","title":{"rendered":"Representing the Impractical and Impossible with JDK 10 &#8220;var&#8221;"},"content":{"rendered":"<p class=\"lead\">Having benefited from &#8220;var&#8221; for many years when writing c#, I&#8217;m delighted that Java is at last getting support for <a href=\"http:\/\/openjdk.java.net\/jeps\/286\">local variable type inference<\/a> in JDK 10.<\/p>\n<p>From JDK 10 instead of saying <\/p>\n<pre lang=\"java\" escaped=\"true\">ArrayList&lt;String&gt; foo = new ArrayList&lt;String&gt;();\r\n<\/pre>\n<p>we can say<\/p>\n<pre lang=\"java\" escaped=\"true\">var foo = new ArrayList&lt;String&gt;();\r\n<\/pre>\n<p>and the type of &#8220;foo&#8221; is inferred as ArrayList&lt;String&gt;<\/p>\n<p>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 <strong>impractical or impossible to represent<\/strong>.<\/p>\n<h2>Impractical Types<\/h2>\n<p>When transforming data it&#8217;s easy to be left with intermediary representations of the data that have deeply nested generic types. <\/p>\n<p>Let&#8217;s steal an example from a <a href=\"https:\/\/code.msdn.microsoft.com\/LINQ-to-DataSets-Grouping-c62703ea#Nested\">c# linq query<\/a>, that groups a customer&#8217;s orders by year and then by month.<\/p>\n<p>While Java doesn&#8217;t have LINQ, we can get <a href=\"https:\/\/github.com\/benjiman\/lambda-integrated-query\/blob\/4cc7e36705372aba8682d793f38d311c262d78cd\/src\/test\/java\/com\/benjiweber\/linq\/for_collections\/Example.java#L47\">fairly close thanks to lambdas<\/a>. <\/p>\n<pre lang=\"java\" escaped=\"true\">from(customerList)\r\n    .select(c -&gt; tuple(\r\n        c.companyName(),\r\n        from(c.orders())\r\n            .groupBy(o -&gt; o.orderDate().year())\r\n            .select(into((year, orders) -&gt; tuple(\r\n                year,\r\n                from(orders)\r\n                    .groupBy(o -&gt; o.orderDate().month())\r\n            )))\r\n       ));\r\n<\/pre>\n<p>While not quite as clean as the c# version, it&#8217;s relatively similar. But what happens when we try to assign our customer order groupings <a href=\"https:\/\/github.com\/benjiman\/lambda-integrated-query\/blob\/e513e3a47727ab762a61342e1b3f36ff245ad3a3\/src\/test\/java\/com\/benjiweber\/linq\/for_collections\/Example.java#L47\">to a local variable<\/a>?<\/p>\n<pre lang=\"java\" escaped=\"true\">CollectionLinq&lt;Tuple&lt;String, CollectionLinq&lt;Tuple&lt;Integer, Group&lt;Integer, Order&gt;&gt;&gt;&gt;&gt; customerOrderGroups =\r\n   from(customerList)\r\n   .select(c -&gt; tuple(\r\n       c.companyName(),\r\n       from(c.orders())\r\n           .groupBy(o -&gt; o.orderDate().year())\r\n           .select(into((year, orders) -&gt; tuple(\r\n               year,\r\n               from(orders)\r\n                   .groupBy(o -&gt; o.orderDate().month())\r\n           )))\r\n   ));\r\n<\/pre>\n<p>Oh dear, that type description is rather awkward. The Java solutions to this have tended to be one of <\/p>\n<ul>\n<li>Define custom types for each intermediary stage&mdash;perhaps here we&#8217;d define a CustomerOrderGroup type.<\/li>\n<li>Chaining many operations together&mdash;adding more transformations onto the end of this chain<\/li>\n<li>Lose the type information<\/li>\n<\/ul>\n<p>Now we don&#8217;t have to work around the problem, and can <a href=\"https:\/\/github.com\/benjiman\/lambda-integrated-query\/blob\/4c77cc371136ca15ca9679ef4cc830090d84d311\/src\/test\/java\/com\/benjiweber\/linq\/for_collections\/Example.java#L47\">concisely represent<\/a> our intermediary steps<\/p>\n<pre lang=\"java\" escaped=\"true\">var customerOrderGroups =\r\n   from(customerList)\r\n   .select(c -> tuple(\r\n       c.companyName(),\r\n       from(c.orders())\r\n           .groupBy(o -&gt; o.orderDate().year())\r\n           .select(into((year, orders) -&gt; tuple(\r\n               year,\r\n               from(orders)\r\n                   .groupBy(o -&gt; o.orderDate().month())\r\n           )))\r\n   ));\r\n<\/pre>\n<h2>Impossible Types<\/h2>\n<p>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.<\/p>\n<p>The simplest example is an anonymous inner class<\/p>\n<pre lang=\"java\" escaped=\"true\">var person = new Object() {\r\n   String name = \"bob\";\r\n   int age = 5;\r\n};\r\n\r\nSystem.out.println(person.name + \" aged \" + person.age);\r\n<\/pre>\n<p>There&#8217;s no type that you could replace &#8220;var&#8221; with in this example that would enable this code to continue working.<\/p>\n<p>Combining with the previous linq-style query example, this gives us the ability to have named tuple types, with meaningful property names.<\/p>\n<pre lang=\"java\" escaped=\"true\">var lengthOfNames  =\r\n    from(customerList)\r\n        .select(c -> new Object() {\r\n            String companyName = c.companyName();\r\n            int length = c.companyName().length();\r\n        });\r\n\r\nlengthOfNames.forEach(\r\n    o -> System.out.println(o.companyName + \" length \" + o.length)\r\n);\r\n<\/pre>\n<p>This also means it becomes more practical to create and use intersection types by <a href=\"http:\/\/benjiweber.co.uk\/blog\/2015\/08\/07\/anonymous-types-in-java\/\">mixing together interfaces<\/a> and assigning to local variables<\/p>\n<p>Here&#8217;s an example mixing together a Quacks and Waddles interface to create an anonymous Duck type.<\/p>\n<pre lang=\"java\" escaped=\"true\">public static void main(String... args) {\r\n   var duck = (Quacks & Waddles) Mixin::create;\r\n   duck.quack();\r\n   duck.waddle();\r\n}\r\n\r\ninterface Quacks extends Mixin {\r\n   default void quack() {\r\n       System.out.println(\"Quack\");\r\n   }\r\n}\r\n\r\ninterface Waddles extends Mixin {\r\n   default void waddle() {\r\n       System.out.println(\"Waddle\");\r\n   }\r\n}\r\n\r\ninterface Mixin {\r\n   void __noop__();\r\n   static void create() {}\r\n}\r\n<\/pre>\n<p>This has more practical applications, such as <a href=\"https:\/\/gist.github.com\/benjiman\/a8945f378691f4c1d258a12bed825ec2#file-example-java-L24\">adding behaviours onto existing types<\/a>, ala <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/classes-and-structs\/extension-methods\">extension methods<\/a><\/p>\n<h2>Encouraging Intermediary Variables<\/h2>\n<p>It&#8217;s now possible to declare variables with types that were erstwhile impractical or impossible to represent. <\/p>\n<p>I hope that this leads to clearer code as it&#8217;s practical to add variables that explain the intermediate steps of transformations, as well as enabling previously impractical techniques such as the above.<\/p>\n<hr\/>\n<p style=\"padding: 20px 0 40px 0\">\n<a rel=\"nofollow\" href=\"http:\/\/softdroid.net\/predstavlenie-nepraktichnogo-i-nevozmozhnogo-s-jdk-10-var\">A Russian translation of this post has been provided at Softdroid<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having benefited from &#8220;var&#8221; for many years when writing c#, I&#8217;m delighted that Java is at last getting support for local variable type inference in JDK 10. From JDK 10 instead of saying ArrayList&lt;String&gt; foo = new ArrayList&lt;String&gt;(); we can say var foo = new ArrayList&lt;String&gt;(); and the type of &#8220;foo&#8221; is inferred as ArrayList&lt;String&gt;&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/\" class=\"more-link\" title=\"Read Representing the Impractical and Impossible with JDK 10 &#8220;var&#8221;\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Representing the Impractical and Impossible with JDK 10 &quot;var&quot; - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Having benefited from &#8220;var&#8221; for many years when writing c#, I&#8217;m delighted that Java is at last getting support for local variable type inference in JDK 10. From JDK 10 instead of saying ArrayList&lt;String&gt; foo = new ArrayList&lt;String&gt;(); we can say var foo = new ArrayList&lt;String&gt;(); and the type of &#8220;foo&#8221; is inferred as ArrayList&lt;String&gt;... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-03T10:44:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-04T19:05:10+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/\",\"name\":\"Representing the Impractical and Impossible with JDK 10 \\\"var\\\" - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"datePublished\":\"2018-03-03T10:44:27+00:00\",\"dateModified\":\"2018-09-04T19:05:10+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2018\/03\/03\/representing-the-impractical-and-impossible-with-jdk-10-var\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1143"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=1143"}],"version-history":[{"count":38,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1143\/revisions"}],"predecessor-version":[{"id":1295,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/1143\/revisions\/1295"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=1143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=1143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=1143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}