Protocol associated type constraints in classes and structs ?


(Trevör Anne Denise) #1

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


(Matthew Johnson) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Trevör Anne Denise) #3

Hi Matthew,

Thanks for this quick reply, happy to see that this is something the community is working on, this will make protocols even more useful!

Since this isn't implemented yet, I am wondering what would be the best approach to my problem with the current Swift version...
Currently since there is no valid default implementation for my methods I use closures (at init time, that then get called by the methods) to let the user of my classes choose his implementation, this is the safest approach I could think of but its isn't pretty and I'm not sure that this really is the best approach...

Trevör

···

Le 10 févr. 2017 à 18:04, Trevör Anne Denise <trevor.annedenise@icloud.com> a écrit :