Skip to content

Mixins as the handbook writes them: a generic function given a class extends it - #774

Merged
ASDAlexander77 merged 1 commit into
mainfrom
mixins
Nov 6, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
mixins

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Mixins as the TypeScript handbook writes them now compile and run:

type Constructor = new (...args: any[]) => {};
function Scale<TBase extends Constructor>(Base: TBase) {
    return class Scaling extends Base { _scale: number = 1; };
}
const EightBitSprite = Scale(Sprite);

Scale(Sprite) specializes Scale for the class Sprite, so the returned class extends Sprite at compile time with an ordinary layout and vtable. The specialization already built the right class. Four things got in the way:

  • "null operand found" (mlirGenClassHeritageClause): the heritage clause wanted only the base's type. A base named by a parameter, though, was read by a load emitted at module level, outside the function that owns the parameter. The load is erased.
  • The class argument (classNamesGivenToTypeParameters): a class name given to a parameter declared as a bare type parameter has no run-time value of the class type that T is inferred as (ts.ClassRef failed to legalize). The call passes a null of that type, and the specialization never reads it. A parameter typed otherwise (f(a: A) called as f(A)) keeps today's behaviour, an error.
  • The constraint (extendsType against a construct signature): a class without a constructor of its own is made by new A(). A method without a return type matches a void one, as the handbook's setPos needs.
  • A run-time base (function make(B: typeof A) { return class extends B {} }): still an error, now one that names the generic form. typeof A may hold a subclass laid out otherwise, which is also why T is inferred as the class itself rather than typeof A.

Not supported, as recorded in docs/classes-as-values-design.md §6: Scale<typeof Sprite>(Sprite), applyMixins, a module-level const C = A or a let C = A, and an integer-initialized field (x = 0, which is s32) against a { x: number } constraint.

Tests

  • 00mixins: constructor and constrained mixins (field and method constraints), chains, a base without a constructor, super calls, instanceof, a class declared in a generic function. Runs compiled and under JIT with gc, rc, none and own.
  • mixin_runtime_base_error: the error, added to the ownership verifier's skip list.
  • Full ctest -C Release: 6664 tests, all passing after the verifier skip-list fix.

🤖 Generated with Claude Code

…extends it

`function Scale<TBase extends Constructor>(Base: TBase) { return class extends Base {...} }`
called as `Scale(Sprite)` specializes Scale for the class Sprite, so the class it returns
extends Sprite at compile time. Four things stood in the way:

- The heritage clause wanted only the base's type, but a base named by a parameter was read
  by a load emitted at module level ("null operand found"). The load is erased.
- A class name given to a parameter declared as a bare type parameter has no run-time value
  of the class type T is inferred as; the call passes a null of that type, which the
  specialization never reads. A parameter typed otherwise (`f(a: A)` called as `f(A)`) is
  left as it was.
- Against a construct signature, a class without a constructor of its own is made by
  `new A()`, and a method without a return type matches a void one.
- A class extending a `typeof A` value (known at run time only) gets an error naming the
  generic form instead of "class cannot extend type".

T stays the class itself, not `typeof A`: a `typeof A` value may hold a subclass laid out
otherwise. Tests 00mixins (gc, rc, none, own) and mixin_runtime_base_error; the design is in
docs/classes-as-values-design.md section 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 01f6d37 into main Nov 6, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the mixins branch November 6, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant