Fast data types in protocol and general types

I have a protocol that has typealias:

protocol Fooable {
    typealias T: Equatable
    func makeFoo() -> T
}

I expect all types matching it to return equal values ​​from makeFoo.

Now, I would like to create an array extension in which the values ​​are stored Fooable:

extension Array where Element: Fooable {

    func arrayFoo<F: Foobable, S>(array: Array<F>, transform: (Element, [F]) -> S) -> [S] {

I expect that given array A containing elements Fooableand array B which contains elements Fooable, I can do:

a.arrayFoo(b, {...})

I have a function part arrayFoo:

var leftGenerator = self.generate()
var rightGenerator = array.generate()

if let leftValue = leftGenerator.next(), rightValue = rightGenerator.next() {
    let leftFoo = leftValue.makeFoo()
    let rightFoo = rightValue.makeFoo()

    if leftFoo == rightFoo {

I expect that leftFoothey rightFoowill be Equableable, because they are created with the help makeFoo()that Equatables should return.

But Swift complains: Binary operator == cannot be applied to operands of type Element.T and F.T

Any ideas or workarounds?

+4
source share
2

arrayFoo() array self Fooable, T, .. Element.T F.T .

where F.T == Element.T :

func arrayFoo<F: Fooable, S where F.T == Element.T >(array: Array<F>, transform: (Element, [F]) -> S) -> [S] {
    // ...
}
+1

, Equatable, :

@warn_unused_result
public func ==(lhs: Self, rhs: Self) -> Bool

func == , Swift , . , , Int String,

let t = 0
let a = "string"
if t == a { // error
}
0

Source: https://habr.com/ru/post/1618079/


All Articles