Make Float80 Codable

Even if that feature existed, what you write would only be possible if LosslessStringConvertible had a requirement named encode(to:). But it does not, nor could it, because it's a protocol that is entirely orthogonal to encoding and decoding. This is what I mean when I wrote:

1 Like

OK, how about a more traditional OO approach:

fileprivate enum StringCoding: String, CodingKey {
    case key = "stringLiteral"
}
public protocol StringCodable: LosslessStringConvertible, Codable {}
public extension StringCodable {
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: StringCoding.self)
        try container.encode("\(self)", forKey: .key)
    }
    init(from decoder: Decoder) throws {
        let stringRep = try decoder.container(keyedBy: StringCoding.self).decode(String.self, forKey: .key)
        self.init(stringRep)!
    }
}

// Opt in.
extension Float80: StringCodable {}
1 Like