Swift Generic Protocol

pseudonym117

Is it possible to have a generic protocol in swift? I tried protocol foo<T>{} and that is not legal. I'm looking for something that can be used similarly to Java's List<T> interface.

fabrice truillot de chambrier

There is no such thing as generics for protocols. But there is something else, which ressembles a lot to the generics when you look at it.

Here is an example taken from the Swift standard library:

protocol Generator {
    typealias Element
    func next() -> Element?
}

The Swift book scratches the surface in the Generics chapter, Associated Types.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift using generic in protocol

Swift generic Serializable protocol

Using Generic swift enum in a protocol

Swift Generic Protocol Function Parameters

Swift Protocol Inheritance & Generic functions

Generic constraint for any protocol in Swift

Swift Protocol as Generic Parameter to Class

Swift typealias in protocol and generic types

Swift: Generic Type conform Protocol

Generic Decoder for Swift using a protocol

Swift Type Erasure with Generic Enum and Generic Protocol

Conforming a generic type to a protocol in a Swift extension

Swift protocol with generic method: invalid redeclaration of implementation

How to construct generic without protocol in Swift?

Further constraining a generic function from a Swift Protocol

Swift: conformance to the Protocol with Generic method with "where" clause

Swift Generic Protocol Class Type in Array

Is it possible to have a property containing generic protocol in Swift?

Swift - Typealias dictionary with value that implements a generic protocol

How create dependent generic protocol in swift

Swift Generic UIView subclass with protocol issue

Generic protocol for observing changes in Swift 5

How to conform to a protocol with a generic superscript affordance in Swift?

Cast a Swift generic class to a protocol with a typealias

Swift: Generic's type protocol not being recognized

Swift protocol conformance when returning a generic

Implement delegate using generic protocol in Swift

Swift: How to pass the Generic protocol as a parameter for another protocol

Swift generic type that conform to protocol cannot be used to refer protocol?