-
Notifications
You must be signed in to change notification settings - Fork 81
/
VariableTypeInference.java
169 lines (137 loc) · 3.95 KB
/
VariableTypeInference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package dev.nipafx.demo.java10.lang.var;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class VariableTypeInference {
// var can only be used for local variables; not for fields
// private var foo = "";
public static void main(String[] args) throws Exception {
simpleVarExample();
loopExample();
tryExample();
powerfulInference();
var();
}
/*
* WORKS
*/
private static void simpleVarExample() throws IOException {
var nipafx = new URL("https://nipafx.dev");
// connection needs to be closed but not in the demo
var connection = nipafx.openConnection();
var reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
reader.lines()
.limit(3)
.map(s -> s + " ")
.forEach(System.out::println);
}
private static void loopExample() {
var numbers = List.of("a", "b", "c");
for (var nr : numbers)
System.out.print(nr + " ");
for (var i = 0; i < numbers.size(); i++)
System.out.print(numbers.get(i) + " ");
System.out.println();
}
private static void tryExample() {
try (var file = new FileInputStream(new File("no-such-file"))) {
new BufferedReader(new InputStreamReader(file))
.lines()
.forEach(System.out::println);
} catch (IOException ex) {
// at least, we tried
System.out.println("There's actually no `no-such-file`. :)");
}
}
private static void powerfulInference() {
// may be inferred
// anonymous class types
var strings = new ArrayList<String>() {
public int tenSize() {
return 10 * size();
}
};
strings.add("1");
System.out.format("TenSize of %s is %d.%n", strings, strings.tenSize());
// intersection types
CharSequence chars = "c";
Comparable comp = "c";
chars.length();
comp.compareTo(null);
// CharSequence & Comparable s = createString("s");
var s = createString("c");
s.length();
s.compareTo(null);
}
private static <T extends CharSequence & Comparable> T createString(String string) {
return (T) string;
}
private static void var() {
// var is still allowed as variable and method names, just not as class name
var var = "var";
}
/*
* DOESN'T WORK
*
* Commented out code doesn't compile
*/
// var can only be used for local variables; not in a method signature
// private var getString() {
// return "foo";
// }
private void miscErrors() {
// From JEP 286:
// "Rejecting locals without initializers narrows the scope of the feature,
// avoiding "action at a distance" inference errors, and only excludes a
// small portion of locals in typical programs."
// "lack initializers"
String s;
// var s;
String f;
// var f;
f = "Foo";
// "declare multiple variables"
int foo = 0, bar = 1;
// var fuu = 0, baar = 1;
// "have extra array dimension brackets"
int ints[] = new int[0];
// var ints[] = new int[0];
// lambda variables (works on Java 11+)
// Function<String, String> appendSpace = (var a) -> a + " ";
}
private void polyTypeErrors() {
// array literals
int[] intses = { 0, 1, 2 };
// var intses = {0, 1, 2};
// lambdas
Function<String, String> appendSpace = a -> a + " ";
// var appendSpace = a -> a + " ";
}
private void inferenceAtADistance() {
// "List" case:
// cross fingers that compiler infers List<User>
var strings = new ArrayList<String>();
// but it doesn't, so this is a compile error:
// strings = new LinkedList<String>();
// other case:
// inferred as `int`
var id = 123;
if (id < 100) {
// very long branch; unfortunately
// not its own method call
} else {
// oh boy, much more code...
}
// now we append this line:
// id = "124";
// if this would compile, `id` needed to be a common supertype of int and String,
// which doesn't work with `<`, so the compile error would be far away in the `if` above
}
}