[swift-evolution] Partially Constrained Protocols [Was: [Proposal] Separate protocols and interfaces]
dgregor at apple.com
Mon Jan 18 14:18:51 CST 2016
Sent from my iPhone
> On Jan 13, 2016, at 2:50 PM, David Waite <david at alkaline-solutions.com> wrote:
>>> On Jan 4, 2016, at 11:40 AM, Douglas Gregor <dgregor at apple.com> wrote:
>>> On Jan 4, 2016, at 10:30 AM, Matthew Johnson <matthew at anandabits.com> wrote:
>>> Interesting. I was under the impression the core team was taking on the generics features.
>> We are, and lots of them. I doubt we can handle another.
>>> I am also very interested in helping to accelerate the process if that is possible (whether that means Swift 3 or not).
>>> Are you thinking specifically about unbound existentials like in your Equatable example or also partly / fully bound existentials such as SequenceType<Generator.Element == Int>?
>> I was thinking mostly about the former. However, the latter is also a highly-requested feature [*] that complements this one, and I think it’s reasonable to discuss both together.
>> - Doug
>> [*] And is often the underlying reason why developers ask for parameterized protocols.
> Resurrecting this conversation, I’ve been working on a proposal to (I believe) cover all these points. I’ve decided it is probably better to resurrect this conversation first in order to start having discussion about the features in general, rather than my specific proposal.
> Specifically I am seeking to define:
> 1. The ability for protocols with Self or associated type constraints to be used other than as generic constraints on concrete types (i.e. as existential types)
> 2. The ability to use said protocols with none, some, or all of the associated types being constrained/bound.
> 3. Rules on what functionality (methods, properties) are exposed when working with a protocol (in various states of constraint) rather than a concrete type.
> 4. The ability to dynamically capture and use a concrete type for the purpose of satisfying Self constraints. This one is the least specified at the moment, but seems the best way to deal with Self constraints.
> For the first three, I’ve tried to keep the behavior as close to as if I had instead defined a new protocol without self or associated types based on the constraints given, then had every concrete type in the system meeting the old protocol and the constraints be extended to implement the new one. (This model ignores that there are cases you cannot declare extensions like this today, such as extending a protocol to support another protocol, or extending either a concrete type or protocol to support a new protocol conditionally.)
> These new protocols include exposing methods which are invariant based on the constraints given, or which have covariant return types (e.g. functions returning Self would indicate they return instances of this protocol).
> The rule complexity hopefully will be sophisticated enough to expose (for example) an unbound SequenceType filter method with the approx. signature: “func filter(includedElements:(Any) throws -> Bool ) rethrows -> [Any]” based on existing function/array behavior.
> Non-bikeshedded syntax currently is something in the manner of:
> let x:SequenceType = [1,2,3] // no constraints specified whatsoever
> let y:protocol<SequenceType where Generator.Element == String> = [“foo”, “bar”] // partially constrained
Not wanting to start throwing paint, but "Generator.Element" could be ambiguous with a Generator in the lexical scope. You'll probably want some way to differentiate it (eg, a leading dot). Otherwise, this is the syntactic direction that I think makes the most sense.
> One interesting side-effect to note is that SequenceType could be redefined to only have “Element” as an associated type. Instead of Generator or SubSequence being associated types, methods could return partially constrained GenericType or SequenceType from the appropriate methods. This would be one way of eliminating issues today with recursive use of associated types (as well as SubSequence over-constraining the output of various algorithms to a single concrete type)
Assuming nobody cares about the identity of the Generator type, which is probably a safe assumption. Note that one could implement this design today by using a function taking no arguments and returning an Element? in lieu of "Generator".
> For the “opening” of an existential type to find the concrete type it stores dynamically, I’m currently using a different syntax just because the “open x as T” originally given makes ‘open’ a keyword’ and makes it unclear where ’T’ came from
Yes, the issue of "which name did I introduce?" is tricky here.
> - I’m instead overloading the typealias keyword when used within an expression context:
> typealias T = x.dynamicType
> let xT = x as! T
x will have to be immutable for this to make sense. That's why my ugly "open" expression extracts a new value and gives it a fresh type in one lexically-scoped block. Both the type and the value end up being scoped. Otherwise, one might reassign "x" with a different dynamic type... Then what does T mean?
> My next step is to become more familiar with the bounds of what can be done with this today by the AST/SIL. In particular, using “typealias” as above may imply capabilities which are simply more dynamic than is feasible/appropriate to support.
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?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution