Skip to content

Compile time check of trait implementations #21

Description

@tonysparks

Problem

Ensuring a type fully implements a trait at compile time. We want compile time errors if a implementation drifts from a trait implementation.

A brief example -- say we have the following trait:

trait Writer {
  writeByte: func(b: u8) : i32;
  writeBytes: func(bytes: *u8, len: i32) : i32;
}

Then an implementation:

public struct StringBuffer {
  ...
}


public func (this: *StringBuffer) writeByte(b: u8) : i32 {
 ...
}

// Writer expects writeBytes instead of writeByteArray
public func (this: *StringBuffer) writeByteArray(bytes: *u8, len: i32) : i32 {
 ...
}

We won't know that StringBuffer doesn't fully implement the Writer trait until we try to assign it. Moreover, if the Writer trait evolves, we don't know StringBuffer no longer fully implements Writer trait.

Rough implementation idea

Create a new implements_traits note that defines a set of traits to be check if the implementation fully defines.

public @note implements_traits {
  traits: []typeid
}

Then use it as so:

// would emit out a compile error if Writer or Reader traits
// were not FULLY implemented within this scope.
@implements_traits(.traits = []typeid {
  typeof(:Writer),
  typeof(:Reader),
})
public struct StringBuffer {
  ..
}

Outstanding questions:

  1. How would primitives use implements_traits?
  2. Given methods can be implemented in any module, the implements_traits check would only be valid for the scope of the module it's used in. This seems like a pretty heavy limitation

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions