[swift-evolution] Protocol requirement `where` clauses constraining associated types
Slava Pestov
spestov at apple.com
Thu Feb 9 01:12:39 CST 2017
> On Feb 8, 2017, at 10:30 PM, Douglas Gregor <dgregor at apple.com> wrote:
>
>
>> On Feb 8, 2017, at 10:21 PM, Slava Pestov <spestov at apple.com> wrote:
>>
>> Hah, Doug and I were just discussing this.
>>
>> In Swift 3.1, we generalized where clauses to allow them to add requirements on outer generic parameters. However we did not remove the diagnostic prohibiting a where clause from being attached to a non-generic method. In theory this can be made to work; the only slightly tricky thing is we will get a GenericParamList with zero parameters but non-zero requirements, which would require shuffling some things around to avoid assertions.
>>
>> This would be a good starter project for someone who wanted to learn more about the generics system.
>>
>> As for index(of:) and the specific details of the stdlib that are involved here, I have no idea — I’m just talking about the bogus diagnostic itself.
>
> Well, I think Brent is talking about doing this on a protocol requirement, which is more interesting because not all conforming types would satisfy the requirement…
Since there would be no way to invoke the requirement on such a type, could we leave the entry blank in the witness table or emit a fatalError() thunk or something?
Slava
>
> - Doug
>
>>
>> Slava
>>
>>> On Feb 8, 2017, at 9:57 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>> In an article on `Collection` today*, Ole Begemann points out that `index(of:)`, along with other `Equatable`- and `Comparable`-constrained `Collection` methods, cannot be overridden. Actually, it *can* be, but only through a private mechanism—there's a `_customIndexOfEquatableElement(_:)` method that's invisible in the generated interface. But that only proves the need for a way to make methods like these overridable.
>>>
>>> The problem is that the `index(of:)` method should only be offered when the element is `Equatable`—otherwise it simply won't work. But there's no way to specify this rule in current Swift. In theory, we could describe such a requirement with something like this:
>>>
>>> func index(of element: Iterator.Element) -> Index? where Iterator.Element: Equatable
>>>
>>> But this is not permitted—you get an error indicating that `where` clauses are only allowed on generic methods. Adding a spurious generic parameter allows this code to compile, but with a deprecation warning indicating that this is deprecated. I don't know if it would actually behave correctly, however.
>>>
>>> Is this a feature we should add? Is this the way to add it? Would it have non-additive ABI impact? (The private `index(of:)` override would certainly go away, but that's why it's private, I suppose.) I don't seem to remember seeing something like this in the generics manifesto, so I thought it was worth bringing up.
>>>
>>>
>>>
>>> * https://oleb.net/blog/2017/02/sorted-array/
>>>
>>> --
>>> Brent Royal-Gordon
>>> Architechies
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
More information about the swift-evolution
mailing list