Interfaces & Closures

Interfaces & Closures

Part of the Sun ABI draft. These are proposed requirements, not guarantees of current releases. Open decisions must be resolved before stabilization.

Interfaces and closures

Suppose an interface call selects a method by table position:

old table: slot 0 = draw,  slot 1 = close
new table: slot 0 = close, slot 1 = draw
old caller invokes slot 0 → calls close instead of draw

This is why a method's name alone cannot establish dispatch compatibility.

A closure has a similar hidden agreement. If it captures count, its callback must receive the pointer it expects in order to find count. Passing a pointer to the outer closure record instead of its captured data can read the wrong bytes.

Adding or reordering interface methods can break compatibility. A fixed method table cannot contain every future specialization of a generic method.

Minimal exampleLevelRequirement
Slot 0 means draw to the caller but close to the callee.MUSTDefine method-slot identity, order, signatures, receiver adjustment, and metadata or destruction entries.
An interface method is first specialized for a new type in the consumer.MUSTDefine how interface methods are specialized.
A callback expects captured data, but receives the outer closure record.MUSTDefine whether the hidden argument points to the environment or to a closure record.
Only the library callback and destructor inspect captured fields.MUSTKeep an environment layout private only when consumers never inspect it and invocation and cleanup use compatible interfaces.
A returned callback refers to a local value that has already been destroyed.MUST NOTLet a capture outlive the value it borrows.

Open decisions

Specify method-table generation, interface reference passing and returning, and closure capture destruction. Interface borrow parameters and return values currently use different lowering rules; describing both as fat pointers is insufficient.