[swift-users] error: value of type 'Self.SubSequence' has no member 'Distance'

Daniel Eggert danieleggert at me.com
Wed Dec 23 17:24:33 CST 2015

I have an array of String and need to call withCString on each one and then pass the resulting array of UnsafePointer<Int8> to a C api. The unsafe pointer is only valid within the scope of withCString.

So I built the following, but the compiler doesn’t like it:

% swift scopedReduceAndApply.swift 
scopedReduceAndApply.swift:18:16: error: value of type 'Self.SubSequence' has no member 'Distance'
        return tail.scopedReduceAndApply(f, g: g, list: ll)
               ^~~~ ~~~~~~~~~~~~~~~~~~~~

What is ‘Distance’ and why doesn’t SubSequence of CollectionType have it?


extension CollectionType where SubSequence : CollectionType {
  /// Calls `f` for each Element to (scoped) generate a value of type `A`
  /// and pass it to the given function (3rd argument to `f`).
  /// The array of all generated `A` values will then be passed to the function
  /// `g`. Its return value will be returned by `scopedReduce`.
  /// This is similar to `reduce` but the function `g` is called while all calls
  /// to `f` are on the stack, i.e. are in scope. This allows `f` to call a
  /// function such as `String.withCString` and ensure that we're in its scope.
  private func scopedApply<A,B>(f: (Generator.Element, (A) -> B) -> B, g: [A] -> B) -> B {
    return scopedReduceAndApply(f, g: g, list: [])
  private func scopedReduceAndApply<A,B>(f: (Generator.Element, (A) -> B) -> B, g: [A] -> B, list: [A]) -> B {
    if let (head, tail) = decompose {
      return f(head) { (a: A) -> B in
        var ll = list
        return tail.scopedReduceAndApply(f, g: g, list: ll)
    } else {
      return g(list)

extension CollectionType {
  var decompose: (head: Generator.Element, tail: SubSequence)? {
    if self.isEmpty {
      return nil
    } else {
      return (self[startIndex], self[startIndex.successor()..<endIndex])

More information about the swift-users mailing list