extension Dictionary where Key == MyStructType

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

···

On Wed, Jan 27, 2016 at 12:16 PM, Darren Mo via swift-users <swift-users@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?

--
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@gmail.com>*/

Thanks!

···

On Jan 27, 2016, at 4:24 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Wed, Jan 27, 2016 at 12:16 PM, Darren Mo via swift-users > <swift-users@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@gmail.com>*/

Wait but then I can’t use Dictionary methods inside of DictionaryType, right?

···

On Jan 27, 2016, at 4:24 PM, Dmitri Gribenko <gribozavr@gmail.com> wrote:

On Wed, Jan 27, 2016 at 12:16 PM, Darren Mo via swift-users > <swift-users@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@gmail.com>*/

Just declare ones that you need in DictionaryType as requirements.

Dmitri

···

On Wed, Jan 27, 2016 at 1:40 PM, Darren Mo <darren.mo@me.com> wrote:

Wait but then I can’t use Dictionary methods inside of DictionaryType, right?

--
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@gmail.com>*/