Is there a way to add conformance to a protocol for types that already conform to RawRepresentable?
Consider a basic example of a class that can store primitive values in a sqlite database:
protocol DatabaseStoreable {}
extension Int: DatabaseStoreable {}
extension Double: DatabaseStoreable {}
extension String: DatabaseStoreable {}
extension Data: DatabaseStoreable {}
func storeValue<T: DatabaseStoreable>(_ value: T) {
...
}
I'd like to also include any type that conforms to RawRepresentable where RawValue: DatabaseStorable:
extension RawRepresentable: DatabaseStoreable where Self.RawValue: DatabaseStoreable {}
However, this generates the following error:
Extension of protocol 'RawRepresentable' cannot have an inheritance clause
Is there a way to work around this because at the moment I have to declare a second function with the following signature:
func storeValue<T: RawRepresentable>(_ value: T) where T.RawValue: DatabaseStoreable {
// Calls through to the function above.
}
SE-0143, which in turn has a good summary underAlternatives Consideredthat I had missed. - kennycextension RawRepresntable where Self : DatabaseStoreable, Self.RawValue : DatabaseStoreable {}, I don't know if it will work though. - kumowoon1025