[swift-evolution] generic associatedtype?
Robert Widmann
devteam.codafi at gmail.com
Sun Sep 18 20:22:56 CDT 2016
> On Sep 17, 2016, at 6:37 PM, Jens Persson via swift-evolution <swift-evolution at swift.org> wrote:
>
> Has there been any discussions about the possibility of having generic associatedtypes?
>
> I (naively) think that it would open up a lot of possibilities.
> Because if, for example, we could do this:
>
> protocol CountType {
> associatedtype Storage<E>
> ...
> }
>
> Then we could do this:
>
> struct Count1 : CountType {
> typealias Storage<E> = (E)
> ...
> }
> struct Count2 : CountType {
> typealias Storage<E> = (E, E)
> ...
> }
> struct Count3 : CountType {
> typealias Storage<E> = (E, E, E)
> ...
> }
> ...
> protocol StaticArrayType {
> associatedtype Count: CountType
> associatedtype Element
> ...
> }
> struct StaticArray<C: CountType, Element> : StaticArrayType {
> typealias Count = C
> var storage: C.Storage<Element>
> ...
> }
>
>
>
> Would adding support for generic associatedtypes be possible? Are there any plans for it?
Possible, yes, plans, no.
Generic associated types go part and parcel with higher-kinded quantification and higher-kinded types, the implementation challenges of which have been discussed thoroughly on this list and elsewhere. Is there a particular flavor you had in mind?
One major problem is that presumably you’d want to constrain such a generic associatedtype and then we’d have to have some kind of type-level-yet-runtime-relevant apply of a generic witness table to another potentially generic witness. It’s not clear what that kind of thing would look like, or how far it would have to be taken to get the kind of support you would expect from a basic implementation higher associatedtypes. Implementations in languages like Haskell tend to also be horrendously inefficient - I believe Edward Kmett calls is the “Mother May I” effect of forcing a witness table to indirect through multiple layers of the witness because inlining necessarily fails for the majority of these things in the MTL.
tl;dr Basic examples like the ones you cite hide the kinds of tremendously evil fun things you can do once you have these kinds of features.
>
> (
> I tried searching for it but I found only this:
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/015089.html <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160411/015089.html>
> )
>
> Thanks,
> /Jens
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160918/9859dc2f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160918/9859dc2f/attachment.sig>
More information about the swift-evolution
mailing list