Etan Etan - 1 month ago 7
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
Data
as T:

foo(Data())


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
Data
instance?

Answer

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