[swift-evolution] Partially Constrained Protocols [Was: [Proposal] Separate protocols and interfaces]

Thorsten Seitz tseitz42 at icloud.com
Mon Jan 18 14:56:59 CST 2016


> Am 18.01.2016 um 21:18 schrieb Douglas Gregor via swift-evolution <swift-evolution at swift.org>:
> 
> I suggest you also look at what can be done with an existential value that hasn't been opened explicitly. Can I call "generate" on a SequenceType value, and what do I get back?

I think this should be possible as SequenceType.Generator is in a covariant position and changes covariantly with the Element type.
The type should use the same bounds as that of the SequenceType:

var zs: protocol<SequenceType where Generator.Element : SomeProtocol>
var gz: protocol<SequenceType.GeneratorType where .Element : SomeProtocol> = zs.generate()
var z: SomeProtocol? = gz.next()

If no bounds would have been given Element would have the type Any.

Should we have path-dependent types like Scala?
(see e.g. http://stackoverflow.com/questions/2693067/what-is-meant-by-scalas-path-dependent-types)

var zs: protocol<SequenceType where Generator.Element : SomeProtocol>
var gz: zs.Generator = zs.generate()
var z: SomeProtocol? = gz.next()
var ys: protocol<SequenceType where Generator.Element : SomeProtocol>
var gy: ys.Generator = gz // type error: path-dependent types ys.Generator and zs.Generator differ

-Thorsten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160118/c1cd06db/attachment.html>


More information about the swift-evolution mailing list