Skip to content

Interfaces - #1

Merged
vito merged 15 commits into
mainfrom
interfaces
Nov 1, 2025
Merged

vito merged 15 commits into
mainfrom
interfaces

Conversation

@vito

@vito vito commented Nov 1, 2025

Copy link
Copy Markdown
Owner

Submitting a PR for this as it's pretty large and I'd like to review it myself.

Most of the complexity arises from the fact that supporting interfaces requires proper subtyping: Cat is an Animal, but Animal is not a Cat. We had to face this previously with null handling - Cat! satisfies Cat but not vice versa - but to do that I just special-cased NonNullType in hm.Unify. For interfaces I'd like to take a more general approach.

The general idea is that any type can return a list of supertypes, and during typechecking we'll check against supertypes as well. So a! has a as its supertype, and Cat has Animal (among whatever other interfaces it implements).

Part of this meant refactoring hm.Unify into hm.Assignable (pending rename?) to make it clearly asymmetrical, so that's a big chunk of the changes here.

vito added 9 commits October 31, 2025 01:10
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
* wire up interface <-> type relationships in Hoist phase so that
  constructors can take interface args with concrete defaults.
  implementation validity is enforced in Infer phase.
* some unrelated test fixes (de-slopping, avoid User conflict w/ test
  GraphQL server)

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
@vito
vito requested a review from Copilot November 1, 2025 15:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive interface type support for the Dang language, enabling structural subtyping where types can implement interfaces and be used polymorphically. The implementation includes:

  • Grammar additions for interface declarations and implements clauses
  • Subtyping system integrated into Hindley-Milner type inference via Supertypes() method
  • Interface validation with GraphQL-spec-compliant covariance/contravariance rules
  • Support for both Dang-defined and GraphQL schema interfaces
  • Enhanced error messages for type mismatches

Reviewed Changes

Copilot reviewed 67 out of 69 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pkg/hm/unify.go Refactored unification to support subtyping via Assignable() and isSubtype() functions
pkg/hm/types.go Added Supertypes() method to all types, implementing subtype hierarchy
pkg/dang/env.go Extended Module with interface tracking, validation, and common supertype finding
pkg/dang/slots.go Implemented InterfaceDecl and ClassDecl with interface linking and validation
pkg/dang/dang.peg Added grammar for interface declarations and implements clauses
pkg/dang/infer.go Updated type inference to use Assignable() instead of Unify()
tests/* Comprehensive test coverage for interface functionality and error cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/hm/types.go Resolved
Comment thread pkg/dang/env.go Resolved
Comment thread pkg/dang/ast_expressions.go Resolved
Comment thread pkg/dang/slots.go Resolved
Comment thread pkg/dang/infer.go Resolved
Comment thread cmd/dang/main.go Resolved
vito added 3 commits November 1, 2025 11:47
...which was the whole point, but I think the AI shoved a stick into its
spokes when it made NonNullType.Types return nil

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
vito added 3 commits November 1, 2025 16:27
mostly removing no-op/dead code from the initial conversion from
chewxy/hm

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito marked this pull request as ready for review November 1, 2025 21:01
@vito
vito merged commit 9350de2 into main Nov 1, 2025
@vito
vito deleted the interfaces branch May 25, 2026 16:33
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.

2 participants