Skip to content

Instantly share code, notes, and snippets.

@gdejohn
Last active March 26, 2024 03:42
Show Gist options
  • Save gdejohn/b68fe1a322749a3f9661bfa583a412db to your computer and use it in GitHub Desktop.
Save gdejohn/b68fe1a322749a3f9661bfa583a412db to your computer and use it in GitHub Desktop.
/// The future of Java:
/// - value classes
/// - all values are objects
/// - universal generics
/// - local-variable type inference
/// - records
/// - reconstructors
/// - sealed types
/// - switch expressions
/// - pattern matching
/// - concise method bodies
/// - Markdown documentation comments
///
/// Not shown:
/// - deconstruction patterns for arbitrary types
/// - serialization v2
/// - null-restricted types
/// - specialized generics
/// - declaration-site variance
/// - stream gatherers
/// - text blocks
/// - string templates
/// - virtual threads
/// - structured concurrency
/// - scoped values
/// - unwind-and-invoke
/// - processor locals
/// - condensers
/// - GraalVM native images
/// - Truffle
/// - Espresso
/// - thread-local, generational ZGC
/// - code reflection
/// - vector API
/// - foreign memory access
/// - foreign function calls
/// - class-file API
/// - 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 implements LooselyConsistentValue {
/**
* 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 {
public implicit Cartesian();
/** 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 {
public implicit Polar();
/** Convert these polar coordinates into Cartesian coordinates. */
public Cartesian cartesian() -> new Cartesian(r * cos(theta), r * sin(theta));
/** reconstructor */
public Polar rotate(double a) -> this with { theta += a; };
}
/**
* Universal generics, switch expressions, pattern matching (record, var,
* constant, any, nested), 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