[swift-users] Conditional generic initializers?

Neil Faiman neil.swift at faiman.org
Sun May 15 13:55:25 CDT 2016


Is this a relatively new language change? When I try it with the version of Swift that comes with Xcode 7.3.1, I get

    error: same-type requirement makes generic parameters 'T1' and 'T2' equivalent
    extension Foo where T1 == T2 {
                           ^


> On May 15, 2016, at 1:33 PM, Karl <razielim at gmail.com> wrote:
> 
> Yes. You need to put it in an extension.
> 
> extension Foo where T1 == T2 {
>    convenience init(values: [T1]){
>    ….
>    }
> }
> 
>> On 15 May 2016, at 14:45, Neil Faiman via swift-users <swift-users at swift.org> wrote:
>> 
>> Is it possible for a generic class to have methods (specifically, initializers) which are only defined if the generic parameters meet certain constratins?
>> 
>> Here’s a concrete example:
>> 
>>   class Foo <T1, T2> {
>>       init(pairs: [(T1, T2)]) {}
>>       // What I’d like to be able to doL
>>       convenience init "where T1 == T2" (values: [T1]) { self.init(pairs: values.map{ ($0, $0) }) }
>>   }
>> 
>> That is, I’d like to provide a convenience initializer that takes an array of values instead of pairs, and turns the values into pairs, IF THE TWO GENERIC TYPE PARAMETERS ARE THE SAME.
>> 
>> I can’t find a way to accomplish this. Is there one?




More information about the swift-users mailing list