Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift泛型集合的紧化_Swift_Generics_Collections - Fatal编程技术网

Swift泛型集合的紧化

Swift泛型集合的紧化,swift,generics,collections,Swift,Generics,Collections,我有一个操作通用集合的函数: func foo<T: RangeReplaceableCollection>(_ bar: T) where T.Iterator.Element == UInt8 { // } func-foo(bar:T) 其中T.Iterator.Element==UInt8 { // } 然后,该函数访问该集合的子范围,因此需要附加约束: func foo<T: RangeReplaceableCollection>(_ b

我有一个操作通用集合的函数:

func foo<T: RangeReplaceableCollection>(_ bar: T) 
     where T.Iterator.Element == UInt8
{
    //
}
func-foo(bar:T)
其中T.Iterator.Element==UInt8
{
//
}
然后,该函数访问该集合的子范围,因此需要附加约束:

func foo<T: RangeReplaceableCollection>(_ bar: T) 
     where T.Iterator.Element == UInt8,
           T.SubSequence: RangeReplaceableCollection,
           T.SubSequence.Iterator.Element == T.Iterator.Element
{
    //
}
func foo<T: RangeReplaceableCollection>(_ bar: T) 
     where T.Iterator.Element == UInt8,
           T.SubSequence: RangeReplaceableCollection,
           T.SubSequence.Iterator.Element == T.Iterator.Element,
           T.SubSequence.SubSequence: RangeReplaceableCollection,
           T.SubSequence.SubSequence.Iterator.Element == 
               T.SubSequence.Iterator.Element
{
    //
}
func-foo(bar:T)
其中T.Iterator.Element==UInt8,
T.子序列:RangeReplacableCollection,
T.SubSequence.Iterator.Element==T.Iterator.Element
{
//
}
它还调用本身对集合的子范围的子范围进行操作的函数,所以它需要更多的约束:

func foo<T: RangeReplaceableCollection>(_ bar: T) 
     where T.Iterator.Element == UInt8,
           T.SubSequence: RangeReplaceableCollection,
           T.SubSequence.Iterator.Element == T.Iterator.Element
{
    //
}
func foo<T: RangeReplaceableCollection>(_ bar: T) 
     where T.Iterator.Element == UInt8,
           T.SubSequence: RangeReplaceableCollection,
           T.SubSequence.Iterator.Element == T.Iterator.Element,
           T.SubSequence.SubSequence: RangeReplaceableCollection,
           T.SubSequence.SubSequence.Iterator.Element == 
               T.SubSequence.Iterator.Element
{
    //
}
func-foo(bar:T)
其中T.Iterator.Element==UInt8,
T.子序列:RangeReplacableCollection,
T.SubSequence.Iterator.Element==T.Iterator.Element,
T.SubSequence.SubSequence:RangeReplaceableCollection,
T.SubSequence.SubSequence.Iterator.Element==
T.SubSequence.Iterator.Element
{
//
}
  • 有什么办法可以清理一下吗

  • 是否至少有一种方法可以隐藏
    typealias
    后面的所有where子句

  • 如果没有,是否有解决这一问题的建议


集合子序列不必具有与集合相同的类型 但就我所知,子序列的子序列 与定义的所有集合的子序列本身的类型相同 在标准库中。因此,还有一个限制

    T.SubSequence.SubSequence == T.SubSequence
应解决任意嵌套子序列的问题:

func foo<T: RangeReplaceableCollection>(_ bar: T)
    where T.Iterator.Element == UInt8,
    T.SubSequence: RangeReplaceableCollection,
    T.SubSequence.Iterator.Element == T.Iterator.Element,
    T.SubSequence.SubSequence == T.SubSequence
{
   // ...
}
func-foo(bar:T)
其中T.Iterator.Element==UInt8,
T.子序列:RangeReplacableCollection,
T.SubSequence.Iterator.Element==T.Iterator.Element,
T.SubSequence.SubSequence==T.SubSequence
{
// ...
}

为什么会有
AnyRandomAccessCollection
而没有
anyRangeReplacableCollection
?如果我想在多个函数上有相同的约束条件,有没有可能使用typealias?