[swift-evolution] specialize a generic type in a method
Vladimir.S
svabox at gmail.com
Tue Jun 21 10:20:49 CDT 2016
On 20.06.2016 21:38, Charlie Monroe wrote:
> Something like this should work:
>
> protocol P1 {
> associatedtype T
> func foo(t: T)
> func bar<U where U == T, U: Equatable>(t: U)
> }
>
Thank you. Strange, but also can't check this code - compiler crashes on it.
IMO it will be good if we can write just
func bar(t: T & Equatable)
>
>> On Jun 20, 2016, at 8:07 PM, Vladimir.S <svabox at gmail.com> wrote:
>>
>> Thank you for reply. Yes, seems this could be solved by extension. No other solutions?
>>
>> But how should I be if I need such a protocol?
>>
>> protocol P1 {
>> associatedtype T
>> func foo(t: T)
>> func bar(t: T) // where T:Equatable ??
>> }
>>
>> I.e. I need to specify T & Equatable for bar in requirements
>>
>> On 20.06.2016 20:03, Charlie Monroe wrote:
>>> This is IMHO better solved via an extension - it even makes more sense to put together methods whose use is limited only to certain generics constraint:
>>>
>>> extension Foo where I: Equatable {
>>>
>>> func bar(i: I) {
>>> ///
>>> }
>>>
>>> }
>>>
>>>
>>>> On Jun 20, 2016, at 6:57 PM, Vladimir.S via swift-evolution <swift-evolution at swift.org> wrote:
>>>>
>>>> Should we be able to specialize a generic type in a method, if that generic type declared in type's definition like here? :
>>>>
>>>> struct Foo<I> {
>>>> func bar(i: I) where I: Equatable { } // should this work?
>>>> }
>>>>
>>>> As I understand, for concrete instance of foo: Foo<X>, if X is not Equatable, then compiler should prevent us from calling foo.bar(x)
>>>>
>>>> Or, probably, the better way to express this could be :
>>>>
>>>> func bar(i: I & Equatable) { }
>>>> or
>>>> func bar(i: Any<I, Equatable>) { }
>>>>
>>>> Can't check if it possible now because my compiler crashes because of 'where I:Equatable' text (submitted to bugs.swift.org)
>>>> _______________________________________________
>>>> 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