ADR 0144: Class-Side Protocol Types — P class Means "a Class Conforming to P"

Status

Implemented (2026-11-05); accepted 2026-11-05, proposed 2026-11-04. Driven by BT-4413.

Context

Problem statement

A common Smalltalk idiom passes a class around as a strategy and calls its class-side methods. Catalina's transports are the clearest case in this codebase. TCPTransport and SSLTransport are sealed classes with class methods only. A Socket or ServerConfig holds one of the two classes and calls transport send: socket data: bytes on it. The Transport protocol already declares that contract with class requirements (BT-1611):

Protocol define: Transport
  class listen: port :: Integer options: options :: List(Symbol | Tuple) -> Result(Port | Tuple, Object)
  class send: socket :: Object data: data :: Object -> Result(Nil, Object)
  class close: socket :: Object -> Result(Nil, Object)
  // ...

There is no type for the field that holds the transport. Both obvious spellings fail (BT-4413, checked on main a202f85):

So Catalina declares these fields :: Dynamic. That means:

Current state

What exists already:

What is missing is the class-side mirror of the last item: a type that accepts any class object whose class conforms to P, and resolves sends against P's class requirements.

Constraints

Decision

The rule

For a protocol P, the type annotation P class denotes the class object of any class that conforms to P.

C class <: P class      iff      C conformsTo: P      (structural, as today)

This is the class-side mirror of the existing instance rule, and the two read the same way:

AnnotationDenotesAccepts
C (class)an instance of C or a subclassC new, …
C class (class)the class object of C or a subclass (ADR 0083)C, a subclass of C
P (protocol)an instance of any class conforming to Pany conforming instance
P class (protocol)the class object of any class conforming to Pany conforming class

"Conforms" is exactly the existing check (ProtocolRegistry::check_conformance): every instance-side selector of P (required and provided) resolves on C's instance side, and every class requirement resolves on its class side. Since there is still only one conformance rule, a class that conformsTo: #Transport at run time is exactly a class the compiler accepts for Transport class, as far as the compiler can see its methods.

Sends to a P class value

A send to a value of type P class resolves, in order:

  1. against P's class requirements (and those of protocols P extends), using their declared parameter and return types;
  2. then against the class-object protocol every class answers (Class → Behaviour → Object): name, printString, respondsTo:, conformsTo:, …, exactly as for any metatype (ADR 0083).

A selector found in neither is reported as not understood, at the same severity as for a P-typed instance today. new and basicNew on a P class value infer P (an instance of some conforming class), not Dynamic, when P declares no class new requirement of its own, and stay Dynamic otherwise, matching ADR 0083's rule for abstract classes.

Object subclass: Socket
  field: socket :: Port | Tuple
  field: transport :: Transport class = TCPTransport

  send: data :: Object -> Result(Nil, Object) =>
    result := self.transport send: self.socket data: data   // checked, typed
    result isOk ifTrue: [self countSent: data]               // `isOk` on Result
    result

Assignment and errors

Assigning a class that does not conform reports the missing selectors, using the same wording the existing conformance warning uses:

> field: transport :: Transport class = Integer
warning: Integer class does not conform to Transport class
  missing: class listen:options:, class accept:, class send:data:, …

A class literal infers C class (ADR 0083), so TCPTransport flows into a Transport class field with no annotation at the use site. A P class value flows into :: Class and :: Behaviour (it is a class object). The reverse, Class into P class, is not provable and is reported like any other narrowing assignment (narrow with conformsTo: or match).

Open-world rules are unchanged (ADR 0100): a class whose method surface the checker cannot see completely (a cross-file superclass, doesNotUnderstand:, a parse-error file) conforms by default, as it already does for :: P.

Unions, Nil and generics

What P class used to mean

Today P class for a protocol P resolves to the metatype of the protocol object (Meta{P}), whose class-side methods are those of the protocol's own class (Protocol reflection). No code in the stdlib, the examples, the tests or any jamesc/beamtalk-* package writes :: P class for a protocol P (checked 2026-11-04). The reflective use, a value that is a protocol object, is spelled :: Protocol and is unaffected.

Out of scope

Prior Art

LanguageMechanismWhat we takeWhat does not transfer
Pharo / SqueakClasses are objects; class-side strategies are duck-typed (aTransport send: ...).The idiom itself, unchanged.No static checking at all.
NewspeakClasses are first-class and structural.Structural conformance.No metatype annotations.
SwiftP.Type / any P.Type metatypes; protocols declare static requirements, and T.Type conforms when T: P.The exact relation: a metatype conforms when its instance type conforms; static requirements are callable on the metatype.Swift's existential/opaque distinction (any/some) is not needed under gradual typing.
KotlinA companion object can implement an interface, so the "class" passed as a value is that companion.The goal: a class-level value satisfying an interface.Needs a separate companion object; Beamtalk's metaclass already is that object.
TypeScriptInterfaces cannot declare static members; a separate "constructor interface" is written by hand.The motivation: the hand-written workaround is the pain point.Two declarations per contract; we keep one protocol.
RustTraits with associated functions; a type parameter T: Transport calls T::send(..).Associated functions belong to the contract.Monomorphised generics, not values; nothing to store in a field.
Elixir / Erlang@behaviour Transport and a module atom passed as a value (Thousand Island does exactly this).The BEAM shape of the idiom: a module as a strategy.Dialyzer types the field as module() and checks nothing about the behaviour.

Swift is the closest: P class here is Swift's any P.Type, with structural instead of nominal conformance.

User Impact

Steelman Analysis

A. P class means "a class conforming to P" (chosen)

B. A new spelling, Class(Transport)

C. :: P also admits class objects when P has only class requirements

D. Status quo (Dynamic plus annotated wrapper methods)

Tension points

Alternatives Considered

Class(P) — a parameterised Class type

Rejected. ADR 0068 makes metatypes name-only on purpose, and a parameterised Class would be the first parameterised metatype. It also introduces a second way to say "a metatype" (C class and Class(C)), and the only thing it protects is the current meaning of P class, which nothing uses.

:: P admits conforming class objects when P is class-only

Rejected. The meaning of :: P would depend on whether P happens to declare any instance requirements, so adding one instance requirement to a protocol would silently retype every :: P field from "a class" to "an instance". A mixed protocol has no answer at all.

Keep Dynamic, type results through wrapper methods

Rejected as the answer, kept as today's workaround. Wrapping each transport call in an annotated method (rawSend: data -> Result(Nil, Object) => self.transport send: ...) types the result but checks nothing about the transport, and costs a method per call.

Nominal conformance (uses: Transport required)

Rejected. Every existing protocol type is structural (ADR 0068, BT-2784); making the class-side one nominal would split the rule.

Consequences

Positive

Negative

Neutral

Implementation

All in beamtalk-core's type checker; no codegen or runtime work.

  1. Resolution (type_checker/type_resolver.rs): DeclaredType::ClassOf(n) where n names a protocol in the ProtocolRegistry resolves to a new protocol metatype, for example InferredType::Meta with a protocol: true marker or a sibling ProtocolMeta { protocol_name } variant; the variant shape is an implementation choice, the rule is the Decision's.
  2. Assignability (type_checker/validation.rs, is_assignable_to*): Meta{C} → P class holds iff ProtocolRegistry::check_conformance(C, P) is Ok, with the ADR 0100 open-world gate it already applies. P class → Class / Behaviour / Object holds. Unions and intersections fall out of the existing union and intersection arms.
  3. Send resolution (type_checker/validation.rs and protocol.rs): a receiver of type P class resolves selectors against ProtocolInfo::all_class_requirements first, then the metatype fallback chain ADR 0083 already uses; parameter and return types come from the requirement's declared signature.
  4. Diagnostics: the conformance warning names the missing class selectors (the existing missing_conformance_selectors output already prefixes them with class ).
  5. Catalina (jamesc/beamtalk-catalina): Socket, ServerConfig and HandlerConfig change transport :: Dynamic to transport :: Transport class, closing BT-4413's typing half. Measure echo_c10_64B before and after.
  6. Docs: docs/beamtalk-language-features.md § Metatypes and § Class Method Requirements gain the P class row and an example.

Tests: type-checker unit tests for each rule above (conforming and non-conforming assignment, sends resolved from class requirements, the Class/Behaviour fallback, | Nil narrowing, intersection, open-world receivers), a BUnit or package-compiler fixture with a two-implementation strategy, and an LSP completion test.

Size: M.

Implementation Tracking

Epic: BT-4434 Status: Done (2026-11-05)

PhaseIssueTitleSizePR
1BT-4435Type P class as the class object of a conforming classM#4832
2BT-4446beamtalk test loads src/ and dependency protocols (found by BT-4437)S#4839
2BT-4447Actor self-sends exempt from the sendability check (found by BT-4437)S#4840
2BT-4436LSP completion/hover and REPL e2e for P class valuesS#4842
2BT-4437Catalina: type transport fields as Transport classSjamesc/beamtalk-catalina#36

Result (Catalina echo_c10_64B): beamtalk_message_dispatch:send/3 per message went from 3 to 2 (result isOk after a transport send is now a direct call), and untyped dynamic sites went from 61 to 41. The class-side sends to the transport itself stay dynamic; making them direct calls is a future ADR 0025 amendment (BT-4413).

Follow-ups: BT-4440 (check C class / P class return annotations), BT-4441 (return-type refinement in P class <: Q class), BT-4444 (block value: arguments against annotated parameters), BT-4445 (distinct internal name for ProtocolMeta), BT-4449 (REPL warning hints), BT-4450 (LSP registry cloning and signature help).

Migration Path

None required. No existing code writes :: P class for a protocol P, and the change only adds checking where the annotation is written.

References