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

2017-07-07 Thread Jens Persson via swift-users
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
static func foo(_ v: T) -> R
}
struct S2 : P {
typealias R = (U, U)
static func foo(_ v: T) -> R {
return (v, v)
}
}
struct S3 : P {
typealias R = (U, U, U)
static func foo(_ v: T) -> R {
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
}
…
enum VectorCount2 : VectorCount {
typealias Storage = (E, E)
…
}
enum VectorCount3 : VectorCount {
typealias Storage = (E, E, E)
…
}
…
struct Vector {
var elements: Count.Storage
…
}

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 : VectorProtocol {
typealias Count = VectorCount2
var elements: (Element, Element)
…
}
struct V3 : 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 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 rather than
V1, V2, …

/Jens


On Fri, Jul 7, 2017 at 6:49 PM, Slava Pestov  wrote:

> Try using an associated type for the result of foo():
>
> protocol P {
>   associatedtype R
>
>   static func foo(_ v: T) -> R
> }
>
> Slava
>
> > On Jul 7, 2017, at 1:50 AM, Jens Persson via swift-users <
> swift-users@swift.org> wrote:
> >
> > protocol P {
> > // …
> > // For example the following will not work:
> > // static func foo(_ v: T) -> R
> > // Is there some other way?
> > // …
> > }
> > struct S2 : P {
> > static func foo(_ v: T) -> (T, T) {
> > return (v, v)
> > }
> > }
> > struct S3 : P {
> > static func foo(_ 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@swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
>
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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

2017-07-07 Thread Slava Pestov via swift-users
Try using an associated type for the result of foo():

protocol P {
  associatedtype R

  static func foo(_ v: T) -> R
}

Slava

> On Jul 7, 2017, at 1:50 AM, Jens Persson via swift-users 
>  wrote:
> 
> protocol P {
> // …
> // For example the following will not work:
> // static func foo(_ v: T) -> R
> // Is there some other way?
> // …
> }
> struct S2 : P {
> static func foo(_ v: T) -> (T, T) {
> return (v, v)
> }
> }
> struct S3 : P {
> static func foo(_ 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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


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

2017-07-07 Thread Jens Persson via swift-users
protocol P {
// …
// For example the following will not work:
// static func foo(_ v: T) -> R
// Is there some other way?
// …
}
struct S2 : P {
static func foo(_ v: T) -> (T, T) {
return (v, v)
}
}
struct S3 : P {
static func foo(_ 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users