[swift-users] extension Dictionary where Key == MyStructType
Darren Mo
darren.mo at me.com
Wed Jan 27 15:40:03 CST 2016
Wait but then I can’t use Dictionary methods inside of DictionaryType, right?
> On Jan 27, 2016, at 4:24 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>
> On Wed, Jan 27, 2016 at 12:16 PM, Darren Mo via swift-users
> <swift-users at swift.org> wrote:
>> I want to add a method to Dictionary but only if the key is a specific
>> non-protocol type.
>>
>> I tried:
>> extension Dictionary where Key == MyStructType
>>
>> This yields “error: same-type requirement makes generic parameter 'Key'
>> non-generic”.
>>
>> Is there a way to do this that doesn’t involve subclassing Dictionary?
>
> There is a workaround that involves adding an otherwise a useless
> protocol. I leave it up to you to decide whether it is worth it.
>
> protocol DictionaryType {
> typealias Key
> typealias Value
> }
>
> extension Dictionary : DictionaryType {}
>
> extension DictionaryType where Key == MyStructType { ... }
>
> Dmitri
>
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-users
mailing list