HN
Today

Rethinking Syntax: Binding by Adjacency

This article introduces 'binding expressions,' a novel concept within the Manifold compiler plugin for Java that treats adjacency as a type-directed binary operator. It allows developers to create powerful, type-safe domain-specific languages (DSLs) directly within Java without modifying core types or resorting to macros. The innovation challenges traditional parsing by enabling structured, compile-time reduction of adjacent expressions, offering a peek into a more expressive future for static languages.

16
Score
2
Comments
#12
Highest Rank
5h
on Front Page
First Seen
Mar 9, 7:00 PM
Last Seen
Mar 9, 11:00 PM
Rank Over Time
1214131520

The Lowdown

The Manifold compiler plugin for Java proposes a radical rethink of syntax with 'binding expressions,' where the mere adjacency of elements is treated as a binary operator. This allows for the creation of highly expressive and type-safe domain-specific languages (DSLs) and literal grammars directly within Java.

  • Adjacency as an Operator: The core idea is to interpret adjacent expressions, like '2025 July 19,' as a single, compilable unit. The compiler intelligently resolves these pairings based on static types.
  • How it Works: Types can define prefixBind or postfixBind methods. When adjacent expressions are encountered, the compiler attempts to reduce them by calling these methods, applying a systematic search with backtracking if an initial binding path fails.
  • Type-Directed DSLs: This mechanism enables building DSLs for units (299.8M m/s), natural language-like expressions (Meet Alice Tuesday at 3pm), or ranges (1 to 10), all with compile-time type safety.
  • Non-Invasive Design: Crucially, this approach avoids modifying base types. The binding logic resides in adjacent types, keeping core classes clean and allowing for domain-specific semantics to be layered on top.
  • Real-World Use Cases: Manifold itself leverages binding expressions for its science library (units and vector math) and enhanced collections (ranges), demonstrating its practical utility.
  • Trade-offs: The approach introduces parsing complexity (a two-stage problem), presents a tension between flexibility and discipline in type system boundaries, and may have an initial cognitive overhead for developers accustomed to conventional Java syntax.
  • Experimental Status: While part of Manifold for several years, binding expressions are still considered experimental, with potential future enhancements like improved formatting rules and support for separators or statements.

By pushing the boundaries of what's possible with static type systems, binding expressions offer a compelling vision for more concise and domain-expressive Java code, albeit with a learning curve that asks developers to rethink fundamental syntactic paradigms.