So, I implement the following:
- A simple protocol
LanguageTypethat matchesHashable - A
Translateable, which should allow you to get (and install) a [String]from the dictionary using the LanguageTypeas key
protocol LanguageType: Hashable {
var description: String { get }
}
extension LanguageType {
var description: String { return "\(Self.self)" }
var hashValue: Int { return "\(Self.self)".hashValue }
}
func ==<T: LanguageType, U: LanguageType>(left: T, right: U) -> Bool {
return left.description == right.description
}
protocol Translateable {
var translations: [LanguageType: [String]] { get set }
}
As usual, Swift has a problem with how the protocol is used LanguageType:

From what I read, this is due to the fact that Swift does not support Existentials , which leads to the fact that the protocols are not actually first class types.
In the context of generics, this problem can usually be solved with an erasable erasable wrap.
In my case, there are no generics or related types.
translations.Key LanguageType, , LanguageType.
, , :
protocol Translateable {
typealias Language: LanguageType
var translations: [Language: [String]] { get set }
}
- . , , - ,
translations.Key LanguageType
, , LanguageType Translateable.
, ?
1:
, LanguageType ( Equatable). LanguageType.
2:
, LanguageType . AnyLanguage:
struct AnyLanguage<T>: LanguageType {
private let _description: String
var description: String { return _description }
init<U: LanguageType>(_ language: U) { _description = language.description }
}
func ==<T, U>(left: AnyLanguage<T>, right: AnyLanguage<U>) -> Bool {
return left.description == right.description
}
LanguageType, , Translateable :
protocol Translateable {
typealias T
var translations: [AnyLanguage<T>: [String]] { get set }
}
:
AnyLanguage:
struct AnyLanguage: LanguageType {
private(set) var description: String
init<T: LanguageType>(_ language: T) { description = language.description }
}
func ==(left: AnyLanguage, right: AnyLanguage) -> Bool {
return left.description == right.description
}
protocol Translateable {
var translations: [AnyLanguage: [String]] { get set }
}
, T Update 2, . .