[swift-users] extension Dictionary where Key == MyStructType

Dmitri Gribenko gribozavr at gmail.com
Wed Jan 27 15:24:45 CST 2016

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 { ... }


(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

More information about the swift-users mailing list