[swift-users] Protocol associated type constraints in classes and structs ?

Matthew Johnson matthew at anandabits.com
Fri Feb 10 10:50:57 CST 2017


Hi Trevör,

What you’re asking for has been discussed extensively and a very thorough proposal was drafted by Austin Zheng: https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md

This is not something that is likely to make it into Swift 4 but is a feature with a lot of community demand so it is likely to be added eventually.

Matthew

> On Feb 10, 2017, at 10:46 AM, Trevör ANNE DENISE via swift-users <swift-users at swift.org> wrote:
> 
> Hello everyone !
> 
> I am currently writing a project where I would like to do something similar to this:
> 
> protocol ProtocolA {
>     associatedtype U
>     var someVariable : U { get set }
>     func someFunction(_ x:U) -> U
> }
> 
> class SomeClass<T> {
>     var someProperty : ProtocolA where ProtocolA.U == T
> }
> 
> But this isn't possible so I had to implement it as a generic class:
> class NewClass<U> { ... }
> 
> And now I am using it like this:
> class SomeClass<T> {
> 	var someProperty: NewClass<T>
> }
> 
> The issue here is that protocols aren't used any more with this approach, and in my case there is no "default" implementation of myFunction, so protocols where really useful!!
> 
> Is there a better way to do that in the current Swift version?
> 
> Today, I came across the proposal SE-0142 (Permit where clauses to constrain associated types), but this only seems to apply inside of protocols, what is your opinion about having similar mechanisms in classes and structs ?
> 
> Thank you!
> 
> Trevör
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170210/586dc9fc/attachment.html>


More information about the swift-users mailing list