Last active
May 25, 2025 00:00
-
-
Save gdejohn/b68fe1a322749a3f9661bfa583a412db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Modern Java: | |
/// - switch expressions | |
/// - sealed types | |
/// - record classes | |
/// - record patterns | |
/// - derived record creation | |
/// - value classes | |
/// - all values are objects | |
/// - universal generics | |
/// - concise method bodies | |
/// - local-variable type inference | |
/// - Markdown documentation comments | |
/// | |
/// Not shown: | |
/// - enhanced primitive boxing | |
/// - deconstruction patterns for arbitrary types | |
/// - primitive patterns | |
/// - constant patterns | |
/// - vararg patterns | |
/// - member patterns (static, instance, pattern lambdas/references) | |
/// - pattern matching in enhanced for-loops, lambdas, catch blocks | |
/// - exception handling in switch | |
/// - operator overloading | |
/// - serialization v2 | |
/// - flexible constructor bodies | |
/// - module imports | |
/// - compact source files | |
/// - nullable and null-restricted types | |
/// - specialization of generics over primitive and value types | |
/// - declaration-site variance | |
/// - arrays 2.0 | |
/// - 64-bit hash codes | |
/// - stream gatherers | |
/// - text blocks | |
/// - string templates | |
/// - virtual threads | |
/// - structured concurrency | |
/// - scoped values | |
/// - processor locals | |
/// - unwind-and-invoke | |
/// - condensers | |
/// - stable values | |
/// - AOT cache | |
/// - GraalVM layered native images | |
/// - Truffle | |
/// - Espresso | |
/// - continuation API | |
/// - JShell | |
/// - Flight Recorder | |
/// - Mission Control | |
/// - compact object headers | |
/// - thread-local, generational ZGC | |
/// - automatic heap sizing | |
/// - vector API | |
/// - foreign memory access | |
/// - foreign function calls | |
/// - code reflection | |
/// - class-file API | |
/// - integrity by default | |
/// - Java Platform Module System | |
import static java.lang.Math.*; // abs, atan2, cos, hypot, sin | |
/// Since this interface is sealed, the only allowed subtypes are the ones specified | |
/// below: Cartesian and Polar. | |
public sealed value interface Coordinates { | |
/// value classes: flat representation in memory, no identity, has the | |
/// flexibility of a class but the performance of a primitive type | |
value record Cartesian(double x, double y) implements Coordinates { | |
/// Convert these Cartesian coordinates into polar coordinates. | |
public Polar polar() -> new Polar(hypot(x, y), atan2(y, x)); | |
} | |
/// record means data class: automatically generated constructor, fields, getters, | |
/// equals, hashCode, toString, and deconstruction pattern | |
value record Polar(double r, double theta) implements Coordinates { | |
/// Convert these polar coordinates into Cartesian coordinates. | |
public Cartesian cartesian() -> new Cartesian(r * cos(theta), r * sin(theta)); | |
/// derived record creation | |
public Polar rotate(double a) -> this with { theta += a; }; | |
} | |
/// Universal generics, switch expressions, pattern matching (record, nested, var, | |
/// constant, unnamed), guards. Because the Coordinates interface is sealed, the | |
/// switch is exhaustive and no default case is needed. | |
static Stream<double> distances(Stream<Coordinates> stream) -> stream.map( | |
coordinates -> switch (coordinates) { | |
case Cartesian(var x, var y) -> hypot(x, y); | |
case Polar(0.0d, _) -> 0.0d; | |
case Polar(var r, _) when r > 0.0d -> r; | |
case Polar(var r, _) -> abs(r); | |
} | |
); | |
/// Concise method body with method reference. | |
static Stream<double> cartesianDistances(Stream<Cartesian> stream) = Coordinates::distances; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment