2

I don't know why this happening, I'd like to return a parameter in accordance to MyProtocol with an array, so I'm trying to achieve that the following way:

import Foundation

protocol Test {

}

protocol ArrayTest {
    associatedtype E : Test
}

extension Array : ArrayTest where Element:Test{
    typealias E = Element
}

class BBB {
 func ggg<T:ArrayTest>( kk: ((_ value:T) -> Void)?){

 }
}

class AAA<T:ArrayTest> {

    func ggg(){
     BBB().ggg { (test) in//Generic parameter 'T' could not be inferred 
 here

     }
 }
 }
BSMP
  • 4,596
  • 8
  • 33
  • 44
Dandelion
  • 136
  • 8

2 Answers2

2

You can't just do BBB().ggg { (test: [Test]) in ... because [Test] does not conform to ArrayTest.

This might come as surprising to you, as you wrote:

extension Array : ArrayTest where Element: Test{
    typealias E = Element
}

Surely the above extension applies to [Test], right? No. For the above extension to apply, Element must conform to Test, but Test does not conform to itself.

I think what you might mean is this:

class AAA<T:ArrayTest> {

    func ggg(){
        BBB().ggg { (test: T) in

        }
    }
}

This uses T from the declaration of AAA.

And as the user of the AAA class, you need to pass an actual type that conforms to ArrayTest, such as [U], where U is a type conforming to Test.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

The compiler can't define a type of generic parameter T. You need to specify the type of T explicitly:

BBB()ggg { (test: [Test]) in

}
Ilya Kharabet
  • 4,203
  • 3
  • 15
  • 29