Swift type erasure protocol with required init method

Vladislav

I have the protocol that conform Equatable

protocol TestProtocol: Equatable {
    var id: Int {get}
}

func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
    return lhs.id == rhs.id
}

To have opportunities to store TestProtocol value we should use type erasure.

class AnyTestProtocol<T: TestProtocol>: TestProtocol {
    var id: Int {
        return item.id
    }

    private let item: T

    init(_ testProtocol: T) {
        self.item = testProtocol
    }
}

And, after all, we can use it, like this struct TestStruct: TestProtocol { let id: Int }

let a = TestStruct(id: 1)
let b = TestStruct(id: 1)

a == b /// true

// let a = TestStruct(id: 1)
// let b = TestStruct(id: 0)
// a == b /// false

And all ok. But I want to use TestProtocol with required init method, such as init(id: Int). How can I implement AnyTestProtocol If TestProtocol contains required init method?

protocol TestProtocol: Equatable {
    var id: Int {get}

    /// Required init method for every protocol implementation
    init(id: Int)
}

func ==<T: TestProtocol>(lhs: T, rhs: T) -> Bool {
    return lhs.id == rhs.id
}

class AnyTestProtocol<T: TestProtocol>: TestProtocol {
    var id: Int {
        return item.id
    }

    private let item: T

    required init(id: Int) {
        ????????
    }

    init(_ testProtocol: T) {
        self.item = testProtocol
    }
}
Martin R

Just forward the required init(id:) call to T:

class AnyTestProtocol<T: TestProtocol>: TestProtocol {

    // ...

    required init(id: Int) {
        self.item = T(id: id)
    }

    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift Type Erasure with Generic Enum and Generic Protocol

Swift - Inherited Protocol Associated Type Erasure

Method overloading with type erasure

Swift: Nested type erasure

Swift type erasure - for this case?

Type erasure on protocol with Self type requirement

Erasure type and Bridging Method clarification

Overriding a method using type erasure

Swift protocol used as type in method not accepting instances of same protocol

Method has the same erasure as another method in type

Erasure of method is the same as another method in type

How can I use Type Erasure with a protocol using associated type

How to resolve Return type error in Swift for a protocol method with associated type?

"Type of a class which conforms to a protocol" as parameter in a method swift

difference between override init and required init? - swift

Overriding method with selector init has incompatible type in Swift

Swift Protocol as a Type

Array of type protocol in swift

swift protocol with associated type

Swift Type Erasure attempt: "Reference to invalid associated type"

Not able to pass self (which implements a protocol) to init method of a class instantiation.(Swift)

Swift init method from JSONJoy protocol not available as Objective-C selector

Swift question: how can we init an non-optional variable in a if else block if it's of protocol type?

Swift - Why it's possible to call the empty init of protocol-type OptionSet?

Init method in swift 3

Parameter 0 of method init in 'Application' required a bean of type 'package' that could not be found

Generic FunctionalInterface and Method Reference messed up by Type Erasure

How to do Type erasure in Swift using if-let?

Swift Protocol inheriting Protocol method with same name