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-3638] Make deep flattening of collections possible #46223

Open
swift-ci opened this issue Jan 14, 2017 · 7 comments
Open

[SR-3638] Make deep flattening of collections possible #46223

swift-ci opened this issue Jan 14, 2017 · 7 comments
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3638
Radar None
Original Reporter Reitzig (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 8f2bdef5eb331bcbed53539567b0a791

Issue Description:

It seems nigh if not actually impossible to write a recursive flattening method for `Collection`. By that I mean that `deepFlatten(c: Collection<Collection<...<T>...>>)` should give a plain `Collection<T>` for all nesting depths.

This feels like a task that the type system and compiler should support.

Find some code and a workaround for arrays (for the case of just counting elements) here.

@belkadan
Copy link
Contributor

I'm not actually sure what language feature would support this. In Swift, overload resolution is always performed at compile time; the only way to get dynamic dispatch is to use protocols...or to write it yourself, either by using as? or by passing closures around.

@swift-ci
Copy link
Collaborator Author

Comment by Raphael (JIRA)

@belkadan I don't see how to "write it myself"; if you do, I'd appreciate your answer here!

@swift-ci
Copy link
Collaborator Author

Comment by Raphael (JIRA)

Related comment.

@belkadan
Copy link
Contributor

I don't mean to write deepFlatten yourself; I mean to get dynamic dispatch behavior yourself. You're right that there's no way to do this for arbitrary Collections-of-Collections today. As an update from my January comment, generalized existentials might be able to support this.

@belkadan
Copy link
Contributor

…but you wouldn't know the ultimate element type. That's just not expressible in Swift's type system, and probably never will be. (I don't think it's expressible in Haskell's type system either.)

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 2, 2017

Comment by Raphael (JIRA)

> I mean to get dynamic dispatch behavior yourself

It's been a while, but I remember encountering problems even with manual dynamic checking. You can't check the parameter/associated type at runtime, or can you?

> That's just not expressible in Swift's type system, and probably never will be.

That's fine. We'd need some form of on-demand dynamic dispatch (e.g. table dispatch at runtime). Static dispatch is fine, but it apparently just can't do some things that, imho, are rather fundamental.

@belkadan
Copy link
Contributor

belkadan commented Oct 2, 2017

That's what generialized existentials do.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 improvement
Projects
None yet
Development

No branches or pull requests

2 participants