[swift-evolution] [Pre-pitch] Conform Int (and others) to LosslessStringConvertible

Matheus Martins matheusmcrds at gmail.com
Sun Jul 23 11:15:44 CDT 2017


I came across what i think is an inconsistency in the standard library.

Why are some numeric types like Int not conforming to
LosslessStringConvertible by default while Float and Double do conform to
it in the standard library?

I came across this while trying to write some generic code:

func array<T: LosslessStringConvertible>(_ text: String) -> [T?] {
return text.components(separatedBy: " ").map { T($0) }
}

It seems rather inconsistent to me that that allows me to do:

let floats: [Float] = array("1.0 2.2 3")

but i can't do:

let ints: [Int] = array("1 2 3 4 5 6")
"type 'Int' does not conform to protocol 'LosslessStringConvertible'"

To further my point, Int is used as the sole example in the
LosslessStringConvertible docs:
http://swiftdoc.org/v3.0/protocol/LosslessStringConvertible/
Yet it does not actually conform to it.

This seems way too basic for me to feel comfortable pitching it here, but i
searched and couldn't find any discussion on this on any mailing list or
website.

Thanks,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170723/707ef50a/attachment.html>


More information about the swift-evolution mailing list