hylo icon indicating copy to clipboard operation
hylo copied to clipboard

Almost

Open dabrahams opened this issue 8 months ago • 1 comments

With the requirement machine branch, this fails

conformance Buffer: Collection {

  public typealias Position = Int

  public fun start_position() -> Int { 0 }

  public fun end_position() -> Int { count() }

  /// Returns the position immediately after `i`.
  ///
  /// - Requires: `i != end_position()`.
  /// - Complexity: O(1).
  public fun position(after i: Int) -> Int { i + 1 }

}

extension Collection {

  public fun count() -> Int { // should be a trait requirement
    var i = self.start_position()
    var r = 0
    while i != self.end_position() {
      i = position(after: i)
      r += 1
    }
    return r
  }

}

extension Array where Element: Copyable {

  public init<C: Collection where C.Element == Element>(_ source: C) {
    self = Array<Element>()
    &self.reserve_capacity(source.count())
    var i = source.start_position()
    while i != source.end_position() {
      var e = source[i].copy()
      &self.append(e)
      i = source.position(after: i)
    }
  }

}

let x = Array<Int>([1, 2, 3])

public fun main() {
  print(x[0])
  print(x[1])
  print(x[2])
}

Errors:

/Users/dave/Documents/WIP/x.hylo:1.13-19: error: undefined name 'Buffer' in this scope
conformance Buffer: Collection {
            ~~~~~~
/Users/dave/Documents/WIP/x.hylo:46.9-14: error: type 'Int[3]' does not conform to trait 'Collection'
let x = Array<Int>([1, 2, 3])
        ~~~~~
/Users/dave/Documents/WIP/x.hylo:46.9-14: error: incompatible types 'Int[3].Element' and 'Int'
let x = Array<Int>([1, 2, 3])
        ~~~~~

dabrahams avatar Jun 20 '24 14:06 dabrahams