Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created March 3, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/4219232a7855f7a6418e4772a191b849 to your computer and use it in GitHub Desktop.
Save mcupak/4219232a7855f7a6418e4772a191b849 to your computer and use it in GitHub Desktop.
Export of my JShell session from the Local variable type inference - Will it compile? talk at ConFoo 2020.
var x = "hello"
/v x
var list = new ArrayList<String>()
/v list
int x
var x
var x = null
var x = (Integer) null
int x=0, y=0
var x=0, y=0
int inc(int x) { return x+1; }
int inc(var x) { return x+1; }
var inc(int x) { return x+1; }
for (var i=0; i<100; i++) {}
for (var i:list) {}
short x = 1
var x = 1
/v x
var x = 0x12345678
/v x
var x = 0x123456789
var x = 0x123456789l
/v x
class MyClass { var x = 1; }
var var = 1
/v var
void var() {}
class var {}
class Var {}
enum MyEnum{ var }
var[] x = {1,2}
var x = {1,2}
var x = {}
var x = new int[]{1,2}
try {} catch (Exception ex) {}
try { var x = 1; } catch (Exception ex) {}
try {} catch (var ex) {}
Supplier = () -> ""
Supplier s = () -> ""
var s = () -> ""
Consumer c = (x) -> System.out.println(x)
Consumer c = (var x) -> System.out.println(x)
BiConsumer c = (var x, var y) -> System.out.println(x)
BiConsumer c = (var x, y) -> System.out.println(x)
BiConsumer c = (var x, Integer y) -> System.out.println(x)
Consumer c = System.out::println
var c = System.out::println
var list = new ArrayList<String>()
var list = new ArrayList<>()
/v list
var list = new ArrayList<>(Set.of(1))
/v list
var list = new ArrayList()
/v list
var list1 = new ArrayList<Integer>()
list1.add(1)
/v list1
var x = list1.get(0)
/v x
list1.add(x)
List<?> list2 = list1
/v list2
var x = list2.get(0)
list2.add(x)
/v x
var x = new Object() { int a = 1; int b = 2; }
x.a
/v x
Object x = new Object() { int a = 1; int b = 2; }
x.a
<T extends Number & Comparable> T makeIntersectionType() {return null; }
var x = makeIntersectionType()
/v x
int x = 0
switch (x) {case 0-> 0; default -> 1; }
var y = switch (x) {case 0-> 0; default -> 1; }
var y = switch (x) {case 0-> 0; default -> "hello"; }
/v y
var y = (x==0) ? 0 : "hello"
/v y
var y = (x==0) ? 0 : null
/v y
var y = (x==0) ? null : null
switch (x) { default -> System.out.println("hello"); }
var y = switch (x) { default -> System.out.println("hello"); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment