I encounter a problem between protocol and generic type
I have this Container protocol
protocol Container {
associatedtype State : Any
associatedtype Effect : Any
associatedtype Intent : Any
var store: Store { get }
}
And also this protocol Store
protocol Store {
associatedtype State : Any
associatedtype Effect : Any
var state: AnyPublisher<State, Never> { get }
var effect: AnyPublisher<Effect, Never> { get }
}
I want my store in Container to be type of Store<State, Effect> where State and Effect are the same as specified in the Container protocol. But I don't get it how, compiler ask me to use any Store but it erase typing.
And also, with any, if I try to subscribe on the state publisher, I got an error : Member 'state' cannot be used on value of type 'any Store'; consider using a generic constraint instead
I tried to do this
protocol Container {
associatedtype State : Any
associatedtype Effect : Any
associatedtype Intent : Any
var store: Store<State, Effect> { get }
func post(intent: Intent)
}
But again compiler complains that Protocol 'Store' does not have primary associated types that can be constrained
I really don't understand how achieve what I want.
Sorry if it is a basic question, I'm a originally a Kotlin developer. To be clear, in Kotlin, it would be this if it could help :
interface Container<State : Any, Effect : Any, Intent : Any> {
val store: Store<State, Effect>
fun post(intent: Intent)
}
Hope someone could help me ! Thanks
As one of the error messages say, you can add primary associated types to
Storeto makeStore<State, Effect>possible.Now it is possible to subscribe to
state/effect:You seem to be translating what you would have written in Kotlin, "word for word", into Swift. This is not a good idea, both for programming languages and for natural languages. Kotlin
interfaces and Swiftprotocols have very different semantics and capabilities. Yes, they both declare requirements for implementing types, but that's where the similarity stops. You will likely run into problems later down the line.Unlike in Kotlin, where the
: Anyconstraint forces the type arguments to be non-nullable,: Anyin Swift is redundant.SomeType?(akaOptional<SomeType>) in Swift might "look like" a nullable type in the Kotlin sense, but it has very different semantics. There usually isn't a good reason to constrain a type parameter to be non-Optional.