[swift-users] Type alias with a "where" clause specifying required associated type

Slava Pestov spestov at apple.com
Mon Sep 4 18:52:27 CDT 2017


Hi Rudolf,

What you are describing is not possible right now. The protocol PointType cannot be used as a type at all, because it has an associated type requirement. Also it is not clear what a ‘where’ clause attached to a type alias would mean.

There has been some discussion of ‘generalized existentials’ on the evolution list. So eventually, you might be able to say something like this:

typealias Point = Any<Point : PointType where Point.Float == Float>

Slava

> On Sep 4, 2017, at 6:12 PM, Rudolf Adamkovič via swift-users <swift-users at swift.org> wrote:
> 
> I have the following ProceduralDrawing type:
> 
> public struct ProceduralDrawing<Float, Point: PointType> where Point.Float == Float {
>     // ... code that used Float and Point ...
> }
> 
> public protocol PointType {
>     associatedtype Float: FloatingPoint
>     var x: Float { get }
>     var y: Float { get }
> }
> 
> I would like to avoid adding Point as a generic parameter but the following doesn’t work:
> 
> public struct ProceduralDrawing<Float: FloatingPoint> {
>     // ERROR: 'where' clause cannot be attached to a non-generic declaration
>     typealias Point = PointType where Point.Float == Float
>     // ... code that uses Float and Point ...
> }
> 
> Is there a way to do this? If not, why?
> 
> Thanks!
> 
> R+
> _______________________________________________
> 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/20170904/1ff8d834/attachment.html>


More information about the swift-users mailing list