Repository navigation
Mixins as the handbook writes them: a generic function given a class extends it - #774
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mixins as the TypeScript handbook writes them now compile and run:
Scale(Sprite)specializesScalefor the classSprite, so the returned class extendsSpriteat compile time with an ordinary layout and vtable. The specialization already built the right class. Four things got in the way: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.classNamesGivenToTypeParameters): a class name given to a parameter declared as a bare type parameter has no run-time value of the class type thatTis inferred as (ts.ClassReffailed to legalize). The call passes a null of that type, and the specialization never reads it. A parameter typed otherwise (f(a: A)called asf(A)) keeps today's behaviour, an error.extendsTypeagainst a construct signature): a class without a constructor of its own is made bynew A(). A method without a return type matches avoidone, as the handbook'ssetPosneeds.function make(B: typeof A) { return class extends B {} }): still an error, now one that names the generic form.typeof Amay hold a subclass laid out otherwise, which is also whyTis inferred as the class itself rather thantypeof A.Not supported, as recorded in
docs/classes-as-values-design.md§6:Scale<typeof Sprite>(Sprite),applyMixins, a module-levelconst C = Aor alet C = A, and an integer-initialized field (x = 0, which iss32) against a{ x: number }constraint.Tests
00mixins: constructor and constrained mixins (field and method constraints), chains, a base without a constructor,supercalls,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.ctest -C Release: 6664 tests, all passing after the verifier skip-list fix.🤖 Generated with Claude Code