Swift Conforming Multiple Protocols Inherits from Same Protocol with Associated Type

returning swift protocol associated type in multiple methods

The solution you came up with by playing around is exactly what you need

As mentioned elsewhere, the main issue with your first protocol is that you're enforcing createSomeView() createAnotherView() both return the same type. While ViewA() and ViewB() are both candidates for V, since they conform to View they are still different types, and therefore cannot BOTH be V in a given object.

By defining both V1 and V2, you allow for each function to return a different type, or the same type, it's all acceptable. By making both V1 and V2 require View conformance, you allow for the some View syntax

protocol with same associated type name

Another workaround is to create a third, combined protocol:

protocol ReadWrite {
associatedtype R
associatedtype W
func read() -> R
func write(a: W)
}

It's not pretty, since it forces you to redeclare the protocol members, but it does keep it generic (you're not limited to String and Int).

Protocol conforming to type with associated value

Because this is not a current feature of Swift. Once there is an associated type, there is always an associated type. It doesn't go away just because you constrain it. And once it has an associated type, it is not concrete.

There is no way to "inherit" protocols this way. What you mean is:

protocol MyProtocol {
var id: UUID { get }
}

And then you can attach Identifiable to structs that require it:

struct X: MyProtocol, Identifiable {
var id: UUID
}

(note that no where clause is required.)

There is no Swift feature today that allows you to say "types that conform to X implicitly conform to Y." There is also no Swift feature today that allows for an Array of "things that conform to Identifiable with ID==UUID." (That's called a generalized existential, and it's not currently available.)

Most likely you should go back to your calling code and explore why you require this. If you post the code that iterates over test and specifically requires the Identifiable conformance, then we may be able to help you find a design that doesn't require that.

Distinguishing between inherited `associatedtype`s in Swift protocols

This has been discussed in Multiple protocols associatedtype name collision in the Swift forum. Xiaodi Wu writes:

It is very much possible, but the identically named associated types must be, in the conforming type, fulfilled by the same type.

In the future, there may be syntax added to allow types to conform to two protocols with such clashing requirements, but it adds great complexity in terms of implementation and is not without its own pitfalls for users (for example, it can get very confusing for end users of your type).

So a type can conform to both A and B with identical associated type T, e.g.

struct Foo: A, B {
typealias T = String
}

and a protocol can inherit from both A and B and restrict T to the identical type:

protocol C: A, B where T == String {

}

Conforming to both protocols with distinct associated types is currently not supported.

How to use multiple protocols in Swift with same protocol variables?

The simple answer is that you can't.

Maybe one protocol depends on another, in which case you would use the dependent protocol for the type of your delegate.



Related Topics



Leave a reply



Submit