ADR 0152: Remove Cross-File Extension Methods
Status
Accepted (2026-11-24)
This supersedes the cross-file part of ADR 0066: a Class >> selector => ... definition in a file that does not define Class. Superseded: §2 (the Class+Feature.bt file convention), §3 (ETS dispatch with interleaved lookup), §4 (compile-time conflict detection and type metadata), §6 (load order), §7 (ETS table lifecycle), and the BT-5002, BT-5060 and BT-5061 lookup-order amendments. Also superseded: §5 (sealed classes can be extended), because neither remaining form of >> can reach a stdlib class. Kept: the >> syntax (§1) for same-file methods and for live method definition, and the >> reflection operator.
The first draft of this ADR proposed replacing cross-file >> with retroactive protocol implementations (Proto implementFor: Class). The owner decided to remove first and defer the replacement until a concrete need appears, and to remove without a deprecation period, because no package uses the feature. That design is kept under Future direction.
How to read the evidence. A claim marked (read) was checked by reading the cited code on main at 9dcf825. A claim marked (inferred) follows from that reading but was not checked directly. A claim marked (reviewer) was reported by an adversarial review pass and spot-checked, not read in full. Nothing in this ADR was verified by running the system.
Context
Problem statement
Beamtalk has one way to add a method to a class from outside the class's file: the cross-file extension method of ADR 0066.
// In helpers.bt. String is defined in stdlib, not in this file.
String >> shout => self uppercase ++ "!"
The compiler registers the method in a runtime table when the declaring module loads, and every lookup consults that table at each level of the superclass walk. The mechanism has three problems.
1. It is almost unused. A count of top-level Class >> selector => definitions whose target class is not defined in the same file:
| Where | Cross-file extensions |
|---|---|
stdlib/src | 0 (read) |
examples/ | 0 (read) |
buccaneer, catalina, http, oauth, mint, hpack, yaml, linear, agents, announcements (about 400 .bt files) | 0 (reported by the owner's survey; not re-checked here) |
beamtalk-symphony (48 .bt files), beamtalk-exdura (92) | 0. Every >> is a Class>>selector reference in a comment (checked by the parent session, and by gh api search/code here) |
| beamtalk-watcher, beamtalk-collections | 0. Same comment-only pattern in the search fragments returned (code search shows a few fragments per file, so this is strong but not exhaustive) |
beamtalk-liquid (32 .bt files), beamtalk-registry | 0 hits for >> in .bt files (liquid also checked by the parent session). With liquid, every dependency of symphony and exdura is covered |
| beamtalk-uat | 1 file: a UAT fixture with SomeActor >> bumpBy: and SomeActor >> tallyPlusTen, written to exercise the feature (BT-2452) |
stdlib/test | 11 files, 16 target classes (read) |
tests/parity | 3 files (read) |
tests/repl-protocol/cases | 0 cross-file definitions. Five scripts type Foo >> sel at the prompt for a class defined earlier, which is a live edit, a refusal test or a same-file case, not a cross-file extension. Five other scripts call beamtalk_extensions through FFI (reviewer, spot-checked) |
benchmarks/, test-package-compiler/cases | benchmarks/vs-elixir/beamtalk/src/vb_shadowed_math_ext.bt (BT-4431) and the foreign_extension and set_theoretic_operators snapshot cases: fixtures written to measure or snapshot the feature (reviewer, spot-checked) |
fuzz/corpus | 0 definitions. The one >> file uses the Counter >> #sel reflection form (read) |
Every use is a test of the feature itself. No package uses it to do work.
2. It is expensive to keep.
runtime/apps/beamtalk_runtime/src/beamtalk_extensions.erlis 913 lines and owns three ETS tables (read).- 59 of 303 non-test Erlang source files mention "extension". About 52 of them mean open-class extensions, and 22 name
beamtalk_extensions(read). - 250 of 1,130 Rust files mention "extension" (166 outside tests). 111 touch the standalone-method or extension-index types (read).
- Every lookup pays for it. Compiled
has_method/1checksbeamtalk_extensions:has/2at each class. The actor and value default dispatch arms callbeamtalk_extensions:lookup/2(crates/beamtalk-codegen/src/core_erlang/gen_server/dispatch.rs:769-786,core_erlang/dispatch_spec.rs:305-360). The inherited-send path carries an{ext, Owner}verdict (inherited_target/2,beamtalk_dispatch.erl:279). BT-4260 and BT-4280 measured this on hot paths; BT-4800 removed double work on the class-side path.
3. Its lookup order is a steady source of bugs. The order is: the receiver's own compiled method, then at each ancestor that ancestor's extension before its compiled method. The class side differs: an extension beats the class's own compiled method (BT-5061). A Linear search for "extension" over the last 90 days returns 55 issues. A handful are about the VS Code extension; most of the rest are lookup-order or precedence bugs:
| Issue | What went wrong |
|---|---|
| BT-5002 | An extension on Actor was ignored more than one level below Actor |
| BT-5060 | An extension on a Value class is ignored by a direct subclass for a selector the class compiles (the "one-hop gap"; assessed and left as a warning) |
| BT-5061 | Class-side extension beats the class's own compiled class method, the opposite of the instance side |
| BT-5062 | removeSelector: on an owner that also has an extension |
| BT-4307 | Class-side extensions on an ancestor not reached through a subclass |
| BT-4588 | new / new: ignored an ancestor's class-side extension |
| BT-4250 | Class-side super consulted the wrong class's extension |
| BT-4133 | Class-side extensions bypassed by direct-called methods |
| BT-3951 | A runtime extension overriding a known collection selector defeated a codegen guard |
Tooling carried the same cost: the WS3 epic (BT-4610, with BT-4608 and BT-4611) made dependency extensions visible to consumers, and BT-4658 and BT-4923 found validators that skipped extension bodies.
Not every extension bug is a precedence bug. BT-4503 (a value-type extension with ^ in a block answered {Result, Self}) and BT-3920 were bugs in how extension bodies are compiled, and this ADR keeps that body codegen (see Consequences).
The precedence bugs share one root. An extension can take precedence over a compiled method, so every dispatch path, static call, cache and tool must ask "is there an extension that wins here?" The BT-5060 work that merged as #5718 is the latest instance: it adds warning plumbing through eval, load and sync replies to tell the user about a case the dispatch order cannot honour.
What >> is actually used for
>> has three uses, and only one of them needs the extension table.
- Same-file methods.
Pt >> extra: n => ...next toValue subclass: Ptis folded into the class before analysis (BT-4721).crates/beamtalk-core/src/compilation/same_file_fold.rs:15-17says: "nothing registers a same-file extension throughbeamtalk_extensions" (read). - Live method definition.
Foo >> sel => bodyat the REPL goesbeamtalk_repl_eval:handle_method_definition→beamtalk_repl_loader:reload_method_definition/4→reload_class_method_definition(beamtalk_repl_loader.erl:238-281). That reads the class's stored source, merges the method in (crates/beamtalk-compiler-port/src/handlers/compile_method.rs:243-252), recompiles the whole class and hot-loads it (read).Foo class >> seltakes the same path withis_class_method = true. The IDE and MCPsave_methodevaluateClass compile: #sel source: "...", which reaches the sameinstall_methodrecompile path (beamtalk_repl_loader_methods.erl:85-171) (read). No workspace source file callsbeamtalk_extensions:register(read). - Cross-file extensions. Only these use the table. Codegen emits a
beamtalk_extensions:register/5call in the declaring module'sregister_class/0(crates/beamtalk-codegen/src/core_erlang/gen_server/extensions.rs:116-143) (read).
So the first two uses already work without the extension table.
The REPL and the build already disagree
A cross-file extension works only in a module built by beamtalk build, test or run. In a workspace it does not (read):
Workspace load:,:syncand reload compile through the compiler port's file handler. A foreign>>in a file that also defines a class is dropped with a warning, "Standalone method targets unknown class" (crates/beamtalk-compiler-port/src/handlers/compile.rs:151-167).- A file that holds only extensions is refused: "No class or protocol definition found in source" (
compile.rs:184-190). - An inline REPL eval drops it the same way (
inline_definitions.rs:90-97). - A release console refuses any
>>definition (tests/repl-protocol/cases/release_console_default.btscript:41-43).
So the same file means different things under beamtalk run and in the REPL, which docs/beamtalk-principles.md §4 forbids. The one path the first draft did not check is real (read, Phase 0): workspace startup activates BEAMs that the CLI built earlier. beamtalk_workspace_bootstrap:activate_project_modules/1 loads _build/dev/ebin and activate_dependency_modules/1 loads _build/deps/*/ebin, through beamtalk_module_activation, which calls each module's register_class/0. A module built by an older compiler therefore registers its cross-file extensions in a workspace, while the same source loaded with Workspace load: drops them. After Phase 1 no build can produce such a module, and the build stamp (Phase 2) forces a rebuild of any older one, so this path closes without extra work.
What is lost
Two things, and the decision has to own both.
- Adding a method to a class you cannot edit. After removal there is no way to make
Stringor another package'sMoneyanswer a new selector. The one need seen so far isasJson:Json generate:callsasJsonon any value that responds to it (beamtalk_json.erl:344-350), and BT-2819'sJsonMappable(#5717, #5719) must be declared withuses:in the class's own body. No package has used a cross-file extension to meet that need, or any other. - Patching a class method of a class the workspace has no source for. BT-5061 kept the class-side "extension first" order for this. In practice the capability is narrow: it needs a rebuilt module, because a workspace drops cross-file
>>on load (above). It is discussed under Decision §4 and is Open Question 1.
Constraints
- Same-file
>>and live method definition must keep working exactly as they do. - A compiled method must be the only thing that can answer a send, apart from the ADR 0084 runtime class-method funs, which are a separate, deliberate mechanism.
- The meaning must be the same in the REPL,
beamtalk run,beamtalk testand a release (docs/beamtalk-principles.md§4). - Live patching is a message send (
docs/beamtalk-principles.md§11). The principle's own example,Counter >> increment => ...at the REPL, stays. A cross-file extension is registered when a compiled module loads, so it is not itself a live message send; what its removal costs is set out in Decision §4. - Changing a REPL error message or reply needs the owner's sign-off (AGENTS.md, "REPL output"). The owner's decision to make cross-file
>>an error at the REPL is that sign-off for the two changes named in Decision §1.
Decision
Cross-file extension methods are removed. A Class >> selector => ... or Class class >> selector => ... definition is valid only where the class's source is at hand: in the file that defines the class, or as a live edit of a class whose source the workspace holds.
1. What stays and what goes
| Form | After this ADR |
|---|---|
Foo >> sel => ... in the file that defines Foo | Unchanged. Folded into the class (BT-4721). |
Foo >> sel => ... and Foo class >> sel => ... at the REPL | Unchanged. Recompiles the class from its stored source and hot-loads it. |
Class compile: #sel source: "...", IDE and MCP save_method | Unchanged. Same recompile path. |
Proto >> sel => ... at the REPL (ADR 0127 §11) | Unchanged. Patches the provision and re-expands users. |
The >> reflection operator, Foo >> #sel, which answers a CompiledMethod | Unchanged. Only >> as a cross-file method definition is removed. Code built on the operator, such as exdura's self runActivity: OrderActivities >> #reserveInventory:, is not affected |
Foo >> sel => ... in a file that does not define Foo, including another file of the same package | Removed. A compile error. |
pkg@Foo >> sel => ... (ADR 0070) | Removed with it. |
beamtalk_extensions:register/4,5 called through FFI | Removed. The module is deleted. |
// money.bt — still valid: Money is defined in this file
Value subclass: Money
field: cents = 0
Money >> isZero => self.cents = 0
// helpers.bt — a compile error after this ADR
String >> shout => self uppercase ++ "!"
error: `String >> shout` adds a method to String, which is not defined in this file.
= cross-file extension methods were removed (ADR 0152).
hint: define the method in the file that defines the class (in the class
body, or as `String >> shout` next to it), or, for a class loaded in
a workspace, define it live from the REPL or the IDE.
For a class you cannot edit, write a method on a class of your own
that takes the String.
At the REPL, a live definition on a class with source works as before:
>> Counter >> incrementBy: n => self.value := self.value + n
>> c incrementBy: 5
=> 6
A live definition on a class without stored source already fails, with "Class source not available for Integer" (beamtalk_repl_loader.erl:241-246), and Class compile:source: refuses a stdlib class outright (beamtalk_repl_eval.erl:1062-1073) (read; not run). So Integer >> double => self * 2, the REPL example in ADR 0066 §1, fails today as far as the code shows. This ADR does not change that behaviour.
There is no deprecation period. Workspace load: of a file with a cross-file >>, and an inline REPL eval that mixes a class definition with a foreign X >> m, today drop the method with a warning (Context). Both now fail with the compile error above. These are changes to REPL output, made by this decision.
2. Lookup order after removal
- The receiver's class: its compiled method.
- Each ancestor in turn: its compiled method.
doesNotUnderstand:args:.
The class side follows the same order over class methods.
"Compiled method" here includes the methods a class was given as runtime funs, which this ADR does not touch: ClassBuilder method funs and put_method/3 on the instance side (beamtalk_object_class.erl:671) (reviewer), and ADR 0084 class-method funs on the class side, which are checked before that class's compiled class methods (put_class_method, beamtalk_object_class.erl:691) (read). They belong to the class and are installed through it.
There is no extension step at any level, on either side. The BT-5060 one-hop gap disappears because there is nothing to skip. respondsTo:, canUnderstand:, perform: and super follow the same order because they have nothing else to consult.
The guarantee, once the runtime pieces are deleted (Phase 2). Loading a package cannot change which method answers a send to an existing class. Between Phase 1 and Phase 2 the table still exists, but nothing the compiler accepts can fill it; only an FFI call or a module built by an older compiler can.
3. What is deleted
The full list, with files, is in Implementation. In summary:
- The runtime extension registry,
beamtalk_extensions.erl, with its three ETS tables, last-writer-wins policy and conflict history. - The extension lookups interleaved in dispatch, in generated code and in
beamtalk_dispatch.erl. - The one-hop paths and the BT-5060 warnings that merged as #5718.
- The class-side extension-first order and the
extensionkind of shadow flag. Theruntime_funandclass_varskinds stay. - The compiler's extension index, conflict check and foreign-extension codegen.
- The WS3 plumbing that carries dependency extensions to a consumer's type checker (BT-4608, BT-4610, BT-4611).
- Reflection and tooling that exist only for extensions:
SystemNavigation extendersOf:andextensionsBy:(system_navigation.bt:223), the extension branches inside most otherSystemNavigationqueries, the browse "extensions" group, extension rows in xref and the ChangeLog, and extension output inbeamtalk doc.
What stays: same_file_fold.rs; the REPL and IDE recompile path; StandaloneMethodDefinition in the AST (the same-file and live forms parse to it); the class-derived cache and its generation read, which hot reload still needs (BT-4984 kept that read partly for extensions; this removes one reason for it, not the read); ADR 0084 runtime funs.
4. Patching a running class
What the code supports today, and so what remains:
| Target | Instance method | Class method |
|---|---|---|
| Class with source in the workspace | Foo >> sel => ... at the REPL (install_method_with_source, beamtalk_repl_loader.erl:265); Class compile:source:; MCP and IDE save_method (install_method/9, beamtalk_repl_loader_methods.erl:85-171). All recompile the class and hot-load it (read) | Foo class >> sel => ... at the REPL, the same path with is_class_method = true (beamtalk_repl_loader.erl:268-279) (read). Not reachable from MCP save_method or Class compile:source:: classCompileSource/3 calls compile_method/6, which is instance side only (beamtalk_repl_eval.erl:1041-1042), and SaveMethodParams has no side field (read) |
| Stdlib class | Refused: "built-in methods are read-only in the workspace" (stdlib_method_read_only_error, beamtalk_repl_eval.erl:1238) (read) | Refused the same way |
| Dependency class | Fails: no class source is stored for a dependency (activate_dependency_modules registers the module and source path only, beamtalk_workspace_bootstrap.erl:121-128) (read) | Fails the same way |
So the REPL and IDE path covers every class the workspace has source for, on both sides at the REPL and on the instance side from the IDE and MCP. That is unchanged.
For the last two rows, a cross-file extension was the only way to add or replace a method, and only from a rebuilt module, not from a workspace load (Context). ADR 0066 kept the class-side "extension first" order (BT-5061) because a class-side extension was "the documented way to patch a class method of a running class" (ADR 0066 §3), and BT-4133, BT-3700, BT-4270, BT-4301, BT-4316 and BT-4317 built the shadow flags and static call keys to honour it.
After this ADR, a class method of a class without stored source is changed by rebuilding and redeploying the class's package. An ADR 0084 runtime class-method fun still takes precedence over a compiled class method, but it has no user-level entry point for an existing class: put_class_method is reached only from the class process, and ClassBuilder's addClassMethod: applies while a class is being built (stdlib/src/class_builder.bt:293) (reviewer). It is not offered as a workaround.
What is actually lost is therefore small: shipping a patch to someone else's class method inside your own package's build. Getting it onto a running node already took a rebuild. This is Open Question 1, and the implementation proceeds on its default.
5. What to do instead, until there is a replacement
- You own the class. Put the method in the class's file, as a body method or a same-file
>>. - You do not own the class. Write the operation on a class of your own that takes the value:
Shout of: aStringin place ofaString shout. - JSON for a foreign class. Convert at the boundary in your own class's mapping.
JsonMapping mapField:to:using:takes a converter (stdlib/src/json_mapping.bt:41-78) (reviewer: read by a research pass, not re-read), so a class that holds a foreignMoneycan say how to encode it withoutMoneyansweringasJson. - You need the foreign class itself to answer. That is the trigger for the Future direction below. Open an issue with the concrete case.
Single source of truth
- The rule "a
>>target must be defined in this file" is one function, called from every file-compile entry point afterfold_same_file_extensions(crates/beamtalk-core/src/compilation/same_file_fold.rs:46-87): the CLI (compile_source.rs), the lint pipeline, the LSP, and the compiler port's file handler and inline-definition handler, where it replaces the two existing warn-and-drop loops (compile.rs:151-167,inline_definitions.rs:90-97). The rule is written three times today; it becomes one. - It must not run on the live-definition path. A lone
Foo >> sel => ...at the prompt parses to a module with a method definition and no class, and is routed to the recompile path byhandlers/expression.rs:219-248(read by a research pass). If the check sat in shared analysis, live definition would break. A parity test must show that a live>>at the REPL still succeeds. - The lookup order is stated once in
stdlib/test/lookup_order_matrix_test.bt, which already compares literal sends,perform:,respondsTo:andsuperagainst one reference order. It keeps that role with the extension dimension removed.
Prior Art
| Language | Can outside code add a method to a class or type? | Notes |
|---|---|---|
| Pharo, Squeak | Yes, *Package extension protocols | Last loaded wins. Overrides are a known packaging problem |
| Ruby | Yes, open classes | Last definition wins. Refinements (lexical) are not seen by respond_to? |
| Objective-C | Yes, categories | Undefined which implementation runs on a clash (Apple QA1908) |
| Swift | Yes, extensions; retroactive conformance warns since SE-0364 | Resolved statically for non-protocol members |
| Kotlin, C# | Yes, but statically resolved and lexically scoped | The member always wins, silently |
| Elixir | Only through a protocol: defimpl P, for: T | Reached as P.fun(x); nothing enters the type's own functions |
| Rust | Only through a trait, under the orphan rule (RFC 2451) | No inherent methods on a foreign type |
| Go | No. Methods may be declared only on types defined in the same package | The idiom is a function or a local wrapper type (from memory, not searched) |
| Newspeak | No | No global namespace to extend |
| Gleam, Erlang | No | Functions in your own module |
| Python | Not for built-in types | User classes can be patched; str and int cannot (from memory, not searched) |
After this ADR Beamtalk sits with Go, Newspeak, Gleam and Erlang: a class's methods are defined with the class. That is a departure from Pharo, taken for the reason ADR 0066 itself gave for rejecting Pharo's last-writer-wins: a package ecosystem is not one developer's image.
The languages that allow outside additions and keep them sound (Rust, Elixir) do it through a protocol or trait with rules about who may implement it. That is the shape of the Future direction.
User Impact
Newcomer. One fewer feature to learn, and one fewer way for a method to appear from nowhere. Someone coming from Ruby or Kotlin will try String >> shout in a helper file and get an error that says what to write instead.
Smalltalk developer. This is a real departure: Pharo developers extend Object and String freely. What stays is the part of the Smalltalk experience Beamtalk actually exercises: redefine a method on a live class from the REPL or the browser and see it take effect.
Erlang/BEAM developer. A class is its module again. To find what answers a send, read the class and its superclasses; there is no table to consult. Generated dispatch loses its extension probes.
Production operator. Loading a package cannot change the behaviour of an existing class. The cost: a class method of a deployed class without source can no longer be replaced from a file (Decision §4).
Tooling developer. No secondary index for extension locations, no "extensions" group, no dependency-extension channel, and no dependency_extensions_unknown guard in the type checker.
Steelman Analysis
Keep ADR 0066 open classes
- 🧑💻 Newcomer: "
String >> shout => ...is one line. Now I have to invent a helper class." - 🎩 Smalltalk purist: "Open classes are Smalltalk. Every Pharo package extends
ObjectandString. Removing them makes sealed primitives truly closed." - ⚙️ BEAM veteran: "It's built and tested, and the matrix test pins the order. Deleting working code has its own risk."
- 🏭 Operator: "A dependency ships a broken class method. Today I can fix it from my own package, with a class-side extension, and rebuild only my package."
- 🎨 Language designer: "It covers both axes of the expression problem with one construct. Removing it with nothing in its place leaves a hole you will have to fill later, under pressure."
Additive-only >> (keep the syntax, forbid overriding)
- 🧑💻 Newcomer: "Same one-liner as before, and I get an error instead of a surprise when it clashes."
- 🎩 Smalltalk purist: "The syntax is unchanged; only overriding goes."
- ⚙️ BEAM veteran: "A miss-only table is one lookup on a path that was going to fail anyway."
- 🏭 Operator: "Compiled methods always win, so loading code still can't change what works."
- 🎨 Language designer: "It fixes the precedence bugs, which are the actual cost, and keeps the capability. Removal throws away the capability to fix the same bugs."
Replace now with protocol implementations (implementFor:)
- 🧑💻 Newcomer: "I'd rather learn the replacement once than lose the feature and get a different one later."
- 🎩 Smalltalk purist: "At least the method is still a message send on the object."
- ⚙️ BEAM veteran: "It's
defimplwith ordinary sends." - 🏭 Operator: "One migration, not two."
- 🎨 Language designer: "The design is already worked out. Shipping removal alone means a release where
asJsonon a foreign class is impossible."
Pure protocol dispatch (Elixir): Json encode: x
- 🧑💻 Newcomer: "It's obvious where the code lives: in the protocol."
- 🎩 Smalltalk purist: "
Json encode: xis still a message send, to the service that owns the operation." - ⚙️ BEAM veteran: "Nothing enters the class's lookup, so nothing can go wrong in it."
- 🏭 Operator: "No table on the dispatch path at all."
- 🎨 Language designer: "Implementations are keyed by
{protocol, class}, so an ownership rule gives real coherence."
Tension points
- The language designer's case for additive-only
>>is the strongest argument against plain removal: it buys the same runtime guarantee and keeps the capability. The answer is that it also keeps a table on the miss path, a registration protocol, load-order questions and tooling for a feature with no users, and it commits to a design (selector-keyed, no ownership) before any real case has shown what the design should be. - Operators lose the ability to patch a dependency's method from their own package's build (Decision §4). It already needed a rebuild and nobody is known to have used it, but its absence will be felt when it is needed.
- Removing first and replacing later means two changes for anyone who did depend on the feature. The survey found nobody.
Alternatives Considered
Keep ADR 0066 open classes as they are
Rejected. The feature has no users outside its own tests, about 52 Erlang and 111 Rust files carry it, and its lookup order has produced about nine precedence bugs in 90 days, the last of which (BT-5060) was closed by adding warnings instead of fixing the order. The cost recurs with every new dispatch optimisation, because each one must prove that no extension outranks the method it resolved.
Additive-only >> without protocols
Keep the cross-file syntax, make a clash with a compiled method an error, and consult the table only on a miss.
// app/src/Money+Json.bt
Money >> asJson => self class jsonMapping encode: self
Money class >> jsonMapping => JsonMapping new |> mapField: #cents to: "amount"
This removes the precedence bugs and keeps the capability. Compared with removal and with the deferred design:
| Remove (this ADR) | Additive-only >> | implementFor: (deferred) | |
|---|---|---|---|
| Precedence bugs removed | Yes | Yes | Yes |
| Extension table on the dispatch path | None | On a miss only | On a miss only |
| Registration, load order, clash handling at run time | None | Needed | Needed |
| Add a method to a foreign class | No | Yes | Yes |
| Ceremony for one method | n/a | None | A protocol |
| Provided methods come with the protocol | n/a | No; each is written out again | Yes |
| Something to attach an ownership rule to | n/a | No | Yes |
| WS3 dependency plumbing | Deleted | Kept | Kept, as declarations |
Rejected for now, on cost: it keeps a runtime table, a registration protocol and the dependency plumbing alive for a feature with no users. It is not rejected for being a worse design than implementFor:; the table above shows the two need the same runtime machinery. It stays on the list: if a need appears and the owner concludes that ownership rules are not worth their ceremony, this is the smaller of the two replacements, and nothing in the miss-only runtime design depends on which syntax fills the table.
Replace now with protocol implementations
The first draft of this ADR. Add Proto implementFor: Class, then deprecate and remove cross-file >>. Rejected by the owner in favour of removing first, for three reasons: nothing uses the feature being replaced; the ownership rule at the centre of the design cannot be settled without a real case (see Future direction, F3); and the replacement is an XL piece of work whose only demonstrated use is asJson. The design is kept below.
Pure protocol dispatch: Json encode: x, nothing enters the class's lookup
Implementations are keyed by {protocol, class} and reached only through the protocol: JsonRepresentable asJson: x, or a facade such as Json encode: x.
This is the cleanest design at run time and the only one where an ownership rule is fully sound. It is not needed for removal, so the question is whether it should be the future direction instead of implementFor:. It was weighed seriously and is not preferred, because:
- It loses
x asJson. A class that writesasJsonin its body and a class that has it implemented from outside would be called differently, or every caller would have to use the protocol form. Either the two populations are not interchangeable, or the language grows a second call syntax for every protocol-shaped operation. - It breaks the existing contract.
Json generate:finds the hook withrespondsTo: #asJsonand a send.JsonMappable's provision isasJson => self class jsonMapping encode: self. Collections of mixed objects are sentasJsonuniformly. All of that would have to become protocol dispatch. - It is a second dispatch mechanism. Beamtalk's object model has one question, "does the receiver understand this message?". Pure protocol dispatch adds "does this protocol have an implementation for the receiver's class?", with its own inheritance walk, reflection and tooling.
- Structural conformance would split. A class conforming by having the method and a class conforming by external implementation would not both pass
conformsTo:by the same test.
What implementFor: gives up in return is real: coherence is by {class, selector}, so its ownership rule is weaker than Rust's (F3). Because this ADR only removes, the choice between the two stays open until a real case arrives, and a library is free to offer Json encode: x-style entry points today.
Lexically scoped static extension functions (Kotlin, C#)
x shout resolves at compile time, from the static type of x, to a function imported into the file.
Rejected because Beamtalk sends are dynamic and the static type is often Dynamic. ADR 0127 rejected "extension methods on a protocol" for the same reason. Concretely:
respondsTo: #asJsonwould answerfalse, so theJson generate:hook would not find the method.perform:, a send through aDynamicreceiver, and a send from another file would not find it either.- The REPL has no lexical import scope to resolve against, so the same expression would mean different things in a file and at the prompt (principles §4).
- Ruby refinements are the dynamic-language attempt at this, and they have exactly these gaps with
respond_to?.
A library-level registry: Json register: aMapping for: Money
Solve JSON in the JSON library and add nothing to the language. Rejected as the general answer: it is one registry per library, each with its own lookup, reload and ownership behaviour, and x asJson still would not work on the class. It remains a reasonable thing for a single library to do.
Fold cross-file >> within one package
Allow Foo >> sel in another file of the package that defines Foo, and fold it into the class at build time as the same-file fold does. Rejected: "one file is one compilation unit for its class" (docs/beamtalk-language-features.md) is what makes reload replace a class whole, and a cross-file fold would make a class's compiled form depend on which other files the build saw. No package does this today.
Deprecate with a warning for a release, then remove
The usual path for a breaking change, and the previous draft's plan. Rejected by the owner: no package in the survey uses the feature, so a warning period would protect nobody that can be seen, and it would keep both the machinery and its tests alive for another release. The cost is that a private package using it breaks at its next build without warning; the error message carries the migration.
Deprecate in documentation only
Leave the runtime and tell people not to use it. Rejected: the cost is in the dispatch path and the tools, not in user code.
Consequences
Positive
- A send is answered by a compiled method or by nothing (including a runtime fun the class itself was given, Decision §2). The lookup order has no extension step.
- The BT-5002, BT-5060, BT-5061 class of bug cannot recur, and neither can the extension-body codegen bugs (BT-4503, BT-3920), because that codegen is deleted too.
- Loading a package cannot change the behaviour of an existing class.
- A file means the same thing under
beamtalk runand in the REPL: a cross-file>>is an error in both, where today one registers it and the other drops it. - The per-level extension probe leaves
has_method/1, the default dispatch arms and the inherited-send path. beamtalk_extensions.erl(913 lines),gen_server/extensions.rs(479 lines),extension_index.rs(583),extension_conflicts.rs(447) anddependency_extensions.rs(272) are deleted outright (line counts from a research pass).- ADR 0070's recorded limitation, that extension conflicts between two dependencies are unresolvable (line 699), no longer applies.
- The #5718 warning plumbing for the one-hop gap is no longer needed.
Negative
- There is no way to add a method to a class you cannot edit, sealed primitives included. ADR 0066 called extensions "the only way to add methods to sealed primitives"; after this ADR there is none.
asJsonon a foreign class is not possible until a replacement exists. The workaround is a converter in the holder's mapping (Decision §5).- A method of a class without stored source can no longer be added or replaced from another package's build; the route is to rebuild and redeploy the class's package (Decision §4, Open Question 1).
- The type checker gets stricter in one case: with the
dependency_extensions_unknownguard gone, a receiver from a dependency that failed to load is no longer treated as possibly extended (ADR 0100's open-world classification). - The work will be redone if a replacement is added: the comparison in Alternatives shows a replacement needs a miss-path table, registration and load-order handling again.
- This is a breaking language change with no deprecation period. The survey found no user code affected, but it could not see private packages; such a package breaks at its next build.
- 14 test files that define cross-file extensions, three benchmark and snapshot fixtures, 23
stdlib/testfiles and 5 REPL scripts that callbeamtalk_extensionsthrough FFI, and 29 Erlang test files that reference it (reviewer) need rewriting or deleting. Some of them use an extension as a convenient way to set up a different scenario, and need another way to do that. - Deleting about 2,700 lines across dispatch, codegen and the type checker carries regression risk in exactly the code paths that had the bugs.
Neutral
- The class-derived cache and its generation read stay.
- Runtime funs installed through the class (ClassBuilder, ADR 0084) are unchanged.
StandaloneMethodDefinitionandModule.method_definitionsstay in the AST. After the fold, a non-emptymethod_definitionsis an error, so the validators that walk it (the BT-4658 and BT-4923 fixes) have nothing to check and can be removed in follow-up cleanups.- Generated Core Erlang changes for every class (the dispatch default arms and
has_method/1lose a probe), sojust core-diffwill show a diff on every module.
Implementation
Sizes are rough (S, M, L).
Phase 0: inventory (S)
Produce the list that Phases 1 and 2 work from. Known so far:
- Tests that define cross-file extensions (14 files).
stdlib/test: 11 files, 16 target classes (guarded_dispatch_differential_test,class_dispatch_differential_test,set_theoretic_operators_e2e_test,class_side_super_test,actor_extension_error_test,sealed_direct_dispatch_differential_test,value_super_extension_test,adr0133enumerable_user_test,own_class_literal_send_test,value_type_nlr_answer_test,foreign_extension_codegen_test).tests/parity:projects/mixed/src/array_extensions_host.bt,string_extensions_host.bt,diagnostics/lint_verdict_parity/src/shouting.bt. For each, record whether it tests extensions (delete) or uses one to reach another scenario (rewrite). An earlier count of 19 included five REPL scripts that are live edits, not cross-file extensions; those must keep passing unchanged. - Other
.btfixtures.benchmarks/vs-elixir/beamtalk/src/vb_shadowed_math_ext.bt(BT-4431) and the snapshot casestest-package-compiler/cases/foreign_extension/main.btandset_theoretic_operators/main.bt(reviewer, existence checked). - Tests that call
beamtalk_extensionsthrough FFI. 23stdlib/testfiles,lookup_order_matrix_test.btamong them, and 5 REPL scripts:actor_intrinsic_dispatch(uses anActor terminate:extension as an observation hook),adr_0114_live_patch_gap,lookup_order_live,remove_selector,workspace_native_api(read). - Rust tests. 37 Rust test files embed
X >> sel =>in source strings; sort them the same way. - Erlang tests. 29 runtime test files reference
beamtalk_extensionsor thebeamtalk_test_extensionsfixture (reviewer), andbeamtalk_test_boot.erl:99, 264creates the extension tables for every Erlang test (read). - The
beamtalk-uatfixture (SomeActor >> bumpBy:, BT-2452), in another repository. - Non-extension code that depends on the module.
beamtalk_extensions:safe_xref/1is a general helper used bybeamtalk_class_publish.erl:198,beamtalk_class_lifecycle.erl:118andbeamtalk_object_class.erl:2152;beamtalk_runtime_app.erl:31callsbeamtalk_extensions:init()at startup (read). SystemNavigation.stdlib/src/system_navigation.bthas 183 lines that mention extensions. BeyondextendersOf:andextensionsBy:, the extension registry is folded intosendersOf:, the announce-site and FFI-call-site queries,referencesTo:,methodsMatching:, the implementor and selector enumerations and the unsent-selector analysis, through helpers such asextensionSelectorsFor:,liveExtensionsIn:andallExtensionsWithSource(reviewer; the count is read). Each call site is listed here before anything is deleted.- Other runtime and tool modules that mention extensions and are not in the Phase 2 tables yet:
beamtalk_class_dispatch_self.erl,beamtalk_class_metadata.erl,beamtalk_protocol_registry.erl(conformance-cache invalidation),beamtalk_repl_loader_changelog_entries.erl,beamtalk_interface.erl, andcrates/beamtalk-cli/src/commands/doc/extractor.rs(reviewer). The starting point is the 59 Erlang source files and 111 Rust files counted in Context. - Docs that teach cross-file
>>.docs/beamtalk-language-features.md(§ Extension Methods, § Cross-file extensions, "Hot-patching and direct calls", § Live Patching),docs/beamtalk-agent-native-development.md,docs/beamtalk-tooling.md,docs/beamtalk-architecture.md,docs/development/surface-parity.md,docs/development/benchmarks.md, and thestdlib/src/behaviour.bt:496-500doc comment, which says a patch to a stdlib or dependency class installs in memory when the code refuses it (read).examples/has none. - ADRs to annotate. 0066 (done with this ADR), 0070 (
pkg@Target >>, line 699; that line sayspackage@Classdoes not qualify extension targets while the AST hasStandaloneMethodDefinition.package, so settle which is true), 0082 (ChangeLog extension entries), 0084 §5 (cross-class class-side extensions as future work), 0087 (xrefextensionprovenance), 0100 (the WS3 guard in receiver classification), 0101 (cites the extension registry as a layer), 0112 (removeSelector:removes the extension first), 0127 (§3a "foreign extensions count as inherited", §12 extension-conflict row), 0130 (the class-side extension fun shape), 0142 (the BT-4307 resolution) (reviewer for 0082, 0087, 0100, 0101, 0112, 0142). - One check to make. Whether workspace startup activates CLI-built BEAMs, the one path by which an extension could still be registered in a workspace. Done: it does (see Context, "The REPL and the build already disagree"). It needs no separate fix, because Phase 1 stops the compiler producing such modules.
Exit: the list is complete and each item has a disposition.
Phase 1: make cross-file >> an error, and migrate (M)
There is no deprecation warning. One change does all of this:
- The compiler rejects cross-file
>>, instance side and class side, from the one check described under "Single source of truth", inbuild,check,lint,test, the REPL compiler port (Workspace load:and inline evals), MCP lint and the LSP, with the message in Decision §1. The live-definition path is not touched, and a test proves it. - This settles Open Question 1 in practice. Once the compiler refuses a class-side cross-file
>>, nothing can create a class-side extension except raw FFI. Option (b) of that question must be chosen before Phase 1 lands, not before step 2c. - Migrate the tests and the fixtures from the Phase 0 list: the 14 test files, the benchmark and snapshot fixtures, the Rust tests that embed cross-file
>>source, and thebeamtalk-uatfixture (a change in that repository, landed together). Tests that pin extension precedence are deleted with a note of what they pinned. Tests that register throughbeamtalk_extensionsFFI keep working until Phase 2 deletes the module, and are migrated here if they are in the way. - Stop emitting registrations. With no cross-file
>>accepted,register_class/0no longer containsbeamtalk_extensions:registercalls; deletegen_server/extensions.rs. - Docs and release notes. Update the docs from the Phase 0 list and
docs/development/surface-parity.md. Add a breaking change entry toCHANGELOG.mdand the release notes: cross-file>>method definitions are removed; the>>reflection operator, same-file>>and live method definition are unchanged. Annotate the ADRs listed in Phase 0. - Must pass:
just ci,just test-corpus(the PR carriesci:corpus, because codegen changes), and the parity projects reporting the new error identically on CLI, MCP and LSP.
Phase 2: delete the machinery (L)
Three steps, in this order, each its own PR. Phases 1 and 2a may be one PR if that is cleaner.
Compiled code from before this change. Every module built by an older compiler calls beamtalk_extensions:has/2 from its generated has_method/1, with no try around it (dispatch_spec.rs:478), and a module with a cross-file extension calls register/5 on load. Loaded into a runtime without the module, such a BEAM fails with undef on ordinary sends. What prevents that is the build stamp: a build is invalidated when BEAMTALK_VERSION changes (crates/beamtalk-cli/src/commands/build_stamp.rs) (reviewer), so a normal build recompiles everything. Step 2c must confirm that dependency BEAMs built ahead of time and BEAMs a workspace has persisted are covered by the stamp. If they are not, 2c keeps a stub beamtalk_extensions for one release (has/2 answers false, lookup/2 answers not_found, register/5 raises a structured error naming this ADR).
2a. Compiler. Delete extension_index.rs, extension_conflicts.rs, ClassHierarchy::register_extensions (class_hierarchy/mod.rs:850) and its class-side "extension replaces signature" rule (BT-5061, BT-5111), the pkg@Target >> qualifier, and the WS3 plumbing: crates/beamtalk-project/src/dependency_extensions.rs, the dependency_extensions_unknown guard (class_hierarchy/mod.rs, receiver_knowledge.rs, consulted.rs), and its carriers in beamtalk-package (dependency_classes.rs, lint_pipeline.rs, manifest.rs), beamtalk-language-service, beamtalk-mcp (server/lint.rs) and the CLI build commands. LSP and language-service code that indexes extension locations goes with it.
2b. Instance-side runtime.
| Piece | Where |
|---|---|
| Extension probes in generated code | beamtalk_extensions:lookup in the actor and value default arms and the beamtalk_extensions:has step of has_method/1 (gen_server/dispatch.rs:769-786, core_erlang/dispatch_spec.rs:355, 478, supervisor_codegen.rs:239) |
| Interleaved lookups | beamtalk_dispatch.erl: the extension checks in lookup/5, super/5 and lookup_in_class_chain/5; the {ext, Owner} verdict of inherited_target/2; inherited_value_ext/5. check_extension/2 itself stays until 2c, because class-side code still calls it |
| The one-hop gap and its warnings (BT-5060, #5718) | one_hop_children_missing_extension/2, one_hop_parent_extensions_missing/1 (beamtalk_dispatch.erl:339, 368); warn_one_hop_children/2, warn_one_hop_for_class/1, emit_child_warning/3, emit_parent_warning/3 (beamtalk_extensions.erl:316-410); warn_one_hop_gaps/1 (beamtalk_repl_loader_activate.erl:156); with_extension_warning_scope/2 and its '$extension_scope' rows (beamtalk_class_registry.erl:432); the bt5060_live_* fixtures. The slow routing of one_hop_selectors (value_type_codegen/dispatch.rs:308) can then go straight to the superclass's dispatch/3 |
| Guards against an extension overriding a known selector | The String-extension gate on the Binary fast path (binary_or_string_module/2, beamtalk_primitive.erl:1005-1010); the per-level extension probe on the equals: / isKindOf: default-chain path (beamtalk_behaviour_intrinsics.erl:373-482, described in beamtalk_equality.erl:120-140); beamtalk_collection_selectors.erl:90-127 (BT-3951); beamtalk_default_method:note_override/2 on registration (inferred: each needs checking that the extension case is its only reason) |
| Reflection and tooling | respondsTo: and canUnderstand: extension checks (beamtalk_behaviour_intrinsics.erl); the extension-first step of removeSelector: and purge_extensions (beamtalk_class_refactor.erl, beamtalk_class_lifecycle.erl:127-132); SystemNavigation extendersOf: / extensionsBy: and every extension branch in its other queries (Phase 0 list), then regenerate class_hierarchy/generated_builtins.rs; the browse extension provenance (beamtalk_repl_ops_browse_origin.erl); extension xref rows; ChangeLog extension entries (beamtalk_repl_loader_changelog_entries.erl); beamtalk_interface.erl; extension output in beamtalk doc (doc/extractor.rs). These go before the registry is deleted, or sendersOf: and the other queries fail with undef |
2c. Class-side runtime, and the registry. 2b and 2c may be one PR. After Phase 1 the class-side path is reachable only through FFI and its tests are gone, so it should not be left in place for long.
| Piece | Where |
|---|---|
| Class-side extension-first order | class_side_extension/2 (beamtalk_class_dispatch.erl:945) and its callers in handle_class_method_call/6 and walk_class_side; the extension verdict of the owner memo (BT-4800); invoke_class_extension/7 (ADR 0130) (reviewer) |
| Constructor extensions | The class-side-extension half of the new / new: check in beamtalk_object_class.erl:1907 and ancestors_have_user_new1 (beamtalk_class_metadata.erl:1668) (BT-4409, BT-4588). The ADR 0084 runtime-fun half stays |
| Remaining callers | beamtalk_class_dispatch_self.erl; the extension-triggered invalidation of the conformance cache (beamtalk_protocol_registry.erl) and of the class-derived cache; then beamtalk_dispatch:check_extension/2 |
| Shared helper | Move safe_xref/1 out of beamtalk_extensions into beamtalk_xref (or a leaf module below both) before deleting the registry; its three callers are ordinary hot-patch and class-removal paths |
| Startup | The beamtalk_extensions:init() calls in beamtalk_runtime_app.erl:31 and beamtalk_test_boot.erl:99, 264 |
The extension shadow-flag kind | beamtalk_class_shadow_flags.erl, and the direct-call-key invalidation that an extension registration triggers. runtime_fun and class_vars stay |
| The registry itself | beamtalk_extensions.erl, deleted last, with beamtalk_test_extensions.erl |
Tests that must still pass after each step:
just test,just test-corpusandjust verify-threaded-ir. Phase 1 and step 2b change codegen, so their PRs carryci:corpus.ci:core-diffis expected to show a diff on every module in 2b; review a sample.stdlib/test/lookup_order_matrix_test.bt, reduced to the order in Decision §2, for actors, value types, primitives and class-side sends through a literal send,perform:,respondsTo:andsuper.- The same-file fold tests (BT-4721, BT-4489, BT-4799) and
tests/repl-protocol/cases/same_file_extension_redefine.btscript. - Live patching, unchanged:
load_method_patch.btscript,hot_reload_actors.btscript,protocol_live_patch.btscript,release_console_default.btscript, and theClass compile:source:andremoveSelector:tests. A new case: a liveFoo >> selat the REPL succeeds while the same line in a loaded file is an error. SystemNavigationqueries (sendersOf:,referencesTo:,implementorsOf:,methodsMatching:) after the extension branches are removed.- ADR 0084 runtime class-method fun tests, and the BT-4133 / BT-4316 / BT-4317 direct-call tests with the extension cases removed.
just test-repl-protocol,just dialyzer, and thetests/parityprojects, which must report the new error identically on CLI, MCP and LSP.
A benchmark run (docs/development/benchmarks.md) before and after 2b records what the removed probes cost.
Phase 2b and 2c as landed (BT-5141, runtime and stdlib half). 2b and 2c went in one change, as the 2c preamble allowed. Where the plan above differs from the code:
- No
beamtalk_extensionsstub. The build stamp covers what the plan worried about:BEAMTALK_VERSIONcarries the commit (-dev+<sha>) and a dirty-tree tag, the stamp is written per scope (the project and each dependency under_build/deps), and a stamp miss rebuilds that scope. A workspace loads only what the CLI built before it started. A BEAM built by an older compiler therefore never reaches a runtime without the module, and thehas/2,lookup/2andregister/5stub is not needed. - The 2b "inferred" items were checked.
beamtalk_default_method:note_override/2stays: compiled method records still raise theisKindOf:flag, so only its extension reason went (owner/2no longer answers{extension, C}). TheString-extension gate inbinary_or_string_module/2is gone and the shared-selector fast path is unconditional, which is sound because stdlib classes are read-only.beamtalk_collection_selectors.erlkeeps onlyspecies_dependent/0, the runtime copy of the compiler's list thatspeciesConstructionOverriddenFromName/2reads (the Rust test that holds the two equal was narrowed to it);extended_on/1andspecies_construction_extended_on/1had no other reader.respondsTo:andcanUnderstand:reached the registry only throughbeamtalk_dispatch:responds_to/2andselectorProviderFromName/2; both lost the probe. - The BT-5060 warnings are removed by reverting #5718's runtime half, not only the registry side the table lists:
beamtalk_class_registry:with_extension_warning_scope/2andrecord_extension_warning/1,beamtalk_runtime_apiwrappers,beamtalk_refactor_support:note_reply_warning/1,beamtalk_repl_loader_support:load_binary_noting_warnings/3,beamtalk_repl_shell:load_file_with_warnings/3andload_source_with_warnings/2, and theproject_syncplumbing that carried the warnings back. removeSelector:and the ChangeLog. The extension-first step is gone, soremoveSelector:resolves a class-side runtime fun (ADR 0084) before the compiled method.emit_extension_remove_change_entry/7and thenot_flushable_reason: "extension"value are deleted. The not-found message no longer says "(or as an extension)". The xrefextensionprovenance and the browse "extensions" protocol are deleted.- Constructor and shadow-flag details.
constructor_shadowed/2keeps only the ADR 0084 runtime-fun half, andancestors_have_user_new1no longer asks for an ancestornew:extension. Theextensionshadow-flag kind is gone;is_shadowed/2becameis_shadowed/1(keyed by class name).beamtalk_class_shadow_flags:mark_ready/0stays as the readiness gate for the class-side fast path and is now called frombeamtalk_runtime_app:start/2. safe_xref/1moved tobeamtalk_xref, as planned.pkg@Foo >> sel. The Phase 0 note about whetherpackage@Classqualifies an extension target was settled by the code: the AST carriedStandaloneMethodDefinition.package, and the form now parses as an error that names this ADR (2a).- Tests. Tests whose only subject was an extension were deleted. Tests that also covered other behaviour were migrated: the lookup-order matrix (
stdlib/test/lookup_order_matrix_test.bt) is the ADR §2 order with no extension dimension,tests/repl-protocol/cases/lookup_order_live.btscriptcovers hot reload, a liveFoo >> sel,removeSelector:and class removal, and the runtime-fun cases that sat beside the extension cases (put_class_method) stay.
Migration Path
A cross-file extension has no direct replacement, and there is no deprecation period.
| You have | Do this |
|---|---|
Foo >> sel in another file of the package that defines Foo | Move the method into Foo's file |
String >> shout or another addition to a class you do not own | A method on your own class that takes the value: Shout of: aString |
| An extension that overrides a method of a class you do not own | Subclass, if the class is not sealed, or wrap |
| A class-side extension used to patch a class in another package | Fix it in that package and redeploy (Open Question 1) |
Foo asJson on a foreign Foo | A using: converter in the holder's JsonMapping (Decision §5) |
Known affected code outside this repository: the beamtalk-uat fixture only, which is migrated with Phase 1. There is no warning period: a package the survey could not see gets the compile error on its next build, with the hint above. The change is listed as breaking in CHANGELOG.md and the release notes.
Future direction: retroactive protocol implementations
This section is not part of the decision. It records the design worked out in the first draft so that it is the starting point if the need appears.
Trigger. A concrete package that needs to attach a protocol to a class it does not own, for example a package that must make another package's value class JsonRepresentable. One real case also supplies what the design could not settle on paper: who owns what, and so which ownership rule is right.
Outline. A top-level declaration makes a class answer a protocol's selectors, allowed only where it cannot change what a compiled method does:
F1. Syntax
// app/src/Money+JsonRepresentable.bt
JsonRepresentable implementFor: Money
asJson => #{"amount" => self cents, "currency" => self currency}
The header is ProtocolName implementFor: ClassName. The indented body holds method definitions, written as in a class body. A class-side method is prefixed class, as in a class or protocol body.
The owner's sketch ended the header with =>. This draft drops it. Protocol define: X and Object subclass: X are the existing declaration headers and neither takes =>; => introduces one method body everywhere else. This is a recommendation, not a decision.
Either name may be package-qualified as ADR 0070 allows: json@JsonRepresentable implementFor: money@Money.
The file convention from ADR 0066 stays, with the protocol as the feature: Money+JsonRepresentable.bt. One file may hold several implementations.
A provided method comes with the protocol. This is the BT-2819 gap closed:
JsonMappable implementFor: Money
class jsonMapping -> JsonMapping =>
JsonMapping new
|> mapField: #cents to: "amount"
|> mapField: #currency
// Money now answers `asJson`, which JsonMappable provides.
F2. Rule: additive only
An implementation may add selectors. It can never change what a compiled method does.
At compile time:
- The body may define only selectors the protocol declares (required or provided), on the side the protocol declares them.
- A body selector that the class already defines or inherits as a compiled method, on that side, is a compile error. Methods flattened in from a trait (ADR 0127 §3), synthesized accessors and constructors, and (on the class side) the methods of the class object's own chain (
Class,Behaviour,Object) all count as compiled. - After the implementation, the class must conform to the protocol: every required selector resolves, from the body, the class's own methods or the protocol's provisions. Otherwise it is a compile error.
- An implementation that installs nothing, because the class already conforms, is allowed and gets a lint warning that it is redundant.
- Clash between implementations. Two implementations that install the same selector on the same side are a compile error, naming both, when they target the same class, or when they come from different packages and target classes related by inheritance. One package may implement a selector for both
CollectionandList; the nearer one answers. Two packages may not, because loading one would change what the other's sends do. - Known subclasses. If a subclass visible in the build already compiles an implemented selector with a signature incompatible with the protocol's, the build warns: the subclass is now declared to conform and does not.
error: JsonRepresentable implementFor: Money defines `asJson`, but Money already
defines `asJson` (money/src/money.bt:14).
= an implementation can add selectors to a class; it cannot replace one.
hint: delete this method; Money's own `asJson` already answers.
Provided methods. Every provision of the protocol is registered for the class, whatever the class already understands. Miss-only lookup makes that safe: a provision the class already answers with a compiled method never runs. For typing and reflection the compiler computes which provisions are shadowed.
- A provision shadowed by a method in the class's own body is silent, as class-wins is in ADR 0127 §3.
- A provision shadowed by an inherited method gets a warning naming both.
- This differs from
uses:on purpose. Under ADR 0127 §3a a provision may replace an inherited method when the class writesoverriding:, and aprintStringprovision replacesObject's without it. UnderimplementFor:a provision never replaces anything.Describable implementFor: Moneydoes not changeMoney printString;uses: DescribableinMoney's body does. - Registering every provision also covers version skew: if the owner later removes the method that shadowed a provision, the provision answers instead of the send failing.
At run time:
- Implemented methods live in a protocol-implementation table. It is consulted only where the root
doesNotUnderstand:args:would otherwise raise: the compiled lookup has missed on the receiver's class and every ancestor, and no class on the chain belowProtoObjectcompiles its owndoesNotUnderstand:args:. A compileddoesNotUnderstand:args:is a compiled answer. SoErlang,ErlangModuleand forwarding proxies keep receiving every miss, and an implementation forActororObjectdoes not reach them. - The table is searched from the receiver's class upward, so an implementation for
Collectionanswers forList. - A compiled method anywhere in the chain is found first. There is no interleaving, and the class side follows the same order as the instance side. On the class side the table comes after the class object's own chain.
- Rows are registered by class name. The class need not be loaded yet, because module activation orders by superclass only (reviewer). When a class activates, the runtime re-runs the additive check for its rows, as
warn_one_hop_for_class/1did for extensions. - If a class gains a compiled method for an implemented selector (the owner ships a new version, or the class is hot-reloaded), the compiled method answers. The next build of the implementing package fails with the error above. At run time the dead row is reported once as a warning.
- A run-time clash does not depend on load order. If two packages that were never built together register clashing rows (possible in a REPL or after
:sync), neither answers: the send raises a structuredambiguous_implementationerror naming both, until one is removed. The registration is also reported in the reply warnings.
The guarantee, stated exactly. With cross-file >> already gone, loading a package cannot change the result of a send that a compiled method answers. It can make a send answer that used to raise does_not_understand, which is the purpose, so code that branches on respondsTo: or catches DNU can observe it. It cannot change which implementation another package's send reaches, because of the clash rule.
This keeps the property the removal buys: no dispatch path, static call or cache has to ask whether something outranks a compiled method.
The cost is real and deliberate: an implementation cannot customise a selector that Object or another ancestor already defines. Printable implementFor: Money with a printString body is an error, because Money inherits printString.
F3. Rule: ownership
An implementation is allowed only if the declaring package owns the protocol or the class.
Definition of "owns". Each class and protocol resolves to exactly one owner, and the check compares that owner with the declaring package:
| Entity | Owner |
|---|---|
A class or protocol in a file compiled under a beamtalk.toml | That manifest's [package] name |
| A class or protocol carried in from a dependency | The dependency's package name (ClassInfo.package, ProtocolInfo.package) |
A stdlib class or protocol, including bootstrap builtins whose ClassInfo.package is None (such as Future) | stdlib |
| A class or protocol defined at the REPL, or in a script outside any project | The workspace's package |
| Anything whose owner cannot be resolved | Unknown. An unknown owner never matches, so the implementation is refused |
The declaring package is the [package] name the file is compiled under, or the workspace's package at the REPL.
This needs one resolver, written once, because None in ClassInfo.package means "REPL classes and bootstrap builtins" (class_info.rs:330-333) while None in ProtocolInfo.package means the package being compiled (protocol_registry.rs:141-145) (read). It also needs every protocol's signatures and owner to reach consumers. Today only protocols with provided methods, and protocols named like a stdlib trait, are carried into other compiles (is_carried_protocol, trait_expansion.rs:567) (read), so a requirements-only protocol such as JsonRepresentable may reach a consumer without its requirement list or owner (reviewer).
So a user package may implement its own protocol for String. It may not implement stdlib's JsonRepresentable for another package's Money.
error: package `app` cannot implement JsonRepresentable (owned by `stdlib`)
for Money (owned by `money`): it owns neither.
hint: ask `money` to implement it, or declare a protocol in `app`.
In an acyclic dependency graph, at most one package can satisfy the rule for a given protocol and class from each side, and both sides at once would need the two packages to depend on each other. So for any protocol and class in different packages there is exactly one package that may write the implementation without a circular dependency, plus the degenerate case where one package owns both.
What the rule guarantees, and what it does not. In Rust the orphan rule gives coherence because a method belongs to its trait. Beamtalk selectors are global and conformance is structural, so the rule is much weaker here. The owner should decide with this in view:
- It guarantees that every added selector has a named, owned contract.
- It does not stop the thing it was proposed to stop. An app that owns neither side can write
Protocol define: AppJsonwith anasJsonrequirement and implement that forMoney. AfterwardsMoney asJsonworks,Json generate:finds it, andMoney conformsTo: #JsonRepresentableanswerstrue, because conformance is structural. Two unrelated packages can each do this forString. - For
JsonMappablethe workaround is worse: the local protocol has to copy the providedasJsonbody, which is the duplication this project's rules forbid. - What actually prevents two packages from both adding
String asJsonis the clash rule in F2, at the consumer's build. The consumer still cannot repair a clash between two dependencies (the limitation ADR 0070 already records at line 699).
As written, then, the ownership rule costs ceremony and protects little. The three coherent positions are listed under "Questions to settle then" below. Choosing between them needs a real case, which is one reason this design is deferred.
F4. Rule: declared conformance
An implementation is a declaration that the class conforms.
- Type checker. The implementation's selectors join the class's method surface, and the class conforms to the protocol, so a
Moneyis accepted where:: JsonRepresentableis expected. Conformance in Beamtalk is structural (ADR 0068), so this mostly follows from the selectors being visible. The new part is the completeness check in F2. A compiled signature always beats an implementation's signature, on both sides. Implemented methods are tagged as such in the class's method table, so static calls, guarded direct calls and sealed self-sends never target them (BT-4133 is the precedent for what goes wrong otherwise). - Runtime.
Money conformsTo: #JsonRepresentableanswerstrue.conforms_to/2(beamtalk_protocol_registry.erl:434) usesclassCanUnderstandFromName, which will consult the implementation table after the compiled walk. - Reflection. Added beside the ADR 0127 §12 surface:
Protocol implementationsOf: #JsonRepresentable(the classes with a declared implementation) andBehaviour implementedProtocols.CompiledMethod originanswers the protocol, as it does for a flattened trait method.usersOf:stays reserved foruses:. - Browse. Implemented methods are grouped under the protocol's name with a new
provenance := implementationrow, where theextension → "extensions"branch used to be (protocol_from_source/4,beamtalk_repl_ops_browse_origin.erl:275).
F5. Rule: call sites unchanged
x asJson is an ordinary send. respondsTo:, canUnderstand:, perform: and super see an implemented method, after compiled methods. The respondsTo: #asJson hook in beamtalk_json.erl:344-350 needs no change.
>> (Money cents: 250 currency: "EUR") asJson
=> #{"amount" => 250, "currency" => "EUR"}
>> Money conformsTo: #JsonRepresentable
=> true
>> (Money >> #asJson) origin
=> #JsonRepresentable
F6. Class-side requirements
implementFor: supports the protocol's class-side requirements (BT-1611, ADR 0144), written class selector => body. The same rules apply on the class side: additive only, miss-only lookup after the class-method chain. After the implementation the class conforms on both sides, so it is accepted where P class is expected.
A class-side implemented method is compiled as the class-side extension fun was, fun(Args, ClassSelf) -> Result (ADR 0130 §3). It runs where a class method of the target class runs under ADR 0142: in the caller, unless the class or an ancestor declares class variables or the selector is process-bound, in which case it runs in the class process. That is what a class-side extension did (ADR 0142, BT-4307).
Class-side provisions are still post-v1 in ADR 0127 §1; this ADR does not change that.
F7. What an implementation body may do
- Public interface only. A body may send messages to
self. It may not read or writeself.field, and a class-side body may not touch the target's class variables. This is the sketch's default and is not decided. The reasons: the implementer usually does not own the class, a field rename in the owner's minor release would otherwise break the implementation at run time with no build error, and ADR 0127 §7 already makes provided methods stateless. - Actors. An implemented method on an
Actorclass runs inside the actor's process, as a foreign extension did, and itsselfsends are same-process sends. It would use the old extension fun shape,fun(Args, Self, State) -> {Result, NewState}(gen_server/extensions.rs:27-34), because a self-send can change state even when the body cannot touch slots.JsonMappable's provision relies on same-process self-sends. ^and blocks. Bodies need the body codegen this ADR deletes withgen_server/extensions.rs, or a successor. That code had its own bug history (BT-4503, BT-3920). A future implementation must run the^-in-block and error-propagation cases against value, actor and class-side implementations.
F8. Hot reload and removal
- Reloading the implementing file replaces the rows that module owns. A method deleted from the file stops answering. A deleted cross-file extension used to keep answering until
removeSelector:or a restart. - Deleting the implementing file and running
:syncpurges the rows of a module that is gone. - Re-evaluating an implementation from the same package replaces it. A clashing implementation from a different package makes the selector ambiguous (F2). There is no last-writer-wins and no first-writer-wins.
- Reloading the class changes nothing unless the class now compiles an implemented selector. Then the compiled method answers, and a warning names the dead implementation row.
- Editing the protocol (a provision or a requirement) re-expands implementations with the users, under ADR 0127 §11's two-stage rule: compile everything, then load.
Money removeSelector: #asJsonon an implemented selector is an error, as it is for a trait-provided selector (refuse_if_protocol_provided/4,beamtalk_class_refactor.erl:267-276): "asJsonis implemented for Money byJsonRepresentable implementFor: Money(packageapp); remove the implementation instead." Removing one selector would leave a declared conformance that is false.- Removing an implementation is one operation on the whole unit:
Protocol removeImplementationOf: #JsonRepresentable for: #Money(spelling not decided). It records a ChangeLog entry as extension removal did. removeFromSystemon the class or the protocol purges its implementation rows, aspurge_extensionsdid.
F9. Questions to settle then
- The ownership rule. As stated in F3 it can be sidestepped with a local protocol, and it blocks the plain form of the motivating case. Three coherent positions:
- (a) Selector-based, with a root-package exemption. A package may not make a foreign class answer a selector that a dependency's protocol declares, unless it owns that protocol or the class. The root package of a build is exempt, because nothing depends on it; an orphan implementation in a dependency is an error. This is the only option that does what the rule was proposed to do. "Root package" means the package being built, not a package with an
[application]section; a library can have one. - (b) As stated in F3. Simple, and documents intent, with the loophole accepted.
- (c) A lint. No hard rule; warn on an orphan implementation in a library, as Clojure's guidance does. The clash rule in F2 remains the real protection. This strengthens the case for additive-only
>>instead.
- (a) Selector-based, with a root-package exemption. A package may not make a foreign class answer a selector that a dependency's protocol declares, unless it owns that protocol or the class. The root package of a build is exempt, because nothing depends on it; an orphan implementation in a dependency is an error. This is the only option that does what the rule was proposed to do. "Root package" means the package being built, not a package with an
- Header syntax. With or without a trailing
=>; whether there is also a reflective message form for live use (principles 6 and 11); the removal selector. - Run-time clash behaviour. Ambiguous (neither answers, F2) or first-registered-wins with a warning.
- Slot and class-variable access from implementation bodies (F7), and whether the class's own package gets more.
- REPL strictness. Whether the ownership rule applies at the REPL.
methodsandincludesSelector:. Whether they list implemented selectors.- Whether this is the right replacement at all. Additive-only
>>and pure protocol dispatch (Alternatives) should be re-weighed against the real case.
A first implementation step would be a wire check: register a hand-written fun in a miss-only table through FFI, hook the root DNU path, and run the lookup-order matrix for a value class, an actor, a primitive, a class-side send, a class with its own doesNotUnderstand:args:, respondsTo:, perform: and super. DNU handling is generated per class today (gen_server/dispatch.rs:986-1021) (read), so whether the hook has one choke point is a real question.
Open Questions
-
Patching a method of a class with no stored source. After removal, a method of a stdlib or dependency class cannot be added or replaced from another package; the route is to fix the owning package and redeploy (Decision §4). Options:
- (a) Accept the loss. Remove the class-side extension path with the rest. File follow-ups for the two gaps the survey of the patch paths found: MCP
save_methodandClass compile:source:have no class-side form althoughinstall_method/9supports one, and the workspace stores no source for dependency classes, so they cannot be patched live at all. - (b) Keep class-side cross-file
>>(compiler and runtime) until the workspace can patch a dependency class live. This keeps theextensionshadow flag, the extension-first order and the registry.
Default taken by the implementation: (a). The reasons: no package uses a class-side extension; a workspace already drops a cross-file
>>on load, so the capability exists only through a rebuild, which is what (a) asks for anyway; and (b) keeps most of the class-side cost this ADR exists to remove. Because Phase 1 makes class-side cross-file>>a compile error, choosing (b) means changing Phase 1, so the owner should say so before Phase 1 lands. - (a) Accept the loss. Remove the class-side extension path with the rest. File follow-ups for the two gaps the survey of the patch paths found: MCP
Decided, and recorded here:
- Sequencing. Remove cross-file
>>now; deferimplementFor:until a concrete need appears (Future direction). - Deprecation. None. Cross-file
>>becomes an error in one step, because no package in the survey uses it. - REPL output. A file load or inline eval containing a cross-file
>>fails with the compile error of Decision §1.
References
- Related issues: BT-5060 (#5718), BT-5002, BT-5061, BT-5062, BT-4307, BT-4588, BT-4250, BT-4133, BT-3951, BT-4503, BT-3920, BT-4260, BT-4280, BT-4800, BT-4984, BT-4721, BT-4610, BT-4608, BT-4611, BT-4658, BT-4923, BT-2819 (
JsonMapping,JsonMappable; #5717, #5719), BT-2452 - Related ADRs: ADR 0066 (cross-file part superseded), ADR 0068 (structural protocols), ADR 0070 (packages;
pkg@Target >>), ADR 0084 (runtime class-method funs), ADR 0127 (traits), ADR 0130 (class-side extension fun shape), ADR 0142 (where class methods run), ADR 0144 (class-side protocol types) - Code:
runtime/apps/beamtalk_runtime/src/beamtalk_extensions.erl,beamtalk_dispatch.erl,beamtalk_class_dispatch.erl,beamtalk_class_shadow_flags.erl;runtime/apps/beamtalk_workspace/src/beamtalk_repl_loader.erl,beamtalk_repl_loader_methods.erl;crates/beamtalk-core/src/compilation/same_file_fold.rs,extension_index.rs,extension_conflicts.rs;crates/beamtalk-codegen/src/core_erlang/gen_server/extensions.rs;crates/beamtalk-project/src/dependency_extensions.rs;stdlib/test/lookup_order_matrix_test.bt - Prior art: Rust RFC 2451, Swift SE-0364, Elixir
Protocol, Clojure protocols, Haskell orphan instances, C# extension methods, Ruby refinements, Apple QA1908