Why aren't Range and Array LosslessStringConvertible?

Are there any reasons why eg Array, Range and ClosedRange shouldn't conform to LosslessStringConvertible?

Naive implementation

extension Range: LosslessStringConvertible where
Bound: LosslessStringConvertible
{
public init?(_ description: String) {
let c = description.components(separatedBy: "..<")
guard c.count == 2, let lower = Bound(c[0]), let upper = Bound(c[1])
else { return nil }
self = lower ..< upper
}
}
extension ClosedRange: LosslessStringConvertible where
Bound: LosslessStringConvertible
{
public init?(_ description: String) {
let c = description.components(separatedBy: "...")
guard c.count == 2, let lower = Bound(c[0]), let upper = Bound(c[1])
else { return nil }
self = lower ... upper
}
}
extension Array: LosslessStringConvertible where
Element: LosslessStringConvertible
{
public init?(_ description: String) {
self.init()
for oe in description
.dropFirst().dropLast().components(separatedBy: ", ")
{
guard let e = Element(oe) else { return nil }
self.append(e)
}
}
}

The issue here is that lossless strings aren’t a text safe encoding - E.g code trying to parse an array of strings wouldn’t know if a comma was emitted by it or one of its Elements

4 Likes

Right, there is no limit on what the lossless string to which a value is converted may contain, so no way to ensure that any delimiter (e.g., commas, "...", etc.) doesn't appear in the lossless string representation of the bound of a range or an element of an array in a way that is distinguishable from the delimiter. It is perfectly valid for me to create a custom type where the lossless string representation of a value is ,,,....

2 Likes
Terms of Service

Privacy Policy

Cookie Policy