Skip to content

in and instanceof on a union of interfaces - #737

Merged
ASDAlexander77 merged 1 commit into
mainfrom
interface-union-in
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
interface-union-in

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

in and instanceof now work on a union of interfaces.

For a: Fish | Bird, "swim" in a was "not supported", and a instanceof Fish was always false: it compared the type Fish | Bird with Fish. Both members are interfaces, and an interface's union tag always reads "interface", so nothing at run time said which interface the union held.

  • Compile time: the compiler works out which members have the key, or which member is the instanceof interface. If every member qualifies, the result is the constant true; if none does, false.
  • Run time: when only some members qualify, the union's tag is checked against just those members. An interface's type descriptor now has an id of its name in TYPE_DESCR_LAYOUT, the slot tuples use for their layout id (any to object type checks fields; own: literal with capturing arrows given to an object type (spec 66) #734). The new op ts.IsTypeTagOf compares a tag's id with an interface's. The id comes from the name, so it matches across modules.
  • Narrowing: if ("k" in a) narrows a to the members that have the key, and the else branch to the others, as instanceof already did.

Test: 00in_interface_union (compile and JIT; gc, rc, none, own). It covers two- and three-member unions, else narrowing, instanceof, and keys that every member or no member has.

Validation: ctest 6327/6327. DefaultLib release JIT: gc 181/181, rc and none 180 (1 skipped), own 170 (11 skipped).

🤖 Generated with Claude Code

`"swim" in a` for `a: Fish | Bird` was "not supported", and
`a instanceof Fish` was always false (the types compared, `Fish | Bird`
against `Fish`). Every interface's union tag reads "interface", so
nothing at run time said which interface the union held.

Which members have the key, or are the interface, is decided when
compiling: every one or none folds to a constant. Otherwise the union's
tag is asked: an interface's descriptor now has an id of its name in
TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's.
`"k" in a` narrows `a` to the members with the key (and the `else` to
the others), as `instanceof` already narrowed.

Test: 00in_interface_union (compile and JIT; gc, rc, none, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 21d0bfe into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the interface-union-in branch October 29, 2026 15:32
ASDAlexander77 added a commit that referenced this pull request Oct 29, 2026
…s; design notes (#738)

* `in` and `instanceof` on a union of interfaces

`"swim" in a` for `a: Fish | Bird` was "not supported", and
`a instanceof Fish` was always false (the types compared, `Fish | Bird`
against `Fish`). Every interface's union tag reads "interface", so
nothing at run time said which interface the union held.

Which members have the key, or are the interface, is decided when
compiling: every one or none folds to a constant. Otherwise the union's
tag is asked: an interface's descriptor now has an id of its name in
TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's.
`"k" in a` narrows `a` to the members with the key (and the `else` to
the others), as `instanceof` already narrowed.

Test: 00in_interface_union (compile and JIT; gc, rc, none, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An object literal or class object given to a union of interfaces

`move({ swim: () => "s" })` for `move(a: Fish | Bird)` failed with
"'ts.Cast' op type [...] can't be stored in [...]", and so did a class
object. castToTaggedUnionType now casts such a value to the first
interface member whose every required field and method it has
(interfaceMemberFitting, by name, as `"k" in v` asks), and the union's
tag says which (`"swim" in a`, #737).

Test: 00interface_union_literal (compile and JIT; gc, rc, none, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Fewer-parameter callbacks: generic inference and a thunk's owned result

Two bugs in the fewer-parameters thunk (#733, #735), found by giving the
default library's array callbacks an index parameter:

- A closure given for a generic callback `(v: T, i: number) => void` had
  its captures paired with `T` (`string | Reference<{...}>`): its type is
  its function bound over its captures. Inference now drops the bound
  first parameter, of the closure's type and of the arrow type rebuilt.
- Under -mm=own a heap result the thunk hands on (a string a `map`
  callback builds) was "a second reference": the thunk's call is now
  marked owned and consumed by its return, as `return f(x)` is.

Test: 00closure_fewer_parameters (a generic callback, a string result).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Design notes: generic interface methods, classes as values

- docs/generic-interface-methods-design.md: generic methods in interfaces
  (rejected today) and in class hierarchies (not virtual: an override is
  never reached through a base reference, silently). Options: erased
  `any` slots, a closed-world table, dictionary passing; recommends an
  error for overridden generic class methods now, erased slots later.
- docs/classes-as-values-design.md: `typeof A` values (ts.ClassRef has no
  value lowering) and local classes that capture (members lifted with an
  extra parameter nobody passes). Recommends the vtable as the class
  value with a `.constructor` slot, and the captures in a hidden
  `.captured` field of the instance.
- docs/tuple-object-shape-ambiguity-design.md: re-checked, still open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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