Etan Etan - 9 months ago 32
Swift Question

Passing Data into function that accepts generic collection

I have a function that accepts generic collections.

func foo<T: BidirectionalCollection>(_ bar: T)
where T.Iterator.Element == UInt8,
T.SubSequence: BidirectionalCollection & RangeReplaceableCollection,
T.SubSequence.Iterator.Element == UInt8,
T.SubSequence.Index == T.Index,
T.SubSequence.SubSequence == T.SubSequence

I want to call this function with
as T:


However, this gives the error:

error: generic parameter 'T' could not be inferred

What's going on here? How to properly call the function with a


I think the problem is that Data.SubSequence does not conform to RangeReplaceableCollection protocol, while your generic function has this requirement:

T.SubSequence: BidirectionalCollection & RangeReplaceableCollection