4

I'm trying to get a specific Dictionary type to conform to a protocol.

typealias FirebaseDictionary = Dictionary<String, FirebaseValue>

I would like to have the conform to a FirebaseValue protocol

protocol FirebaseValue {
    // stuff here
}

I tried this

extension FirebaseDictionary: FirebaseValue {

}

but I got an error Constrained extension must be declared on the unspecialized generic type 'Dictionary' with constraints specified by a 'where' clause. So I now have this

extension Dictionary where Key == String, Value == FirebaseValue {

}

but I cannot figure out the proper syntax to make this conform to a protocol, if at all possible. If not possible is there any other way to achieve the same effect? I'm trying to only allow specific types into a property, and be able to discern easily what type they are when read back.

This question was asked but was given no definitive answer, and it may have changed regardless

Justin Oroz
  • 614
  • 8
  • 14
  • Swift doesn't have [conditional conformance](https://github.com/apple/swift-evolution/blob/master/proposals/0143-conditional-conformances.md) yet – workarounds would either be a wrapper type that contains a `[String: FirebaseValue]` and provides the conformance to `FirebaseValue` (compare https://stackoverflow.com/q/33332613/2976878), or if possible, just define additional overloads of the (presumed) functions that you were planning on using with `FirebaseValue` to work with `[String: FirebaseValue]` as well. – Hamish Jun 20 '17 at 21:26
  • @Hamish Thank you. I tried as in the link but I cannot get `struct FirebaseDictionary` to conform the protocol `FirebaseValue` even when empty. I get `Inheritance from non-protocol type 'FirebaseValue'` – Justin Oroz Jun 20 '17 at 21:53
  • I figured it out, i had improper syntax – Justin Oroz Jun 20 '17 at 22:01
  • 2
    @JustinOroz Post the results please – Cerlin Aug 23 '17 at 10:49

1 Answers1

2

Since Swift 4.2 you can do this with:

extension Dictionary : FirebaseValue where Key == String, Value == FirebaseValue {
}
Paul
  • 1,897
  • 1
  • 14
  • 28