Inferring constraints for type parameter via associated type

protocol O { }
protocol P {
    associatedtype A: O
    func foo(_: A)

struct S<A> : P
    where A: O // Could this line be made redundant by improved inference?
    func foo(_: A) { }

This is only my guess, but I'd say "no" because it kind of creates an extra case where it's inferred while if you compare with retroactive conformances you still need to write that constraint on the type by yourselves.

struct S<A> where A : O {
    func foo(_: A) { }
extension S : P {}

It's also similar to the inheritance constraints for generic classes, where you're required to write the same constraints over and over again for each subclass if it has a generic type parameter that is passed down to the superclass.

class Test<T> where T : NSObject {}

class Something<T> : Test<T> // error: 'Test' requires that 'T' inherit from 'NSObject'
1 Like
Terms of Service

Privacy Policy

Cookie Policy