ADR 0116: Double-Dispatch Coercion for Number-on-the-Left Arithmetic

Status

Implemented (2026-08-25) — Phases 1–4 landed (BT-3262–BT-3266, parent BT-2712); 2026-10-18 addendum follow-ups BT-3892, BT-3924, BT-3925 also Done. The 2026-11-25 addendum (comparison operators, BT-5176) is Accepted (2026-11-25); its runtime entry point send_number_comparison/4 and the reworded arithmetic hint (decision 4) landed in BT-5208, codegen (Phase 2) onward pending.

Context

ADR-less Phase 1 of the "operators as messages" epic (BT-2708) — implemented in BT-2709 and BT-2710 — made + - * / and < > <= >= dispatchable messages so a user value-type can overload them as the receiver: aMoney + bMoney works because Money defines +. The codegen shape (generate_binary_op in crates/beamtalk-core/src/codegen/core_erlang/operators.rs) is:

This is receiver-dispatch only, by design (BT-2709's explicit scope cut). It has no answer for 5 + aVector — a number on the left, a non-numeric value-type on the right. Concretely:

5 + aVector    "aVector is a Vector, not a Number"

Here the left operand is a numeric literal, so receiver_is_statically_numeric is true and codegen emits the bare erlang:'+'(5, X) with no guard at all. At runtime this crashes with a raw badarith — not even a does_not_understand, since the dispatch layer is never reached. The same gap exists for any receiver that is guarded but turns out numeric at runtime (guarded_op_doc's is_number branch also calls the bare BIF): whenever the left operand is a number, the right operand is assumed to be one too, and nothing catches the case where it isn't.

Constraints carried over from Phase 1

  1. The numeric fast path must stay zero-cost. BT-2709 measured guard-vs-bare overhead specifically to protect this; any fix here must not turn total + delta (right operand a plain numeric variable) into an always-guarded operation.
  2. Static typing must stay informed. BT-2709 deliberately avoided perform:-based dynamic dispatch so the type checker keeps a known return type at each call site (covariant-return refinement, ADR 0068). Any number-on-the-left mechanism must preserve that — a reflected hook needs a declared, checkable signature, not a string-keyed perform:.
  3. No scalar tower exists on BEAM. Beamtalk has no Fraction, ScaledDecimal, or Rational class — erlang:'+' already fuses int↔float natively. Classic Smalltalk "generality" (rank types on a total order, coerce the lower-ranked operand, retry) was built to solve exactly that mixing problem, which BEAM has already solved at the BIF level.
  4. Comparison operators are out of scope for this ADR. < > <= >= share the same operator-guard machinery (BT-2710) but fail differently: Erlang's < never raises, so 5 < aVector today silently term-orders instead of crashing — a worse failure mode (silent wrong answer vs. a crash) tracked as a separate follow-up, not designed here. (That follow-up is the 2026-11-25 addendum below, Accepted.)

Decision

Beamtalk adopts double dispatch via per-operator reflected methods — not a generality/coercion tower, and not a perform:-based reflective retry.

Reflected method protocol

A value-type that wants aNumber <op> self to work declares one reflected method per arithmetic operator it supports, using the <verb>FromNumber: naming convention:

Value subclass: Vector
  field: components :: Array = #()

  "receiver-dispatch: aVector + 5, aVector - 5"
  + other :: Number -> Vector => self collect: [:c | c + other]
  - other :: Number -> Vector => self collect: [:c | c - other]

  "number-on-the-left: 5 + aVector, 5 - aVector"
  plusFromNumber:  n :: Number -> Vector => self collect: [:c | n + c]
  timesFromNumber: n :: Number -> Vector => self collect: [:c | n * c]
  minusFromNumber: n :: Number -> Vector => self collect: [:c | n - c]

Operand order matters for non-commutative operators and is easy to get backwards: minusFromNumber:'s receiver (self) is the value that was on the right of the original expression, and its parameter (n) is the value that was on the left — 5 - aVector sends aVector minusFromNumber: 5, computing n - self (5 minus each component), not self - n. aVector - 5 (receiver-dispatch -, unchanged from BT-2709) computes the opposite: each component minus 5. The two methods above are not one implementation reused for both directions — the subtraction is genuinely reversed between them, same as divFromNumber:.

Each reflected method is an ordinary, fully-typed method — no perform:, no dynamic selector construction. There is exactly one reflected method per operator (plusFromNumber: / minusFromNumber: / timesFromNumber: / divFromNumber:), not the adaptToInteger: / adaptToFloat: / adaptToFraction: fan-out Pharo needs — because BEAM fuses int/float into a single effective Number category on the left, one hook per operator suffices. (divFromNumber: reflects /, Beamtalk's single division operator — distinct from the separately-named div: keyword message for integer division on Integer, so the shared "div" substring doesn't collide as a selector, just as a reading hazard worth a beat of attention.)

When an operator doesn't apply: implement it anyway, to reject with intent

Not every reflected operator makes sense for every type — 5 / aVector ("a number divided by a vector") rarely has an obvious meaning the way 5 * aVector (scale) or 5 + aVector (broadcast-add) do, and some types are point-like rather than vector-space-like (a Temperature sensibly supports 5 + aTemperature, an offset, but not 5 * aTemperature, which has no physical meaning). For those, the recommended pattern is to implement the method anyway, with a body that rejects explicitly:

divFromNumber: n :: Number -> Vector =>
  self error: "Cannot divide a number by a Vector — did you mean (aVector / n)?"

This is better than simply omitting the method. Omitting it leaves does_not_understand to fire on the synthesized selector ('divFromNumber:') — a name the caller never typed (they wrote 5 / aVector) — which is harder to connect back to their own code than an ordinary DNU is (see the dispatch-mechanism hint below, which narrows but doesn't eliminate this gap for methods left unimplemented). A deliberate self error: names the actual problem and, where relevant, points at the supported alternative. Practically, this means a type author who commits to number-on-the-left arithmetic at all will typically implement all four methods — some as real arithmetic, the rest as intentional rejections — not a variable subset; see § Consequences, Negative for what this means for the earlier boilerplate estimate.

Dispatch mechanism (codegen)

Trigger condition, refined. receiver_is_statically_numeric (BT-2709) already exists and takes an arbitrary Expression, not just the receiver — so the same check applies to the right operand for free. The mechanism below only engages when the left operand is (statically or dynamically) numeric and receiver_is_statically_numeric(right) is false — i.e. the right operand's type genuinely isn't known at compile time. When the right operand is statically numeric too (a literal, a :: Number-family param, self in Integer/Float, or a numeric/untyped field — the exact same rule already applied to the left operand), codegen skips this mechanism entirely and emits the bare BIF, identical to today. Concretely: total + delta where delta :: Integer — the common case this ADR must not tax — never enters a try at all; only a right operand whose type is genuinely unknown (5 + aVector) does. This also converts the "does the try cost anything on the happy path" question from "prove it's negligible everywhere" to "prove it's negligible on the strictly smaller set of already-dynamically-dispatched call sites" — the de-risking spike (§ Implementation) measured exactly that narrower question and found the cost comparable to the already-accepted is_number guard's on those same call sites.

For that remaining case, the call site wraps the BIF call in a try/ catch on badarith, following the same catch-clause shape already used throughout the codebase (control_flow/exception_handling.rs's on_do_catch_preamble: bind the raw type/error/stack as variables, case- match on them with an explicit fallback arm, re-raise via primop 'raw_raise', never erlang:raise/3 — which expects a pre-built stacktrace term, not the raw internal trace a catch clause binds). Both operands are let-bound before the try (mirroring guarded_op_doc's own left_var/right_var binding), so the catch handler can reference Right without re-evaluating it — referencing the inlined expression twice, the way the existing bare fast path embeds left_code/right_code directly, would double-evaluate a non-trivial right operand:

let BinLeft = <left operand> in
let BinRight = <right operand> in
try
    call 'erlang':'+'(BinLeft, BinRight)
of <TryResult> -> TryResult
catch <Type, Error, Stack> ->
    case {Type, Error} of
        <{'error', 'badarith'}> when 'true' ->
            case call 'erlang':'is_number'(BinRight) of
                <'true'> when 'true' ->
                    %% BinRight IS a number — badarith wasn't a coercion
                    %% miss (e.g. `5 / 0`, or float overflow). Re-raise
                    %% unchanged so the existing badarith classification
                    %% (ADR 0028/BT-2704) still handles it.
                    primop 'raw_raise'(Type, Error, Stack)
                <'false'> when 'true' ->
                    call 'beamtalk_message_dispatch':'send_number_coercion'(
                        BinRight, 'plusFromNumber:', [BinLeft], '+')
                %% is_number/1 is boolean-exhaustive — `erlc`'s own BIF
                %% return-type inference proves this arm unreachable (it
                %% warns "clause cannot match" if present, confirmed by the
                %% spike below) — but `guarded_op_doc`'s existing
                %% `case_clause_fallback` convention adds it anyway,
                %% defensively, rather than depending on that inference
                %% holding across OTP releases. `error({case_clause, _})`,
                %% not `raw_raise` — this is an internal-invariant guard, not
                %% a real exception to propagate.
                <NoMatch> when 'true' ->
                    call 'erlang':'error'({'case_clause', NoMatch})
            end
        %% Not badarith at all (shouldn't occur for this specific BIF call,
        %% but the case must be exhaustive). Binds a fresh throwaway
        %% variable, not `Type`/`Error` again — those are already bound by
        %% the enclosing `catch` clause and stay in scope for `raw_raise`
        %% below without needing to be re-destructured here, mirroring
        %% `on_do_catch_preamble`'s own catch-all arm.
        <OtherPair> when 'true' ->
            primop 'raw_raise'(Type, Error, Stack)
    end
end

Spike-verified (see Implementation § De-risking spike results): the try/catch/is_number/raw_raise shape — including the mandatory try ... of ... catch ... end form (Core Erlang, unlike Erlang source, requires the of clause explicitly; its absence is a syntax error) and the mandatory when guard on every case clause — was hand-compiled with erlc and confirmed to core_lint cleanly, then functionally exercised end-to-end for all three branches (happy-path add, non-numeric-right dispatch, and numeric-right re-raise via float overflow). That spike used a bare send/3 stub, predating the send_number_coercion/4 wrapper added below — the outer try/catch shape shown above is unchanged by that addition (still spike-verified), but send_number_coercion/4 itself is a new, not-yet-compiled function and should go through the same hand-compile- and-verify treatment before implementation, not be assumed correct by extension.

+ - * / can each raise badarith between two genuine numbers, not only when the right operand isn't a number: / on a zero divisor, and — empirically verified against the BEAM runtime, since Erlang floats have no IEEE infinity/NaN representation — + - * on float overflow (1.0e308 + 1.0e308 raises badarith rather than returning an infinite float). Naively catching every badarith and dispatching unconditionally would misroute 5 / 0 (divisor 0 is a number) to beamtalk_message_dispatch:send(0, 'divFromNumber:', [5]), which fails as does_not_understand: Integer does not understand 'divFromNumber:' — a regression of the existing, documented badarith → TypeError "bad arithmetic operation" classification (beamtalk_exception_handler:wrap_raw/2, ADR 0028), which correctly explains it as an arithmetic error today. The is_number(BinRight) check inside the catch handler distinguishes the two badarith causes generically, independent of which specific numeric failure triggered it: a non-numeric BinRight is a genuine coercion miss and dispatches to the reflected method; a numeric BinRight means the failure is a real numeric error unrelated to coercion (division by zero or float overflow), and primop 'raw_raise' re-raises it with its original class, reason, and stacktrace — identical to today's uncaught propagation — so it reaches the same classification layer unchanged regardless of which of the two numeric causes produced it. The catch block itself only ever runs on an actual badarith, so its cost is paid exclusively on the already-failing path; the try wrapper's cost on the non-failing path, restricted to call sites where the right operand's type is genuinely unknown was measured by the de-risking spike (§ Implementation) and found comparable to the already-accepted is_number guard's cost on those same call sites — not the zero-cost claim an unqualified reading of "happy path" might suggest, but the correct comparison given the compile-time skip already removes the true zero-cost call sites from this mechanism's reach entirely.

This is deliberately the one place BT-2709 avoided (catch-on-failure instead of a guard) — correct here specifically because the happy path (number + number) can never raise, unlike the general receiver-dispatch case where a non-numeric receiver is the common case a guard has to cover cheaply, and because the refined trigger condition above already removes the statically-known-numeric-right-operand cases a guard would otherwise have to cover for free.

Compiler-generated exception handling on Beamtalk's own state-threading constructs (loops, on:do:/ensure:, NLR boundaries, class-var shadow-writes) is required to lower through ThreadedIr and its verify() (CLAUDE.md, ADR 0111). This try/catch doesn't: it threads no state across the boundary — no class variable, no loop accumulator, no NLR relay — and lives entirely inside a single expression's evaluation, the same "general expression codegen stays AST-directed" category the existing Phase 1 is_number guard (guarded_op_doc, a plain case Document) is already in. It is built directly as a Document from the AST and generator state, with no ThreadedIr involvement, consistent with that existing guard and outside ThreadedIr's scope.

Hinting the DNU when the reflected method is missing

A plain beamtalk_message_dispatch:send(BinRight, 'plusFromNumber:', [BinLeft]) that finds no plusFromNumber: on BinRight's class produces an ordinary does_not_understand: "<Class> does not understand 'plusFromNumber:'". That's a real gap for a selector the caller never typed — unlike a normal DNU (foo bar → "Foo does not understand 'bar'", directly traceable to the caller's own source), plusFromNumber: is synthesized by this mechanism; someone who wrote 5 + aVector has no reason to already know that name.

Rather than teach the general DNU-formatting path (beamtalk_error.erl's generate_message/3 / maybe_enrich_dnu_hint/1) about this one synthetic selector family — those stay generic, by design, and have no way to recover "this was tried because of the + on line N" once the failure reaches them — the fix lives at the one place that still has that context: the coercion-dispatch call site itself. A new runtime helper, beamtalk_message_dispatch:send_number_coercion/4 (Right, Selector, Args, OrigOp), wraps the existing send/3:

send_number_coercion(Right, Selector, Args, OrigOp) ->
    RightClass = beamtalk_primitive:class_of(Right),
    try
        send(Right, Selector, Args)
    catch
        error:#{'$beamtalk_class' := _,
                error := #beamtalk_error{kind = does_not_understand,
                                          class = RightClass,
                                          selector = Selector} = Error}:Stack ->
            %% RightClass and Selector are already bound above/as this
            %% function's argument — the catch pattern re-uses them, so this
            %% only matches a does_not_understand for *this exact* class and
            %% selector, not any DNU that happens to propagate through.
            %% Hint wording as reworded by the 2026-11-25 addendum
            %% (decision 4); the original "~s has no '~s' — implement it to
            %% support 'number ~s ~s' arithmetic" read as advice to add a
            %% method to `nil`.
            Hint = unicode:characters_to_binary(io_lib:format(
                "'~ts' cannot combine a number with ~ts — the right operand "
                "must be a number, or a value whose class implements '~ts'",
                [OrigOp, RightClass, Selector])),
            HintedError = beamtalk_error:with_hint(Error, Hint),
            erlang:raise(error, beamtalk_exception_handler:wrap(HintedError), Stack)
    end.

This mirrors the real shape a DNU actually propagates as: beamtalk_error:raise/1 (the path every production DNU already goes through — e.g. raise_class_dnu/raise_class_self_dnu in beamtalk_class_dispatch.erl) wraps the #beamtalk_error{} via beamtalk_exception_handler:wrap/1 into a #{'$beamtalk_class' := ..., error := #beamtalk_error{...}} tagged map before calling erlang:error/1 — so a catch here has to match that wrapped shape, not the bare record, and the re-raise has to re-wrap the hinted error the same way to stay consistent with every other exception in the system.

Why match on both fields, not just the selector: if plusFromNumber: does exist on Right's class but its body calls something else that itself DNUs (an unrelated bug inside the user's own method), that inner failure has a different selector and must propagate unchanged — not get relabeled as "coercion hook missing" when the hook was actually present and something else broke. Same discipline § Dispatch mechanism already applies to badarith: narrow matching on the exact failure this call site can actually produce, not a broad catch-and-relabel of anything that happens to propagate through.

kind stays does_not_understand (RuntimeError) — this only adds a hint, the same field format/1 already renders for every DNU (§ REPL example below). No new exception class, no change to how on: RuntimeError do: or on: Error do: catch it.

REPL example

Using the project's .btscript // => assertion convention (the verified substring the REPL/eval error message actually contains — beamtalk_error:generate_message/3 for the DNU case, beamtalk_exception_handler:wrap_raw/2 for the badarith case; see tests/repl-protocol/cases/errors.btscript):

5 + aVector
// => Vector(6, 7, 8)

5 + "not a vector"
// => ERROR: String does not understand 'plusFromNumber:'
// => ERROR: '+' cannot combine a number with String — the right operand must be a number, or a value whose class implements 'plusFromNumber:'

aVector + 5
// => Vector(6, 7, 8)

5 / 0
// => ERROR: bad arithmetic operation

(Two // => lines shown for 5 + "not a vector" deliberately — the first is the bare DNU message every other DNU in this doc uses, the second is the added hint send_number_coercion/4 attaches to it, on the same format/1 line every DNU's hint already renders on: "<message>\nHint: <hint>". A real .btscript test asserts against the substring it needs, not both.)

What this ADR does not do

Prior Art

LanguageMechanismVerdict
Pharo/Squeak Smalltalkgenerality-ranked retryRelationalOp:coercing: / aNumber adaptToInteger: self andSend: #+, dispatched via perform:Rejected wholesale — the generality ladder assumes a total-ordered scalar tower we don't have, and non-tower types (Vector, Money) have no meaningful rank. The perform: retry is opaque to Beamtalk's type checker, which Phase 1 was specifically built to avoid.
Python__radd__/__rmul__ reflected dunder methods, tried automatically when the left operand's __add__ returns NotImplementedAdopted as the closest model — one statically-dispatchable method per operator, owned by the type that wants to participate, with no shared coercion ladder. Beamtalk's difference: the trigger is a caught badarith from the numeric BIF rather than a sentinel return value, since the numeric fast path never returns a sentinel — it's a bare BIF call.
GleamNo operator overloading at all; +/+. are fixed to Int/Float, mixed-type or custom-type arithmetic requires named functionsRejects the whole problem by design. Not viable for Beamtalk, which already committed to overloadable operators in BT-2709/2710 for value-type ergonomics (Money + Money, Vector + Vector).
Elixir/ErlangNo user-defined operator overloading; + is fixed to numbers, structs use named functions (Vector.add/2) or protocols (Numeric behaviour, via libraries)Confirms BEAM itself has no operator-dispatch story to inherit — Beamtalk's message-based operators are already a deliberate departure the epic (BT-2708) took on.
NewspeakNo numeric coercion protocol distinct from ordinary message dispatch — arithmetic is just messages, and mixed-type numeric towers are avoided by keeping the numeric class hierarchy shallowReinforces "keep it as ordinary dispatch" — supports treating plusFromNumber: as just another message rather than inventing new machinery.

User Impact

Steelman Analysis

Alternative: Generality/coercion tower

Alternative: perform:-based reflective retry (adaptTo:andSend:)

Alternative: Always-on runtime guard on the right operand's type

Tension points

Alternatives Considered

Generality/coercion tower (generality-ranked retry)

Rank every numeric-participating type on a total order and retry the operator after coercing the lower-ranked operand to the higher-ranked type's representation, à la Pharo Number>>retryRelationalOp:coercing:. Rejected: the tower this exists to order (SmallInteger < Fraction < Float < …) barely exists in Beamtalk (BEAM already fuses int/float at the BIF level, and there is no Fraction/ScaledDecimal/Rational), and the types this ADR actually needs to support (Vector, Money, Matrix) have no meaningful rank relative to Number — 5 + aVector isn't "promote 5 to a vector," it's "broadcast 5 across the vector," which a coerce-to-common-type model can't express.

perform:-based reflected dispatch (adaptToInteger:andSend:)

Re-invoke the operator dynamically via anObject adaptTo: self andSend: #+, internally using perform: aSymbol with: anArgument. Rejected: this is opaque to the type checker — Phase 1's entire design (BT-2709) exists to keep a static fast path and covariant-return inference (ADR 0068) working for arithmetic, and a perform:-based retry throws that guarantee away for exactly the call sites this ADR touches.

Always-on runtime guard on the right operand's type

Instead of catching badarith, emit a proactive runtime check on the right operand (e.g. is_number(Right)) alongside the existing left-operand guard unconditionally — every call site with a numeric left operand pays a right-operand check, regardless of whether the right operand's type is already known at compile time. Rejected in this unconditional form: the common case is total + delta — a numeric literal or known-numeric receiver on the left, and a right operand whose type is already visible to the compiler (a :: Number-family param, another numeric literal, a numeric/untyped field — the identical rule the left operand already uses). Guarding that right operand at runtime would regress it from a zero-cost bare BIF to an always-checked one, for information the compiler already has for free.

Not fully rejected, though — adopted as a compile-time skip instead of a runtime guard. The chosen design reuses exactly this idea (receiver_is_statically_numeric applied to the right operand), but as a codegen-time condition that removes the try/catch from total + delta-shaped call sites entirely, rather than as a runtime is_number check both operands pay on every call. Only a right operand whose type is genuinely unknown at compile time (5 + aVector) reaches the try/catch mechanism at all — see § Dispatch mechanism's "Trigger condition, refined." The remaining design question is narrower than the original framing suggests: not "guard vs. catch, always," but "guard vs. catch, only for the already-dynamically-dispatched residual" — and for that residual, the catch-on-badarith approach still only pays a cost when the fallback actually fires, which an always-on runtime guard would not.

Status quo — leave 5 + aVector unhandled

Do nothing: keep the receiver-dispatch-only behavior from BT-2709/2710 and document number-on-the-left arithmetic as unsupported. Rejected: it leaves the value-type ergonomics the epic (BT-2708) promised half-finished — aVector + 5 working but 5 + aVector crashing with a raw badarith is an asymmetry with no principled justification a user could discover short of hitting the crash, and "arithmetic operators are overloadable" becomes a claim with an asterisk. The cost of the fix (one codegen change, opt-in per type) is low enough that "leave it broken" doesn't clear the bar of a genuine alternative, only a fallback if the chosen design had turned out to be infeasible.

Extend reflection to comparison operators now

Apply the same <verb>FromNumber: pattern to < > <= >= in this ADR, since they share the guard machinery. Deferred, not rejected: comparison's failure mode (silent wrong term-ordering, since Erlang < never raises) has no badarith-style signal to catch, so it needs its own mechanism (likely a proactive is_object-style check on the right operand, mirroring BT-2710's existing left-operand guard) — different enough to warrant its own follow-up rather than folding it into this arithmetic-specific design. (Designed in the 2026-11-25 addendum below, Accepted.)

Consequences

Positive

Negative

Neutral

Implementation

Affected components: codegen, plus one small runtime addition (no parser, type-checker, or new runtime protocol/exception-class changes) — the type checker's existing check_binary_operand_types warning is left as-is, including its now-known false positive on newly-valid number-on-the-left code (§ Consequences, Negative), rather than folded into this ADR's scope. The runtime addition is beamtalk_message_dispatch:send_number_coercion/4 (§ Dispatch mechanism's DNU-hinting subsection) — a thin wrapper around the existing send/3, not a new dispatch mechanism or exception class.

De-risking spike (done — results below)

Mirroring BT-2709's own precedent for this exact call site, run before writing this ADR's Implementation section into real codegen:

1. Hand-written Core Erlang, compiled with erlc. The exact shape in § Dispatch mechanism was hand-written as a standalone .core module and compiled directly. Two real issues surfaced that the illustrative Erlang in earlier drafts of this ADR got wrong, both fixed in that section now:

With both fixed, the module compiles cleanly. core_lint passes; the only diagnostic is the expected one — erlc's own note that the inner is_number case's defensive third arm can't match, confirmed by removing that arm and observing the warning disappear entirely. This is the exact situation guarded_op_doc's doc comment already describes for the identical pattern (an is_number-driven boolean case the compiler can prove exhaustive without a wildcard, defensively given one anyway) — not a new problem, a confirmed instance of an already-accepted one.

2. Functional verification, end to end. The compiled module (with a one-function stub standing in for beamtalk_message_dispatch:send/3) was exercised directly for all three branches: a plain numeric add returns the correct sum; a non-numeric right operand produces the expected plusFromNumber: dispatch call with operands in the documented order; and 1.0e308 + 1.0e308 (float overflow, both operands genuinely numeric) re-raises badarith with its original stacktrace showing erlang:'+' as the failing call — bit-for-bit the same shape as today's uncaught propagation, confirming the re-raise path doesn't fabricate or lose information.

3. Benchmark, with an important caveat. Extending the BT-2709 methodology (same tight-loop shape, same N/Reps, same min_us best-of sampling as bench_guard/0) to compare bare erlang:'+' against this ADR's full try/catch/is_number shape gave a ratio in the same ballpark as the already-shipped is_number guard measured the identical way, on the identical hardware, in the same run — not dramatically worse, and isolating the try/catch wrapper alone (no is_number re-check) showed it contributes a small fraction of that cost; the is_number check itself, not the try/catch, is where most of the overhead already lives in the existing, accepted mechanism this ADR reuses that check from.

The one thing this spike does not give: a trustworthy absolute ns/add number. Reproducing bench_guard/0 completely unmodified, on this sandbox, gave a ratio well outside the ~2.7–3.0× this repository's own docs/development/benchmarks.md records for that exact same benchmark — confirming the doc's own "(run-dependent)" caveat rather than contradicting it. The relative comparison (this ADR's mechanism vs. the guard it's layered next to, same run, same hardware) is sound; the absolute number is not portable off this sandbox and needs re-measuring wherever the project's real benchmark numbers get recorded (docs/development/benchmarks.md) as part of implementation, not asserted from this spike.

Conclusion: the mechanism is real, syntactically valid once corrected, functionally correct on all three branches, and costs roughly what the already-accepted is_number guard costs on the same call sites — which, thanks to the compile-time skip (§ Dispatch mechanism), are exactly the call sites that already pay a guard-shaped cost today. It does not touch the true zero-cost fast path (total + delta, 2 + 2) at all — that remains a structural guarantee (no try emitted there), not a benchmark claim. Safe to proceed to the codegen wiring below.

No number.bt changes — reflected methods are declared only on the types that opt in, not as an abstract protocol entry, since there is no receiver to declare subclassResponsibility against (the hook is looked up dynamically on whatever the right operand turns out to be).

Migration Path

No changes required for existing code — this only changes behavior for expressions that currently crash with a raw badarith (number-on-the-left arithmetic against a non-numeric right operand). No previously-successful expression's result changes.

One narrow, existing-code-visible effect is worth naming rather than glossing over: code that wraps such an expression in on: TypeError do: (or a broader on: Error do:) specifically to recover from today's badarith will observe a different outcome after this ADR ships, split by whether the right operand's type gained the reflected hook —

Addendum (2026-08-25): BT-3266 — right-operand untyped self.<field> gap, accepted

receiver_is_statically_numeric (BT-2709) treats an untyped self.<field> as statically numeric to keep self.count := self.count + 1-shaped counter arithmetic on the bare-BIF fast path. § Dispatch mechanism's "Trigger condition, refined" deliberately reuses that same predicate, unmodified, to gate this ADR's right-operand coercion check — so the identical leniency reproduces on the right side: self.total + self.extra with extra untyped stays a bare erlang:'+', no guard and no try/catch, and raw- crashes on badarith if extra ever holds a non-number. BT-3263 code review (PR #3515) flagged this as a real, if narrow, gap and filed it as BT-3266 rather than folding a fix into that PR.

Decision: accept the gap as specified; do not diverge the right-operand check from the left-operand one. Concretely, this addendum closes BT-3266 without a code change to the trigger condition — the codegen already matches what's decided here (see "What already shipped" below).

Rationale:

What already shipped (landed in BT-3263, commit 871eac5, ahead of this addendum): receiver_is_statically_numeric's own doc comment (operators.rs) names this exact asymmetry and cross-references BT-3266; test_number_coercion_untyped_self_field_right_operand_stays_bare (codegen/core_erlang/tests/expressions.rs) pins the current, accepted behavior down as a regression test, and a second test using self.<field> on both operands (self.total + self.extra, this issue's literal example) was added closing out BT-3266's coverage criterion.

If untyped-field arithmetic's silent-badarith risk ever becomes a real problem in practice, revisit both operands together as one design pass — not the right operand alone.

Addendum (2026-10-18): BT-3892 — untyped/union self.<field> trust revisited, both operands

The risk above became real: field: p = nil set to a Point in setUp, then self.p + q, skipped the left guard, hit badarith, and was routed to plusFromNumber: on q — a DNU that never sent + to the Point. Union-typed fields (:: Point | Nil) had the same failure because only simple annotations were recorded. Per the addendum above, both operands were revisited together, by changing what receiver_is_statically_numeric (and its comparison twin) know about a field, not by splitting the predicate:

Both operands still share one rule, so the right-operand gap closes with the left. The implementation is crates/beamtalk-codegen/src/core_erlang/field_types.rs.

A union with a Nil member (:: Integer | Nil) stays bare for arithmetic, where the BIF already raises badarith on nil, but is guarded for comparisons. BT-3924 inverted the runtime comparison guard from "is this an object?" (beamtalk_primitive:is_object/1) to an inline primitive-comparable test (is_number or is_binary): numbers, characters and strings take the bare BIF and every other receiver is sent the operator as a message. So nil < 5 raises does_not_understand wherever it appears — an untyped local or parameter, or a nilable field — instead of silently answering false. Symbol gained < > <= >= primitives so #a < #b keeps working through dispatch.

Addendum (2026-11-25): BT-5176 — number-on-the-left comparison (< <= > >=)

Status: Accepted (2026-11-25). Nothing in this addendum is implemented yet. It supersedes constraint 4, the "No comparison-operator reflection" bullet of § What this ADR does not do, the deferred alternative "Extend reflection to comparison operators now" and the first Neutral consequence, all of which deferred comparison to "a separate follow-up". This is that follow-up.

Context

3 <= nil answers true. A numeric <, <=, > or >= whose right operand is not a number is handed to the Erlang comparison BIF, and Erlang's term order is total: every number sorts below every atom, tuple, map, list and binary, and nothing raises. max:, min: and between:and: (stdlib/src/number.bt) are written with those operators, so they inherit it: 3 max: nil answers nil. So does a class that takes > from uses: Comparable, whose derived > is other < self: aRank > 5 runs 5 < aRank and answers true. (All three observed on 0.6.0-edge while writing this.)

The receiver side was closed in two steps. BT-3924 (2026-10-18 addendum) made a non-comparable receiver dispatch, so nil < 5 raises does_not_understand. BT-4823 then stopped trusting a declared receiver (x :: Integer), because a declaration is not enforced at run time and < cannot raise: only a receiver the compiler proves (a literal, self in a builtin class) keeps the bare BIF. The argument side was left open on purpose by BT-3928 (2026-10-19), which documented the term-order fallback, widened the static warning and pinned the behaviour with two tests.

Every path a numeric comparison takes tests the receiver only:

PathWhereToday
Inlined, proven receivergenerate_binary_op (crates/beamtalk-codegen/src/core_erlang/operators.rs), ComparisonReceiver::Provenbare erlang:'<'(L, R)
Inlined, declared receiverOperatorGuard::DeclaredComparisonkind test on L, then bare BIF or dispatch
Inlined, unknown receiverOperatorGuard::Comparisonis_number(L) or is_binary(L), then bare BIF or dispatch
Primitive method bodygenerate_comparison_bif (primitives/mod.rs), used by Integer, Float, Character and Stringbare erlang:'<'(Self, P0) — every dynamic send and perform:

Why BT-3928 is being reversed. It gave three reasons for a static-only fix. Each has since changed:

  1. "A runtime check adds a branch to every bare-BIF comparison, including the statically numeric hot paths, and no operator benchmark exists." The check below is elided when the right operand is proven numeric, the BT-5176 research measured the remaining cost at +0.1–0.3 ns per comparison (the same order as the receiver guard those sites already pay), and benchmarks/vs-elixir now has comparison rows (11d–11g, added for BT-4823) to measure against.
  2. "Arithmetic doesn't raise type_error for a wrong-kind argument; it goes through reflected dispatch and ends in does_not_understand, so a comparison type_error would be a new, inconsistent precedent." This addendum does not add that precedent for numeric operators. It uses the same reflected dispatch and the same does_not_understand. (The objection was narrower than it sounds: stdlib value types already raise type_error from their own comparisons — aDuration < 5 is "Argument must be a Duration", beamtalk_duration.erl. See § Consequences.)
  3. "The static checker already catches the numeric case." Only when the argument's static type is known. BT-5175 is the counter-example: 1 to: nil do: [...] compiled, and the pure Integer>>to:do: body (self <= end, end :: Integer) then looped forever, because every number is below nil. A silent wrong answer in a loop condition is a hang, not a cosmetic defect.

The owner's decisions going in (BT-5176): comparisons get reflected hooks for consistency with this ADR rather than a bare type_error; String, Character and Symbol wrong-kind ordering is a separate follow-up; and this is an addendum, not a new ADR.

Decision

  1. Rule. When the left operand of <, <=, > or >= is a number at run time and the right operand is not, the right operand is sent a reflected comparison method. The four operators no longer term-order a number against a non-number. max:, min:, between:and: and every other caller of the four operators inherit the rule. Equality (==, =:=, /=, =/=) is untouched and stays total: 3 == nil is false.
  2. Hook shape: one reflected method per operator, named on this ADR's <verb>FromNumber: scheme — not a single three-way compareFromNumber:.
  3. Missing hook: does_not_understand with a hint, the same kind this ADR already raises for a missing plusFromNumber: — not a type_error.
  4. Trigger: a proactive is_number test on the right operand, on both the inlined and the primitive path, elided only where the compiler proves the right operand numeric. The failing arm is a single call to a runtime helper that shares the arithmetic one's implementation.
  5. Type checker: the hook is an ordinary declared method; the checker looks it up on the argument's class, so a present hook silences the existing warning and supplies the send's type.

Reflected comparison protocol

ExpressionSent to the right operandThe hook answers
n < xx lessThanFromNumber: nn < x
n <= xx lessOrEqualFromNumber: nn <= x
n > xx greaterThanFromNumber: nn > x
n >= xx greaterOrEqualFromNumber: nn >= x

As with minusFromNumber:, the receiver is the value that was on the right and the parameter is the number that was on the left: the method name reads in source order (n less than self).

Value subclass: Money
  field: cents :: Integer = 0

  // receiver-dispatch (unchanged): aMoney < bMoney
  < other :: Money -> Boolean => self.cents < other cents

  // number-on-the-left: 500 < aMoney
  lessThanFromNumber:       n :: Number -> Boolean => n <  self.cents
  lessOrEqualFromNumber:    n :: Number -> Boolean => n <= self.cents
  greaterThanFromNumber:    n :: Number -> Boolean => n >  self.cents
  greaterOrEqualFromNumber: n :: Number -> Boolean => n >= self.cents

Three points of guidance go into the language documentation:

Missing hook: does_not_understand, with a hint

A right operand whose class has no hook raises the ordinary does_not_understand for the synthesized selector, with a hint that names the operator the caller actually typed. Kind does_not_understand (RuntimeError), class the right operand's, selector the hook:

3 <= nil
// => ERROR: UndefinedObject does not understand 'lessOrEqualFromNumber:'
// => ERROR: '<=' cannot order a number against UndefinedObject — the right operand must be a number, or a value whose class implements 'lessOrEqualFromNumber:'

500 < aMoney     // => true    (aMoney lessThanFromNumber: 500)
aMoney < bMoney  // receiver dispatch to Money>>'<', unchanged
3 max: nil       // => ERROR: UndefinedObject does not understand 'greaterThanFromNumber:'
3 == nil         // => false   (equality stays total)
nil < 3          // => ERROR: UndefinedObject does not understand '<'   (unchanged, BT-3924)

(Two // => lines for one error, as in § REPL example: the message, then the hint that format/1 renders on the Hint: line.)

The two failures are now symmetric: nil < 3 is a does_not_understand for < on UndefinedObject, and 3 < nil is a does_not_understand for lessThanFromNumber: on UndefinedObject. One on: RuntimeError do: handler covers both, and covers 3 + nil too. The symmetry is between a number and a value with no ordering; it does not extend to types that validate their own argument (aDuration < 5 raises TypeError, and TypeError and RuntimeError are siblings under Error).

The hint comes from the same code as the arithmetic one. The body of beamtalk_message_dispatch:send_number_coercion/4 is kept as the one implementation, and a second entry point, send_number_comparison/4, calls it with the ordering wording; codegen chooses the entry point, so the runtime holds no table of which operators are comparisons (a rule that would otherwise be duplicated across the Rust/Erlang boundary). The ordering text differs from today's arithmetic template because that template ("implement it to support …") reads as advice to add a method to nil, the common case here and the wrong advice. The arithmetic hint is reworded to match (decision 4 below): 3 + nil gets "'+' cannot combine a number with UndefinedObject — the right operand must be a number, or a value whose class implements 'plusFromNumber:'", which replaces the text in § Hinting the DNU when the reflected method is missing. The narrow catch — only a does_not_understand for this class and this selector — is what keeps a bug inside a present hook from being relabelled.

Trigger and codegen

There is no badarith to catch, so the mechanism is the proactive test the deferred alternative predicted: is_number on the right operand, placed in front of every bare comparison BIF whose left operand is a number.

Inlined path (generate_binary_op). R? below stands for "unless the right operand is proven numeric" (§ Elision):

ReceiverTodayWith this addendum
Proven number (5 < x)bare BIFR? case is_number(R): BIF / hook
Proven String ("a" < x)bare BIFunchanged (follow-up)
Declared number or Character (n :: Integer)kind test on L: BIF / sendtrue arm gains R? case is_number(R): BIF / hook
Declared Stringis_binary(L): BIF / sendunchanged
Declared union with String (:: Integer | String)is_number(L) or is_binary(L): BIF / sendsplit by kind like the unknown receiver: only the number arm gains the test
Unknownis_number(L) or is_binary(L): BIF / sendnumber arm gains the R? test; binary arm unchanged

Reference shape for the unknown receiver, hand-compiled with erlc +clint and exercised for the number, string and non-number-right cases (the case_clause fallback arms that guarded_op_doc adds to every boolean case are omitted here):

let <BinLeft> = <left operand> in
let <BinRight> = <right operand> in
case call 'erlang':'is_number'(BinLeft) of
  <'true'> when 'true' ->
    case call 'erlang':'is_number'(BinRight) of
      <'true'> when 'true' ->
        call 'erlang':'<'(BinLeft, BinRight)
      <'false'> when 'true' ->
        call 'beamtalk_message_dispatch':'send_number_comparison'(
            BinRight, 'lessThanFromNumber:', [BinLeft], '<')
    end
  <'false'> when 'true' ->
    case call 'erlang':'is_binary'(BinLeft) of
      <'true'> when 'true' ->            %% String receiver: unchanged
        call 'erlang':'<'(BinLeft, BinRight)
      <'false'> when 'true' ->           %% BT-3924: dispatch the operator
        call 'beamtalk_message_dispatch':'send'(BinLeft, '<', [BinRight])
    end
end

Both operands are let-bound before the test, as guarded_op_doc and number_coercion_bare_path already do, so the right operand is evaluated once. Everything is built from form::* and typed leaves (ADR 0089). This is expression codegen, like the existing guard: it threads no state and does not go through ThreadedIr.

Primitive path (generate_comparison_bif). The @primitive bodies of Integer, Float and Character </<=/>/>= become case is_number(P0) of true -> BIF; false -> hook. String's stay bare. The call is in tail position in the method body, so it costs nothing on the numeric arm. This is what makes perform: and every other dynamic send agree with the inlined path. generate_comparison_bif takes no class and is shared with String (string.rs) and reflection.rs, and also emits the equality BIFs, so it is split: a numeric-ordering variant for the three classes, the existing one for everything else. The unit tests in primitives/integer.rs that pin the bare output change with it.

Character. A Character is a codepoint integer at run time (beamtalk_primitive:class_of/1 answers Integer), so a character held in an untyped variable is indistinguishable from a number and necessarily takes this rule. The rule is therefore stated on the runtime representation and applied to statically known Character receivers too, so that $a < nil behaves the same whether or not the compiler can see the $a. What stays for the follow-up is String and Symbol receivers, and number-versus-character confusion ($a < 5), which no runtime test can see.

Four places read "a comparison on a proven receiver cannot run user code and answers a Boolean". Each must take the right operand into account, from one shared predicate:

A proven-receiver site that gains the test should also offer the BT-4279 fast path (is_number(R)), which the bare path has never needed to.

Elision: proven, not declared

The right-operand test is omitted only when the compiler proves the right operand is a number. This is a new predicate, "proven numeric". It is not ComparisonReceiver::Proven, which also accepts a String literal and self in String and records no kind (5 < "abc" must be tested):

A declared right operand (n :: Integer parameter or field) is tested. This deliberately differs from arithmetic, which reuses receiver_is_statically_numeric and trusts a declaration, and from the BT-5176 research proposal, which suggested the same for comparison. The reason is the one BT-4823 established for the receiver: arithmetic can trust a declaration because a wrong value still raises badarith; comparison cannot, because a wrong value answers silently. Trusting declared right operands would leave exactly the motivating cases broken — Number>>max:, min: and between:and: all declare other :: Number, and Integer>>to:do: declares end :: Integer, so 3 max: nil would still answer nil and 1 to: nil do: would still depend on a local guard. The right operand gets the receiver's standard of evidence (only a proof removes the test), not a second, looser rule.

Counted loops are unaffected: an inlined to:do: / timesRepeat: emits its own erlang:'=<' against a limit it has already number-checked once in the loop preamble (counted_loops.rs, BT-5136, BT-5167), and never goes through generate_binary_op. BT-5175 (in flight, PR #5762; not on main as this is written) adds the same check to the pure Integer>>to:do:, whose body today is a bare self <= end. That guard is worth keeping after this addendum: it raises a type_error that names the limit, once, before the first iteration, matching the inlined path. With this addendum the comparison inside the loop would raise as well, so neither depends on the other.

The failing arm, and what it costs

The BT-5176 research found that the shape of the failing arm matters more than the test: an arm that ends in erlang:error/1 is free, while an arm that makes an ordinary call forces a stack frame onto the whole loop iteration (1.4 → 8.3 ns per comparison on a literal-receiver loop). It recommended erlang:error(Term). A reflected hook cannot do that: the hook answers a value, so its arm must be a call that returns. Measured for this addendum (hand-written Erlang, OTP 28, loop with five live values, 50M iterations, median of 9, two runs; the absolute numbers are this sandbox's and only the ratios are meaningful):

Shapens/iter
bare N < B4.3 – 4.6
is_number(B), failing arm erlang:error/14.7 – 5.0
is_number(B), failing arm a hook call17.1 – 17.5
bare N + B4.5 – 4.9
N + B in this ADR's try/catch (what 5 + x compiles to today)17.9 – 18.0
unknown receiver today (guard with a dispatch arm)14.2 – 17.3
unknown receiver with the right-operand test and hook arm14.6 – 14.7

Reading it:

So the "failing arm must be erlang:error/1" requirement is met in spirit only: the failing arm is one call to a runtime helper — never the inline beamtalk_error construction chain of type_error_raise_doc, and the raise itself happens inside the helper — but it is a call, and the literal-on-the-left shape pays for it. The owner accepted that trade (decision 1 below).

How the type checker sees the hook

Constraint 2 holds as it does for arithmetic: the hook is an ordinary method with a declared signature, and nothing is looked up through perform:.

What this addendum does not do

Prior art

LanguageMixed orderingVerdict
Python 33 < None raises TypeError; Python 2 ordered mixed types arbitrarily and Python 3 removed that. a < b tries a.__lt__(b), and on NotImplemented the reflection b.__gt__(a); there is no __rlt__. The three-way __cmp__ was removed in favour of per-operator methods.Same behaviour change, for the same reason. Per-operator adopted. The reuse of > as the reflection of < is not (see "Mirror dispatch").
Ruby3 < nil raises ArgumentError ("comparison of Integer with nil failed"). Numeric operators ask the right operand to coerce; Comparable derives the operators from one <=>, which answers nil for incomparable values.The three-way design at its best — and it needs a fourth answer (nil) to be honest about partial orders.
Pharo / Squeak3 < nil raises doesNotUnderstand: on nil, for the coercion selector the number's failed primitive retries with (the adaptTo… family; the exact selector varies by image). 3 = nil is false.The same outcome as this addendum: a DNU on the right operand for a selector the caller never typed, equality total. Recalled, not run.
Erlang / ElixirTotal term order; 3 < nil is true. Elixir documents it as structural comparison and gives Date.compare/2 and friends for semantic ordering.The behaviour being removed. It is right for a term language and wrong for one whose < is a message.
Gleam< is Int-only and <. is Float-only; anything else is a compile error.Not available to a gradually typed language, but the type-checker warning is the same idea.
RustPartialOrd::partial_cmp answers Option<Ordering>; lt/le/gt/ge are derived but individually overridable; mixed-type ordering is an explicit impl PartialOrd<Money> for i32.Opt-in per type pair, as here. Again the three-way form needs a "no answer" case.

User impact

Steelman analysis

Alternative: a single compareFromNumber:

Alternative: plain type_error, no hooks

Alternative: status quo (BT-3928)

Tension points. The BEAM veteran's case for type_error is the strongest argument against this design, and it is a measured one: hooks cost a frame on literal-receiver sites that type_error would not. The language designer's case for compareFromNumber: is strong for total orders and weak exactly where a user type differs from a number. And the newcomer is better served by a TypeError message than by a hinted DNU — the hint narrows that gap but does not close it.

Alternatives considered

Single compareFromNumber: answering -1, 0 or 1. One method instead of four, consistent by construction, and it would cover max:, min: and between:and: at once. Rejected:

One hook that carries the operator (Squeak's adaptToInteger:andCompare: shape, e.g. compareFromNumber: n op: #<). One method, any answer, and no trichotomy problem. Rejected for the reason this ADR rejected adaptTo:andSend:: the operator becomes data, the body is a perform: or a four-way branch, and the return type no longer depends on a selector the checker can look up.

Convert, then re-send (Ruby's coerce: ask the right operand to turn the number into its own kind, then send the operator receiver-side). One method, it reuses Money>>< and Comparable, and it has no recursion trap — the strongest alternative on boilerplate. Rejected: it only fits types a number converts into (is 500 five hundred cents or dollars? a Vector has no such conversion), which is the generality-tower model this ADR rejected for arithmetic; and it would give comparison a different protocol from the plusFromNumber: family it sits beside. A type that does have a natural conversion can write each hook as one line through it.

Fix only the primitive path and the stdlib callers (is_number checks in the @primitive bodies and in max:, min:, between:and:, to:do:), leaving inlined codegen alone. Far smaller. Rejected: almost every comparison in user code is inlined, so 3 <= nil and x < y would keep term-ordering and the two paths would disagree, which is the state BT-4823 removed for receivers.

Plain type_error (the BT-5176 research recommendation). Raise type_error "Comparison argument must be a number" with a frame-free erlang:error/1 arm. It is cheaper and smaller. Rejected by the owner for consistency, and the consistency argument is concrete: BT-3928's own objection to a runtime check was that a comparison type_error would diverge from arithmetic; 3 + nil and 3 < nil would raise different exception classes for the same mistake (though the language is not uniform here either way: aDuration < 5 already raises TypeError from Duration's own check, so with hooks 5 < aDuration and aDuration < 5 differ instead); and aMoney < 5 working while 5 < aMoney is a hard error with no opt-in is the asymmetry this ADR exists to remove. Shipping type_error now and hooks later is not a cheap middle path either: the second step would change the error's class again, a second breaking change for anyone who had started catching the first. It remains the fallback if the frame cost on literal-receiver sites were ever judged unacceptable; the owner accepted that cost (decision 1 below).

Mirror dispatch (Python's rule): send x > n for n < x. No new selectors at all. Rejected: a type's > is declared for its own kind (other :: Money), so a number would arrive inside a method that never expected one and fail somewhere in its body instead of at a named hook; a type could not opt out; and with uses: Comparable the derived > is other < self, so 5 < aRank → aRank > 5 → 5 < aRank recurses forever.

Trust a declared right operand (elide the test for n :: Integer). Cheaper on literal-receiver sites. Rejected in § Elision: it leaves max:, min:, between:and: and to:do: silently wrong.

Raise type_error when the hook is missing, hooks otherwise. Gives the newcomer the friendlier error. Rejected: the helper would have to probe for the method before sending, the same mistake would raise TypeError for comparison and RuntimeError for arithmetic, and a missing method is a does_not_understand everywhere else in the language.

Status quo. Rejected: see "Why BT-3928 is being reversed".

Consequences

Positive

Negative

Neutral

Migration

CHANGELOG (with the codegen change), following the file's Breaking: form:

Breaking: a number is no longer ordered against a non-number (BT-5176, ADR 0116 addendum). <, <=, > and >= with a number on the left and a non-number on the right — and so max:, min: and between:and: — used to fall back to Erlang term order (3 <= nil was true, 3 max: nil was nil). They now send the right operand a reflected method (lessThanFromNumber:, lessOrEqualFromNumber:, greaterThanFromNumber:, greaterOrEqualFromNumber:) and raise does_not_understand when it has none.

  • Unchanged: ==, =:=, /=, =/=; String and Symbol receivers; default sort and Dictionary/Set order.
  • Migration: test for nil before comparing (x isNil, ifNil:), and for a symbol sentinel before comparing against it (elapsed < timeout where timeout may be #infinity); for a deliberate mixed order use default sort or an explicit comparator; to make a value type comparable with numbers, implement the four hooks.
  • Reverses BT-3928.

Pinned tests (both pin the BT-3928 fallback on purpose):

Documentation. docs/beamtalk-language-features.md § Comparison (around line 1113): replace the paragraph "The argument is not checked at run time …" with the rule, the hook table, the error and the three points of guidance; drop "Comparison operators have no number-on-the-left coercion". Add the four selectors to § Number-on-the-left coercion. Reword the comments that cite BT-3928 (arguments.rs near line 1141, stdlib/test/fixtures/typed_comparison_probe.bt, comparison_receiver's doc comment in operators.rs). ADR 0148 (Deferred) records that "the comparison argument stays unchecked by decision (BT-3928)"; it gets a pointer to this addendum in the same change.

Downstream. The BT-5176 research ran a prototype of the rule (as type_error) against the repository: 10,105 of 10,107 BUnit tests passed, the two failures being the pins above, and the stdlib, bootstrap and example suites passed. A grep of 17 satellite repositories found no reliance on mixed ordering. The implementation must repeat this with the real change and run the cross-repo CI.

Implementation plan

The de-risking this ADR asked of its own mechanism is already done for this one: the Core Erlang shapes above (unknown receiver, proven receiver, primitive body) were hand-compiled and run, the Money example passes beamtalk check, and the cost table in § The failing arm is measured.

PhaseSizeContentAcceptance
0. BaselineSAdd benchmarks/vs-elixir rows before any behaviour change: 5 < b with b untyped (proven receiver, unknown right — the new row BT-5176 asked for), a < b both untyped, a :: Integer < b untyped, 5 + b untyped as the arithmetic reference, and an actor loop [0 < self.n] whileTrue: that writes a field. Record them together with the existing rows 11d–11g, which are in the harness (vs_bench.erl) but in no committed results file.Rows run under just bench-vs-elixir; baseline committed under benchmarks/vs-elixir/results/.
1. RuntimeSsend_number_comparison/4, sharing send_number_coercion/4's body. The arithmetic hint is reworded to match (decision 4), with the tests and language-features text that pin it. EUnit: both hint texts; a DNU raised inside a present hook is not relabelled.just test and just dialyzer green.
2. Codegen and behaviourM–L, ci:corpusShared selector table in beamtalk-core (replaces coercion_selector). Right-operand test on both inlined arms and in generate_comparison_bif for Integer/Float/Character. The shared "bare and Boolean" predicate for classify_binary, the BT-4279 fast path and the guard-return rewrite. Flip the two pins. New stdlib/test/number_comparison_arg_test.bt. CHANGELOG entry and language-features rewrite in the same PR.See below.
3. Type checkerSHook lookup in check_binary_operand_types for all eight operators; new hint text; inferred type from the hook. Blocked by phase 2.Tests: a class with the hook produces no diagnostic and the send has the hook's return type; a class without it warns with the new hint; an untyped argument is silent; 5 + aVector with plusFromNumber: no longer warns.
4. Follow-ups (separate issues)—String/Symbol receivers; proving more right operands (loop counters, arithmetic results); match-arm guards.—

Phase 2 acceptance.

Decisions on the open questions

The draft put four questions to the owner, who answered them on 2026-11-25 when accepting this addendum:

  1. The frame on literal-receiver sites: accepted. Hooks make the failing arm a returning call, so 0 < x (proven receiver, unproven right) pays a stack frame in call-free code — what 5 + x already pays — and can move a field-writing actor loop to StateAcc. The frame-free type_error without hooks is not taken. The benchmark gate in phase 2 still applies.
  2. Declared right operands are tested, not trusted. Confirmed, as in § Elision, so that max:, min:, between:and: and to:do: raise.
  3. Character receivers are covered. The wrong-kind follow-up is narrowed to String and Symbol receivers.
  4. The arithmetic hint is reworded to match the ordering wording. 3 + nil and the other three arithmetic operators take the new template (§ Missing hook). The change is made in phase 1, not in the pull request that added this addendum.

Implementation Tracking

Parent: BT-2712 (Phase 4 of BT-2708, already tracked this ADR's spec — reused as the parent for the issues below rather than creating a duplicate Epic) Issues:

Status: Complete for the arithmetic design — all issues Done (BT-3262–BT-3266 closed 2026-08-25, BT-2712 closed 2026-08-25; addendum issues closed 2026-10-18). The 2026-11-25 comparison addendum is implemented at run time (Phases 0–2, BT-5207–BT-5211) and in Collection max/min (BT-5214, BT-5266); the type-checker phase (BT-5212) is open.

References