Skip to content

An arrow that captures can take fewer parameters than its function type - #735

Merged
ASDAlexander77 merged 1 commit into
mainfrom
closure-fewer-params
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
closure-fewer-params

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

An arrow that captures can now take fewer parameters than the function type it's given as. For example, apply((a: number) => a + k) for apply(f: (a: number, b: number) => number), or each(names, (name: string) => { count += name.length; }).

  • Before: an error with a misleading message, "it takes 2 argument(s), 2 expected", because the captured variables counted as a parameter. A finally that throws during unwinding replaces the exception; fewer-parameter functions as wider function types #733 covered only named functions and arrows that capture nothing.
  • How: fewerParametersThunk also takes a closure. The thunk takes the captures first, then every parameter of the target type, and calls the arrow with the captures and its own parameters. The closure is rebound to the thunk in place, so its captures keep the same owner (rc's retain and release, own's box).
  • Test: 00closure_fewer_parameters (compile and JIT; gc, rc, none, own). It captures a number and a class object, and uses the captured object after the calls.

Not changed: typeof of an interface value stays "interface". tslang's typeof reports its own kind names throughout ("class", "tuple", "s32"), and its generated code dispatches on those names.

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

🤖 Generated with Claude Code

`apply((a: number) => a + k)` for `apply(f: (a: number, b: number) =>
number)` was "it takes 2 argument(s), 2 expected": the arrow's captures
counted as a parameter, and #733's thunk covered only named functions
and arrows that capture nothing.

fewerParametersThunk also takes a closure (ts.CreateBoundFunction with
__owns_capture over a symbol): the thunk takes the captures first, then
every parameter of the type, and calls the arrow with the captures and
the first ones. The closure is rebound to the thunk in place, so what
owns its captures (rc's retain and release, own's box) is unchanged.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 301dbe9 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the closure-fewer-params branch October 29, 2026 14:33
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