Files
swift-mirror/test/Interpreter/protocol_extensions.swift
Doug Gregor 1e6b445cae Teach normal name lookup to find members of protocol extensions.
We do a silly little dance here of finding all of the members of
protocols and their extensions, then deleting the protocol members. In
the future, this is the place where we should handle the protocol
requirement -> witness mapping, including handling derived
conformances.

Basic protocol extensions seem to be working now:

  extension SequenceType {
    var myCount: Int {
      var result = 0
      for x in self {
        ++result
      }
      return result
    }
  }

  println(["a", "b", "c", "d"].myCount) // 4, duh

Swift SVN r26617
2015-03-27 00:10:21 +00:00

36 lines
628 B
Swift

// RUN: %target-run-simple-swift | FileCheck %s
extension SequenceType {
var myCount: Int {
var result = 0
for x in self {
++result
}
return result
}
}
// CHECK: 4
println(["a", "b", "c", "d"].myCount)
extension CollectionType {
var myIndices: Range<Index> {
return Range(start: startIndex, end: endIndex)
}
}
extension CollectionType {
func indexMatching(fn: Generator.Element -> Bool) -> Index? {
for i in myIndices {
if fn(self[i]) { return i }
}
return nil
}
}
// CHECK: 2
println(["a", "b", "c", "d"].indexMatching({$0 == "c"})!)
// CHECK: DONE
println("DONE")