Skip to content

Structural interfaces across schemas; raise any Error-shaped value - #306

Merged
vito merged 4 commits into
mainfrom
structural-interfaces
Nov 12, 2026
Merged

vito merged 4 commits into
mainfrom
structural-interfaces

Conversation

@vito

@vito vito commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Interfaces declared in Dang are now structural. A type that has an interface's fields is a value of it, with or without implements, and whichever schema it comes from. This makes Dang's interfaces the way to write one interface over types from different schemas. Interfaces a GraphQL schema declares stay nominal.

The prelude's Error is one of these interfaces, so any type with a public message: String! can be raised, including a schema's own error type. In a Dagger module's entrypoint, raise can now take a Dagger.Error directly. The engine gets back the very object that was raised, with all its values. Before, an entrypoint could raise only a message.

Structural conformance

A type has an interface's shape when every interface field is a public field of the type, with a type that fits by the rules implements already checks:

  • covariant returns
  • contravariant arguments
  • extra arguments must be optional
  • blocks
  • generic methods at least as general as the interface's

A union has the shape when each of its members does. A schema's union doesn't qualify, because a handle on it has no fields until the server says what it holds.

interface Named { name: String! }
type Person { name: String! }
greet(n: Named!): String! { `hello, ${n.name}` }
greet(Person("Ada"))

How it works:

  • Type checker. hm.assignable asks a want that implements hm.StructuralType, after checking supertypes.
    • The pairs a check assumes are threaded on its own stack. Recursive shapes are judged coinductively, and checks running at the same time share nothing. That matters for the engine, which runs many evaluations in one process.
    • The check is a question, never a constraint: the interface's type variables are skolemized, and an answer that binds anything but the field's own freshened variables is refused.
  • case/rescue type patterns ask the same question, both statically (moduleFits, resolveInterfaceTypePattern) and at runtime (matchesType).
  • Merging is unchanged. Branches of two types that only have the shape still widen to a union, and a list of them needs one cast: [(a :: Named!), b].
  • Error messages name the missing field, the private (let) field, or the field whose type doesn't fit.

There is no version gate, since the interpreter never branches on version. The v2.5 release notes list the two ways a case or rescue arm on an interface can change: an arm placed after it for a type of its shape is now unreachable, and in an operand that also holds a declared implementation, a value of the shape takes the interface's arm.

Raising

  • A raised GraphQL object's message is read when it is raised, because Error() can't make a query. The same goes for an object whose message is a method, and for each link of an explicit cause chain, which the uncaught report now shows.
  • A message that fails to read is reported as such, and doesn't replace the error, so a rescue still matches it by type.
  • A rescued error raised again unchanged keeps its message, its causes, and the Go error it was classified from, exposed as RaisedError.Origin().
    • After e: GraphQLError! => raise e, a host can still report the GraphQL error with its extensions.
    • It is deliberately not Unwrap, which would let errors.As find the original failure's source location instead of the raise site.

Docs

  • A new section on structural interfaces across schemas, on the interfaces page.
  • The errors page.
  • v2.5 release notes.
  • The dang-language and dang-internals skills.

Testing

  • tests/test_interface_structural.dang covers plain types, methods, generics, unions, recursive shapes, interface-to-interface conformance, casts, type patterns, and raising.
  • Error goldens cover a missing field, a private field, a less general method, and raising a type without a message.
  • pkg/dang/structural_test.go, against a fake schema, covers:
    • raising and rescuing a schema Error
    • re-raising one
    • schema unions and objects without a message
    • schema interfaces staying nominal
    • Origin
    • explicit-cause chains, including nested ones and re-raises
    • computed and failing messages
    • a required id
  • pkg/dang, pkg/hm, tests, docs and fmt/stale pass.

vito added 3 commits November 12, 2026 18:54
An interface declared in Dang is now structural: a value of any object or
interface type with its fields, public and of fitting types, is a value of
the interface, whether or not its type says `implements`, and whichever
schema it comes from. A union of such types is one too, unless a schema
declares it: a handle on a schema's union has no fields to read. That
makes Dang's interfaces the way to write one interface over types from
different schemas, none of which can name it. Interfaces a GraphQL schema
declares stay nominal: the server decides which types implement them.

hm asks a structural want (hm.StructuralType) after the supertypes. The
pairs a check assumes are threaded on its own stack, so recursive shapes
are judged coinductively and concurrent checks share nothing. The check
pins no type: the interface's type variables are skolemized, and an answer
binding anything but the field's own freshened variables is refused. Type
patterns go through the same question, statically and at runtime. Merging
is unchanged: branches of two types that only have an interface's shape
still widen to a union, and a list of them needs one cast to it.

There is no version gate: the interpreter can't branch on one. Most code
only gains programs, but an interface arm of a case or rescue now takes
values of the shape, so an arm after it for such a type is unreachable,
and in an operand also holding a declared implementation, such a value no
longer falls through to a later arm.

Signed-off-by: Alex Suraci <[email protected]>
With Error structural, any type with a public `message: String!` is an
error, a GraphQL schema's own error type included: Dagger's Error raises
and rescues as one, and the raised value is the very object, for the host
to read back. Error() can neither query nor call, so a message that takes
either, a GraphQL object's or an object's computed one, is read when the
error is raised, and so are those along its explicit `cause` chain, which
the uncaught report shows. A message that fails to read is reported as
such rather than replacing the error, so a rescue still matches it by type.
Raising a type without the shape says what it lacks.

A rescued error raised again as it was is now that error again, with its
message, its causes, and the Go error it was classified from, as
RaisedError.Origin: after `rescue { e: GraphQLError! => raise e }`, the
GraphQL error with its extensions, for a host to report as if nothing had
rescued it. It is not Unwrap, which would let errors.As find the original
failure's source location in place of the raise's.

Signed-off-by: Alex Suraci <[email protected]>
The interfaces page frames Dang's interfaces as the way to write one
across schemas, with what `implements` still adds, and the errors page says
any value of Error's shape raises, a schema's own error type included. The
2.5 release notes list structural interfaces and the case arms they can
change, and the skills follow.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: e1a4ea7
Status: ✅  Deploy successful!
Preview URL: https://8f711528.dang-3kk.pages.dev
Branch Preview URL: https://structural-interfaces.dang-3kk.pages.dev

View logs

dang fmt puts a blank line after the pick function.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 496a3b6 into main Nov 12, 2026
9 checks passed
vito added a commit that referenced this pull request Nov 12, 2026
#306 added its sections to the 2.5 notes. The lede, checklist, Go
embedders section and sources now cover it too: structural interfaces in
the lede, a checklist step for case arms an interface arm now shadows,
RaisedError.Origin for hosts, and #306 in the sources, verified at 496a3b6.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 13, 2026
#306 added its sections to the 2.5 notes. The lede, checklist, Go
embedders section and sources now cover it too: structural interfaces in
the lede, a checklist step for case arms an interface arm now shadows,
RaisedError.Origin for hosts, and #306 in the sources, verified at 496a3b6.

Signed-off-by: Alex Suraci <[email protected]>
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