1+ package io .github .ingvarc .jdk10 ;
2+
3+ import java .io .*;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
6+ import java .util .function .BiFunction ;
7+ import java .util .function .Function ;
8+
9+ /**
10+ * Experiments with Local-Variable Type Inference.
11+ */
12+ public class LocalVariableTypeInference {
13+
14+
15+ // Type Inference can only be used for local variables, NOT for class fields
16+ // private var number = 125; // DOESN'T compile
17+
18+
19+ // type inference cannot be used for method parameters
20+ // void greeting(var name) { // DOESN'T compile
21+ // System.out.println("Hello, my name is " + name);
22+ // }
23+
24+
25+ // var is allowed as variable and method name
26+ private static void var () {
27+ var var = "var" ;
28+ }
29+
30+
31+ // var is not allowed as class name
32+ // private class var{} // DOESN'T compile
33+
34+
35+ // var cannot be used in a method signature
36+ // private var getString() {
37+ // return "foo";
38+ // }
39+
40+
41+ public static void main (String ... args ) throws IOException {
42+
43+ // simple type inference
44+ var number = 125 ;
45+ var text = "some text" ;
46+ var character = 'A' ;
47+
48+ System .out .println (number );
49+ System .out .println (text );
50+ System .out .println (character );
51+
52+
53+ // multiple variables
54+ int foo = 0 , bar = 1 ;
55+ // var foo = 0, bar = 1; // DOESN'T COMPILE
56+
57+
58+ // `var` is not allowed as an element type of an array
59+ int intArray [] = new int [0 ];
60+ // var intArray[] = new int[0]; // DOESN'T COMPILE
61+
62+
63+ // `var` is not allowed with array initializer
64+ long [] longArray = {1L , 2L , 3L };
65+ // var longArray = { 1L, 2L, 3L }; // DOESN'T COMPILE
66+
67+
68+ // compiler infers ArrayList<String> instead of List<String>
69+ var strings = new ArrayList <String >();
70+ // so this is a compile error
71+ // strings = new LinkedList<String>(); // DOESN'T COMPILE
72+
73+
74+ // type inference from return type
75+ var name = getName ();
76+ var age = getAge ();
77+
78+ System .out .println (name );
79+ System .out .println (age );
80+
81+
82+ // type inference from parameterised type
83+ var authors = new ArrayList <Author >();
84+ authors .add (new Author ("Walter Isaacson" , 65 ));
85+ authors .add (new Author ("Joanne Rowling" , 52 ));
86+
87+
88+ // type inference in loops
89+ for (var author : authors ) {
90+ System .out .println (author .getName () + " " + author .getAge ());
91+ }
92+
93+ var numbers = List .of ("one" , "two" , "three" );
94+ for (String aNumber : numbers ) {
95+ System .out .println (aNumber );
96+ }
97+
98+
99+ // type inference in try-with-resources statement
100+ try (var file = new FileInputStream (new File ("non-existent-file" ))) {
101+ new BufferedReader (new InputStreamReader (file ))
102+ .lines ()
103+ .forEach (System .out ::println );
104+ } catch (IOException ex ) {
105+ System .out .println ("There's actually no `non-existent-file`" );
106+ }
107+
108+
109+ // CharSequence & Comparable comparableCharSequence = getComparableCharSequence("s");
110+ var comparableCharSequence = getComparableCharSequence ("The Force will be with you. Always." );
111+ System .out .println (comparableCharSequence .length ());
112+ System .out .println (comparableCharSequence .compareTo ("" ));
113+
114+
115+ // type inference with non-denotable types
116+ Object luke = new Object () {
117+ String name = "Luke Skywalker" ;
118+ int age = 53 ;
119+ };
120+ // System.out.println(luke.name + " " + luke.age); // DOESN'T COMPILE
121+
122+ var han = new Object () {
123+ String name = "Han Solo" ;
124+ int age = 63 ;
125+ };
126+ System .out .println (han .name + " " + han .age );
127+
128+
129+ // Illegal or impractical use of type inference
130+
131+ // type inference variables must be initialized straight away
132+ // var name; // DOESN'T COMPILE
133+ // name = "Luke"; // DOESN'T COMPILE
134+
135+ // Lambdas must always declare an explicit type
136+ BiFunction <Integer , Integer , Integer > add = (x , y ) -> x + y ;
137+ // var add = (x, y) -> x + y; // DOESN'T COMPILE
138+ // var nameSupplier = () -> "Luke"; // DOESN'T COMPILE
139+ // var nameFetcher = LocalVariableTypeInference::getName; // DOESN'T COMPILE
140+
141+ String message = "The Force will be with you. Always." ;
142+ Function <String , String > quotify = m -> "'" + message + "'" ;
143+ // var quotify = m -> "'" + message + "'"; // DOESN'T COMPILE
144+
145+
146+ // diamond operator influence
147+ List <Author > fantasyAuthors = new ArrayList <>(); // fantasyAuthors<Author>
148+ var fictionAuthors = new ArrayList <>(); // fictionAuthors<Object>
149+ }
150+
151+ private static <T extends CharSequence & Comparable <T >> T getComparableCharSequence (T text ) {
152+ return text ;
153+ }
154+
155+ private static String getName () {
156+ return "Luke Skywalker" ;
157+ }
158+
159+ private static int getAge () {
160+ return 53 ;
161+ }
162+
163+ private static class Author {
164+ private String name ;
165+ private int age ;
166+
167+ Author (String name , int age ) {
168+ this .name = name ;
169+ this .age = age ;
170+ }
171+
172+ String getName () {
173+ return name ;
174+ }
175+
176+ int getAge () {
177+ return age ;
178+ }
179+ }
180+ }
0 commit comments