kennyc
(Kenny Carruthers)
1
In the code pasted below, the compiler generates the following error:
Type 'Position' does not conform to protocol 'RegistryCodable'
import Cocoa
struct IntBuffer {}
struct StringBuffer {}
protocol RegistryCodable {
associatedtype BufferType
init(registryBuffer: BufferType)
}
extension RegistryCodable where Self: RawRepresentable, Self.RawValue == Int {
init(registryBuffer: IntBuffer) { /* A default implementation. */ }
}
extension RegistryCodable where Self: RawRepresentable, Self.RawValue == String {
init(registryBuffer: StringBuffer) { /* A default implementation. */ }
}
enum Position: String, RegistryCodable {
case top
case bottom
}
Why is this default implementaiton not considered sufficient for making Position conform to RegistryCodable? Is there a way to make it?
I have numerous enums that are RawRepresentable. Something that is RawRepresentable, in this application, can be easily encoded and decoded into a type of Registry. I was hoping I could avoid having to define an implementation for each enum by using an extension as shown above, but that doesn't seem to be the case.
cukr
2
I don't know if there is a technical reason why the thing you wrote isn't sufficient, but you can help the compiler by explicitly writing what is your buffer type
enum Position: String, RegistryCodable {
typealias BufferType = StringBuffer
case top
case bottom
}