jep 286 local-variable type inference - ted's tool time
TRANSCRIPT
Ted’s Tool TimeTed VinkeFirst8
JEP 286 Local-Variable Type
Inference
March 2016
Updated
●JEP 269: Convenience Factory Methods for Collections(Updated 2016/02/26, Created 2014/06/26)
●JEP 286: Local-Variable Type Inference(Updated 2016/03/09, Created 2016/03/08)
JEP 269: Convenience Factory Methods for Collections (1)
Define library APIs to make it convenient to create instances of collections and maps with small numbers of elements, so as to ease the pain of not having collection literals in the Java programming language. http://openjdk.java.net/jeps/269
// small, unmodifiable collectionSet<String> set = new HashSet<>();set.add("a");set.add("b");set.add("c");set = Collections.unmodifiableSet(set);
// copy constructor from another collectionSet<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
// double braceSet<String> set = Collections.unmodifiableSet(new HashSet<String>() {{ add("a"); add("b"); add("c");}});
// Java 8Set<String> set = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));
JEP 269: Convenience Factory Methods for Collections (2)
Define library APIs to make it convenient to create instances of collections and maps with small numbers of elements, so as to ease the pain of not having collection literals in the Java programming language. http://openjdk.java.net/jeps/269
// provide static factory methods on the List, Set, and Map interfaces // for creating unmodifiable instances of those collections
Set<String> set = Set.of("a", "b", "c");List.of(a, b, c);// Maps up to 10 entiresMap.of()Map.of(k1, v1)Map.of(k1, v1, k2, v2)Map.of(k1, v1, k2, v2, k3, v3)
Map.ofEntries(Map.Entry<K,V>...)Map.ofEntries( entry(k1, v1), entry(k2, v2), entry(kn, vn));
Alternatives
Language changes have been considered several times, and rejected:
1. Project Coin Proposal, 29 March 2009
2. Project Coin Proposal, 30 March 2009
3. JEP 186 discussion on lambda-dev, January-March 2014
Also rejected: static factories on conrete classes
Project Coin: Collection Literals
List<Integer> digits = [ 1, 2, 3 ];Set<Integer> primes = { 2, 7, 31 };
New concrete classes returned!
● Not exposed in public API - no guarantees of runtime type or identity
● Optimized for runtime space● Serialization proxy
JEP 286: Local-Variable Type Inference (1)
Enhance the Java Language to extend type inference to declarations of local variables with initializers.
JEP 286
http://openjdk.java.net/jeps/286
var list = new ArrayList<String>(); // infers ArrayList<String>var stream = list.stream(); // infers Stream<String>
int maxWeight = blocks.stream() .filter(b -> b.getColor() == BLUE) .mapToInt(Block::getWeight) .max();
var path = Path.of(fileName);var fileStream = new FileInputStream(path);var bytes = Files.readAllBytes(fileStream);
Syntax choices
var, val, auto, const, final, let, def...
Simplified error messages
Main.java:81: error: cannot infer type for local variable x var x; ^ (cannot use 'val' on variable without initializer)
JEP 286: Local-Variable Type Inference (2)
Take the survey!
https://www.surveymonkey.com/r/KGPTHCG
Syntax choices
var, val, auto, const, final, let, def...
References
●JEP 269: Convenience Factory Methods for Collectionshttp://openjdk.java.net/jeps/269
●JEP 286: Local-Variable Type Inferencehttp://openjdk.java.net/jeps/286
●Survey about JEP 286: Local-Variable Type Inferencehttps://www.surveymonkey.com/r/KGPTHCG
Thank you