Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14740] Allow where clauses with non-strict protocol inheritance #57090

Open
swift-ci opened this issue Jun 8, 2021 · 1 comment
Open
Labels
compiler The Swift compiler in itself feature A feature request or implementation generics Feature: generic declarations and types

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 8, 2021

Previous ID SR-14740
Radar rdar://problem/79094446
Original Reporter ocky7 (JIRA User)
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s
Labels New Feature, LanguageFeatureRequest
Assignee None
Priority Medium

md5: d2264e785671907a61a2b68407a14555

Issue Description:

Consider extension function:

extension Array where Array.Element : Floopable {
    func floopAll() {
        for element in self {
            element.floop()
        }
    }
}

My external library FloopLibrary provides utility functions that return arrays of floopables either as Array<Floopable> or specialized Array<SpecializedFloopable> (where SpecializedFloopable : Floopable).

I would like my extension function to just work on both types, and like this it only works on types that strictly inherit Floopable. I could make the where clause be 'Array.Element == Floopable' and typecast everything to Array<Floopable>, but I think this typecasting boilerplate is not so elegant and the compiler should be smart enough to see that a protocol 'inherits' itself.

So I'd prefer something like:

extension Array where Array.Element <= Floopable {
    func floopAll() {
        for floop in self {
            floop.floop()
        }
    }
}

Is it possible?

Full demo:

protocol Floopable {
    func floop()
}

struct TypeA : Floopable {
    func floop() {
        print("flooping")
    }
}

struct TypeB : Floopable {
    func floop() {
        print("fl00p1ng")
    }
}

extension Array where Array.Element : Floopable {
    func floopAll() {
        (self as Array<Floopable>).eqFloopAll() // I do not like this force casting
    }
}
extension Array where Array.Element == Floopable {
    func eqFloopAll() {
        for floop in self {
            floop.floop()
        }
    }
}

let homogenousFloopables: [TypeA] = [TypeA(), TypeA()]
let heterogenousFloopables: [Floopable] = [TypeA(), TypeB()]

homogenousFloopables.floopAll() // eqFloopAll() not available
heterogenousFloopables.eqFloopAll() // floopAll() not available

(homogenousFloopables as [Floopable]).eqFloopAll() // I do not like this force casting
@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself generics Feature: generic declarations and types and removed new feature labels Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation generics Feature: generic declarations and types
Projects
None yet
Development

No branches or pull requests

3 participants