# error: value of type 'Self.SubSequence' has no member 'Distance'

**URL:** <https://forums.swift.org/t/error-value-of-type-self-subsequence-has-no-member-distance/765>\
**Category:** Using Swift\
**Created:** [December 23, 2015, 11:24pm UTC](https://forums.swift.org/t/error-value-of-type-self-subsequence-has-no-member-distance/765 "2015-12-23T23:24:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Eggert](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Daniel\_Eggert](https://forums.swift.org/u/Daniel_Eggert)\
**Post date:** [December 23, 2015, 11:24pm UTC](https://forums.swift.org/t/error-value-of-type-self-subsequence-has-no-member-distance/765/1 "2015-12-23T23:24:33Z")

</div>

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'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return tail.scopedReduceAndApply(f, g: g, list: ll)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ~~~~~~~~~~~~~~~~~~~~~~~

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

/Daniel

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

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

---

<div class="post-metadata">

**Author:** ![gribozavr](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@gribozavr](https://forums.swift.org/u/gribozavr)\
**Post date:** [December 24, 2015, 2:11am UTC](https://forums.swift.org/t/error-value-of-type-self-subsequence-has-no-member-distance/765/2 "2015-12-24T02:11:06Z")

</div>

> 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'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return tail.scopedReduceAndApply(f, g: g, list: ll)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ~~~~~~~~~~~~~~~~~~~~~~~
> 
> What is ‘Distance’ and why doesn’t SubSequence of CollectionType have it?

That's just a bad error message.

> extension CollectionType where SubSequence : CollectionType {

I think you need this constraint to be propagated all the way down,  
for recursive calls, so you need:

extension CollectionType where SubSequence == Self

Or, if you want to allow the toplevel type to differ:

extension CollectionType  
&nbsp;&nbsp;where  
&nbsp;&nbsp;SubSequence : CollectionType,  
&nbsp;&nbsp;SubSequence.SubSequence == SubSequence

Dmitri

> **···**
>
> On Wed, Dec 23, 2015 at 3:24 PM, Daniel Eggert via swift-users \<swift-users@swift.org\> wrote:
> 
> --  
> main(i,j){for(i=2;;i++){for(j=2;j\<i;j++){if(!(i%j)){j=0;break;}}if  
> (j){printf("%d\n",i);}}} /\*Dmitri Gribenko \<gribozavr@gmail.com\>\*/

---

<div class="post-metadata">

**Author:** ![Daniel\_Eggert](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@Daniel\_Eggert](https://forums.swift.org/u/Daniel_Eggert)\
**Post date:** [December 26, 2015, 6:31pm UTC](https://forums.swift.org/t/error-value-of-type-self-subsequence-has-no-member-distance/765/3 "2015-12-26T18:31:43Z")

</div>

Thanks. That did the trick.

/Daniel

> **···**
>
> > On 24 Dec 2015, at 03:11, Dmitri Gribenko \<gribozavr@gmail.com\> wrote:
> > 
> > On Wed, Dec 23, 2015 at 3:24 PM, Daniel Eggert via swift-users \> \<swift-users@swift.org\> wrote:
> > 
> > > 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'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return tail.scopedReduceAndApply(f, g: g, list: ll)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^ ~~~~~~~~~~~~~~~~~~~~~~~
> > > 
> > > What is ‘Distance’ and why doesn’t SubSequence of CollectionType have it?
> > 
> > That's just a bad error message.
> > 
> > > extension CollectionType where SubSequence : CollectionType {
> > 
> > I think you need this constraint to be propagated all the way down,  
> > for recursive calls, so you need:
> > 
> > extension CollectionType where SubSequence == Self
> > 
> > Or, if you want to allow the toplevel type to differ:
> > 
> > extension CollectionType  
> > where  
> > SubSequence : CollectionType,  
> > SubSequence.SubSequence == SubSequence
