ADR 0136: Tuple Literal Expressions

Status

Implemented (2026-10-20)

Context

The problem

{a, b} is accepted as a pattern, in match: arms and in destructuring assignment ({k, v} := pair), but there has been no tuple literal expression. A tuple value had to be built with Tuple withAll: #(...), which allocates a list and dispatches a message.

FFI-heavy code builds Erlang option lists all the time. A gen_tcp listener looks like this today:

(Erlang gen_tcp) listen: port with: #(
  #binary,
  Tuple withAll: #(#active, false),
  Tuple withAll: #(#reuseaddr, true),
  Tuple withAll: #(#backlog, 1024))

Thousand Island's Transport.resolve_options/1 merges required, user and default option lists. The Catalina port (BT-3957) does the same in TransportOptions over about ten such tuples, and every setopts: call site adds another one.

What earlier decisions said

Constraints

Decision

{e1, e2, ...} is a primary expression that evaluates to a raw Erlang tuple (class Tuple). It is the expression twin of the existing tuple pattern.

Grammar

primary      ::= ... | tupleLiteral
tupleLiteral ::= '{' [ element { ',' element } [ ',' ] ] '}'
element      ::= keywordExpr     // same level as #( ), #[ ], #{ } values

{a, b} := x versus {a, b} size at the start of a statement

parse_assignment used to treat every leading { as a destructuring pattern. It now uses bounded token lookahead:

  1. At a leading {, scan forward to the matching }, counting { and #{ against } and stopping at Eof.
  2. If the token after that } is :=, parse a destructure with the full pattern grammar.
  3. Otherwise parse an ordinary expression, which reaches the new tuple-literal arm of parse_primary.

Consequences of the rule:

String interpolation

The lexer still rejects an opening "{{". Writing {{ for a literal brace is a far more common mistake than wanting to interpolate a tuple. To interpolate a tuple literal, add a space: "pair: { {a, b} }". Only an opening {{ is special; a closing }} already nests correctly. The doubled-brace error message now says how to interpolate a tuple literal, and the unparser pads the interpolation when an interpolated expression starts with {, so formatting round-trips.

Semantics, codegen and types

Examples

(Erlang gen_tcp) listen: port with: #(#binary, {#active, false}, {#reuseaddr, true}, {#backlog, 1024})
opts := #({#local, path}, {#nodelay, true},)        // trailing comma OK
pair := {key, dict at: key}                          // keyword-send element
{k, v} := pair                                       // destructure (unchanged)
{a, b} := {b, a}                                     // swap
{a, b} size                                          // => 2 (statement-start expression)
single := {#ok}                                      // 1-tuple
empty := {}                                          // 0-tuple
"got { {a, b} }"                                     // interpolate a tuple: note the spaces

Errors on misuse

x := {a. b}
     ^ Expected ',' or '}' in tuple literal, found .
       hint: tuple elements are separated by ',': write `{a, b}`, not Smalltalk's `{a. b}`

x := {1, 2
          ^ Expected '}' to close tuple literal

"{{a, b}}"
 ^ doubled {{ }} in string literal is not interpolation — use \{ and \} for literal braces;
   to interpolate a tuple literal write { {a, b} }

Prior Art

User Impact

Steelman Analysis

The main tension is between Smalltalk familiarity (braces as Array) and BEAM fidelity (braces as tuple). Beamtalk already gives Array its own literal, so braces follow Erlang.

Alternatives Considered

A #-prefixed tuple literal (#t(...), #<...>)

Rejected: #{ is taken by maps, the pattern is bare {...}, and the docs already promised bare braces. See the steelman above.

Smalltalk brace arrays {a. b}

Rejected: . separates statements, all other collection literals use ,, and Pharo braces build an Array rather than a tuple.

Desugar to Tuple withAll: #(...) in the parser

Rejected: the formatter would rewrite user source into the long form, and it costs a list allocation and a message send at run time.

Parse as an expression, then convert to a pattern on :=

Rejected: it loses x :: T, binary and map sub-patterns in destructuring.

Disallow {x} and {}

Rejected: both are valid Erlang terms that FFI needs ({} appears in some OTP option APIs), and the pattern grammar already accepts both.

Consequences

Positive

Negative

Neutral

Implementation

Migration Path

No migration is needed. Existing {...} := expr destructuring parses as before. Source that previously failed with "Tuple literals are not valid expressions" now compiles. Replacing Tuple withAll: #(...) with literals in docs, tests and examples is tracked as BT-3973.

References