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-3829] Provide a method to force generics to be treated as separate methods #46414

Closed
swift-ci opened this issue Feb 2, 2017 · 2 comments
Closed

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Feb 2, 2017

Previous ID SR-3829
Radar None
Original Reporter abe.schneider (JIRA User)
Type Improvement
Status Resolved
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee None
Priority Medium

md5: 8203f9a4356a281611a01c8d139b40f2

Issue Description:

Dispatching a generic at run-time can cause problems when calling one generic to another. Specifically, the second generic function called from the first generic function loses its type information even though technically its available at compile time. This makes many techniques used in languages like C++ won't work in Swift.

For example:

protocol DispatchType {}
class DispatchType1: DispatchType {}
class DispatchType2: DispatchType {}

func doBar<D:DispatchType>(value:D) {
    print("general function called")
}

func doBar(value:DispatchType1) {
    print("DispatchType1 called")
}

func doBar(value:DispatchType2) {
    print("DispatchType2 called")
}

func test<D:DispatchType>(value:D) {
    doBar(value: value)
}

let d1 = DispatchType1()
let d2 = DispatchType2()

test(value: d1)     // "general function called", but expected "DispatchType1 called"
test(value: d2)     // "general function called", but expected "DispatchType2 called"

For the cases where this behavior is desired, a `@compile_time` attribute could be introduced to create the separate `test` functions so the desired behavior occurs. This would minimize the amount of code affected.

While using a protocol to get this behavior is possible, it can force large monolithic classes to occur (e.g. on stackoverflow). This modification would allow Swift to better support both an object orient and functional paradigm under these conditions.

@belkadan
Copy link
Contributor

belkadan commented Feb 2, 2017

This is a fundamental language change that Swift is not interested in as such. The way to get type-based dispatch in Swift is to use protocols.

@hamishknight
Copy link
Collaborator

The problem is that protocols just don't cut it sometimes – a prime example of this is attempting to implement a method to work with all the elements of an arbitrarily deep nested collection (such as asked for in [SR-3638]). If there was a way to force Swift to specialise generic functions, and then dispatch based on that specialisation, you could say things like:

extension Collection {
    var flatCount: IndexDistance {
        return count
    }
}

extension Collection where Iterator.Element: Collection {
    var flatCount: IndexDistance {
        return self.reduce(0) { $0 + $1.flatCount }
    }
}

AFAIK, there's currently no easy way to achieve this for arbitrary collections using either protocols or typecasting.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants