About me ● Edoardo Vacchi – @evacchi – Language Implementation Frameworks @ UniMi (+Mate) – Distributed and High-Performance Streaming @ UniCredit R&D – Currently Drools & JBPM team @ Red Hat
Making a new Point() public class Point { public double x; public double y; public Point(double x_, double y_) { x = x_; y = y_; } } var p = new Point(1.0, 2.0); Point[] ps = { new Point(1.0, 2.0), new Point(1.1, 2.1), new Point(2.0, 3.0), };
Making a new Point() public class Point { public double x; public double y; public Point(double x_, double y_) { x = x_; y = y_; } } var p = new Point(1.0, 2.0); Point[] ps = new Point[] { new Point(1.0, 2.0), new Point(1.1, 2.1), new Point(2.0, 3.0), }; http://bit.ly/java-struct
Control over layout ● Objects are 8 bytes aligned in memory (address A is K aligned if A % K == 0) ● All felds are type-aligned (long/double is 8 aligned, integer/foat 4, short/char 2) ● Fields are packed in the order of their size, except for references (last) Nitsan Wakart - Know Thy Java Object Memory Layout http://psy-lob-saw.blogspot.it/2013/05/know-thy-java-object-memory-layout.html
Valhalla ● Codes like a class, works like an int ● Allow representation of ubiquitous types currently not well supported on the JVM, including complex numbers, vector values, and tuples. ● Avoid boxed types in generics
Value-based classes ● fnal and immutable ● equals, hashCode, and toString solely from the instance's state and not from its identity ● no use of identity-sensitive operations such as reference equality (==) between instances, identity hash code of instances, or synchronization on an instances's intrinsic lock; ● equal solely based on equals(), not based on reference equality (==); ● no accessible constructors, instead instantiated through factory methods which make no committment as to the identity of returned instances; ● freely substitutable when equal
Values ● Pure data aggregates – Can’t extend other classes, cannot be extended – Can’t have felds of the type being declared (no recursion!) – Structural equality, not identity – No wait/notify – All fnal felds – Non-nullable
Making a Point() @jvm.internal.value.ValueCapableClass final class Point { final double x; final double y; public Point(double x_, double y_) { x = x_; y = y_; } }
But really List names = Collections.emptyList(); List names = new ArrayList/>(); Consumer consumer = s /> System.out.println(s); Consumer consumer = System.out/:println;
Pattern Matching String formatted = exprswitch (obj) { case Integer i /> String.format("int %d", i); case Byte b /> String.format("byte %d", b); case Long l /> String.format("long %d", l); case Double d /> String.format(“double %f", d); default /> String.format("String %s", s); };
Data Classes record Point(int x, int y) { } // desugars to final class Point extends java.lang.DataClass { final int x; final int y; public Point(int x, int y) { this.x = x; this.y = y; } // destructuring pattern for Point(int x, int y) // state-based equals, hashCode, and toString // public read accessors x() and y() }
Data Classes interface Shape { } record Point(int x, int y); record Rect(Point p1, Point p2) implements Shape; record Circle(Point center, int radius) implements Shape;
Panama – Header fle extraction tools (Groveler) – Layout description language (LDL, Little Language) – Access to structured data on and off-heap – Native function calling from JVM to C/C++ – Intrinsic types to leverage CPU vector instructions
public class X { public X(T t) {} public T get() { return null; } public static int f(String s) { return 1; } public static int f(Object o) { return 2; } public static void main(String[] args) { f(new X/>("").get()); // 1 f(new X("").get()); // 2 } } http://mail.openjdk.java.net/pipermail/coin-dev/2011-February/003082.html http://tronicek.blogspot.it/2011/03/do-we-really-need-in-diamond-operator.html
If we were to add reifed generics to Java, it is very likely that we will have to support runtime raw types (!!) for backwards compatibility – so the syntactic distinction between diamond and raw turns out to be quite sweet. http://mail.openjdk.java.net/pipermail/coin-dev/2011-February/003083.html