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

David Sweeris davesweeris at mac.com
Sun Jul 23 13:35:54 CDT 2017


> On Jul 23, 2017, at 09:15, Matheus Martins via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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.

Sounds like it's a bug to me, either in the docs or stdlib, depending on the intended behavior. Although I doubt they would've used Int in the docs if they weren't intending Int to conform to it.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170723/544bed5a/attachment.html>


More information about the swift-evolution mailing list