[swift-users] How to add this generic static func as a protocol requirement?

Jens Persson jens at bitcycle.com
Fri Jul 7 12:26:12 CDT 2017


That will not work since R can be different types depending on T (R == (T,
T) for S2 and R == (T, T, T) for S3).
I guess Swift would have to support generic associated type for it to work:
protocol P {
    associatedtype R<U>
    static func foo<T>(_ v: T) -> R<T>
}
struct S2 : P {
    typealias R<U> = (U, U)
    static func foo<T>(_ v: T) -> R<T> {
        return (v, v)
    }
}
struct S3 : P {
    typealias R<U> = (U, U, U)
    static func foo<T>(_ v: T) -> R<T> {
        return (v, v, v)
    }
}

But (sadly) according to previous discussions it seems like generic
associated types are very much out of scope for Swift 4.

PS

The generic static func was only a (what I guess is another) failed attempt
at a workaround, and what I'm really trying to achieve is something that is
perhaps better described like this:

protocol VectorCount {
    associatedtype Storage<E>
}
…
enum VectorCount2 : VectorCount {
    typealias Storage<E> = (E, E)
    …
}
enum VectorCount3 : VectorCount {
    typealias Storage<E> = (E, E, E)
    …
}
…
struct Vector<Count: VectorCount, Element> {
    var elements: Count.Storage<Element>
    …
}

That is, Vector is a statically allocated array type, with type level Count
and Element as type parameters.
I currently have a solution which is based around separate generic vector
types for each VectorCountX type, but they are generic only over Element
not both Count and Element, like this:
…
protocol VectorProtocol {
    associatedtype Count: VectorCount
    associatedtype Element
    …
}
struct V2<Element> : VectorProtocol {
    typealias Count = VectorCount2
    var elements: (Element, Element)
    …
}
struct V3<Element> : VectorProtocol {
    typealias Count = VectorCount3
    var elements: (Element, Element, Element)
    …
}
…
And you can of course also make specific types like eg:
struct RgbaFloatsSrgbGamma : Vector {
    typealias Count = VectorCount4
    typealias Element = Float
    …
}

It's working and it can be written in a way such that the optimizer can do
a good job, vectorizing operations on vectors of 4 floats etc. It also
makes it possible to write generic Table<Element, Index> where Index is an
N-dimensional vector of Int elements, which enables me to write
N-dimensional data processing generically, eg a calculating a summed area
table for data of any dimension, etc.  But it would be much better to base
the vector types around a concept in which both Element and Count are type
parameters, and not just Element, ie Vector<Count, Element> rather than
V1<Element>, V2<Element>, …

/Jens


On Fri, Jul 7, 2017 at 6:49 PM, Slava Pestov <spestov at apple.com> wrote:

> Try using an associated type for the result of foo():
>
> protocol P {
>   associatedtype R
>
>   static func foo<T>(_ v: T) -> R
> }
>
> Slava
>
> > On Jul 7, 2017, at 1:50 AM, Jens Persson via swift-users <
> swift-users at swift.org> wrote:
> >
> > protocol P {
> >     // …
> >     // For example the following will not work:
> >     // static func foo<T, R>(_ v: T) -> R
> >     // Is there some other way?
> >     // …
> > }
> > struct S2 : P {
> >     static func foo<T>(_ v: T) -> (T, T) {
> >         return (v, v)
> >     }
> > }
> > struct S3 : P {
> >     static func foo<T>(_ v: T) -> (T, T, T) {
> >         return (v, v, v)
> >     }
> > }
> >
> > (I'm guessing but am not sure that I've run into (yet) a(nother)
> situation which would require higher kinded types?)
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170707/cc670d08/attachment.html>


More information about the swift-users mailing list