Using associatedtype in a delegate protocol for a generic type

kelin

I have a Game class. I made it generic because I was need to support different types of boards. Now I just want to add a classical iOS-style delegate with a method which will take a game and a new points value as parameters. How to achieve this in the Swift associatedtype way? I really confused that I can't impelemnt such simple logic.

protocol GamePointsDelegate {
    associatedtype B: Board
    func game(_ game: Game<B>, didSetPoints points: Int)
}

class Game<B: Board> {
    let board: Board

    var points = 0 {
        // Compiler Error
        // Member 'game' cannot be used on value of protocol type 'GamePointsDelegate'; use a generic constraint instead
        didSet { pointsDelegate?.game(self, didSetPoints: points) }
    }

    // Compiler Error
    // Protocol 'GamePointsDelegate' can only be used as a generic constraint because it has Self or associated type requirements
    var pointsDelegate: GamePointsDelegate?
}
Qbyte

You could remove the associated type requirement from your protocol and use a generic function game instead:

protocol GamePointsDelegate {
    func game<B>(_ game: Game<B>, didSetPoints points: Int)
}

So you can use the code of your Game class as it is but the downside is that the class which conforms to the protocol has to handle all Boards.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to implement to a generic protocol, which has a function using the Type of associatedtype?

Infer generic from any using protocol with associatedtype

How to check the concrete type of associatedtype of a protocol inside generic class?

Use Protocol with associatedtype as a Type

How to write generic function in Swift using a protocol associatedtype

Swift protocol with associatedtype as a parameter type

Implement delegate using generic protocol in Swift

Swift protocol with associatedtype (ambiguous for type lookup)

Protocol associatedType and <>

Swift Generic struct conform to protocol with associatedType got "Cannot convert return expression of type 'String' to return type 'Key'" Error

How to make extension to protocol using associatedtype and array

Is there information somewhere on Swift protocol associatedtype using `=` versus `:`?

Using a Generic Type with a Protocol and associated type?

Cannot assign Generic value to protocol associatedType variable in an extension

How to use a generic protocol as a delegate?

Generic type as a delegate parameter

how to use Void type in a closure of protocol with associatedtype in Swift

How to require a generic type implement a generic protocol using a specific type in the protocol

AssociatedType in a protocol with typealias

associatedtype protocol conformance issues

Swift protocol with associatedtype error

Attempting to use Protocol with associatedType

How can assing generic (associated type require) protocol as delegate to any controller?

Get generic delegate return type

Protocol can only be used as a generic constraint because it has Self or associatedType requirements

Using protocol with generic data type to pass data between screens

How to initialize generic enum using a protocol with associated type?

Delegate for generic class and method with generic return type

Using delegates on generic protocol