Swift - Syntax to create a type that implements a protocol using a certain associated type
| Originator: | jan.sabbe | ||
| Number: | rdar://17412293 | Date Originated: | 2014/06/22 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Feature (New) | Reproducible: | Always |
On Swift programming language:
My use case: see also https://devforums.apple.com/message/991691
Given a protocol 'Specification'
protocol Specification {
typealias MatchType
func matches(matchObject: MatchType) -> Bool
}
and an implementation
struct AndSpecification<L : Specification, R: Specification where L.MatchType == R.MatchType> : Specification {
var left : L
var right : R
func matches(matchObject: L.MatchType) -> Bool {...}
}
I would like to be able to define a function
@infix func && <L: Specification, R: Specification where L.MatchType == R.MatchType> (left: L, right: R) -> protocol<S: Specification where S.MatchType == L.MatchType> {
return AndSpecification(left: left, right: right)
}
I could specify an AndSpecification as a result type, but I would prefer to hide this 'AndSpecification' type, and only expose a more abstract type to my callers. The Specification protocol is too generic here, caller should know that the MatchType is same as the MatchType of the arguments
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!