[swift-evolution] [swift-evolution-announce] [Returned for revision] SE-0089: Renaming String.init<T>(_: T)

Brent Royal-Gordon brent at architechies.com
Sat May 28 17:33:59 CDT 2016


> If anyone is curious, the revised proposal can be found here: https://github.com/austinzheng/swift-evolution/blob/2b31df6163f5c5d1975a37e72c6996b82d61a5c6/proposals/0089-rename-string-reflection-init.md

This is great. My only suggestion is that `init?(description: String)` should be `init?(_ description: String)`, since by definition the conversion is fullwidth. But you may already be planning to incorporate that change, since Chris mentioned it.

Just quickly scanning the docs on swiftdoc.org, here are protocols which I think should conform to LosslessStringConvertible:

* FloatingPoint
* Integer
* Boolean?

And here are concrete types:

* Bool (if not Boolean)
* Character
* String (kind of circular, but I think it's a good idea) and its views
* String.Index? (Not sure how to represent this; UTF-8 offset, maybe?)
* UnicodeScalar

Protocols which would require conditional conformances:

* RangeReplaceableCollection where Iterator.Element == Character or UnicodeScalar
* SetAlgebra where Iterator.Element == Character or UnicodeScalar

These protocols were chosen because they include `init<S: Sequence where S.Iterator.Element == Iterator.Element>(_: S)` initializers.

Generic types which would require conditional conformances:

* ClosedRange where Bound: LosslessStringConvertible
* CountableClosedRange where Bound: LosslessStringConvertible
* CountableRange where Bound: LosslessStringConvertible
* Range where Bound: LosslessStringConvertible

These would be non-trivial, but useful. They could have a serialization like:

	var description: String {
		let operator = "..<"	// or "..." for the closed ranges
		func escape(_ string: String) -> String {
			let escapedOperator = String(operator.characters.flatMap { ["\\", $0] })
			return string.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: operator, with: escapedOperator)
		}
		
		return escape(String(lowerBound)) + " " + operator + " " + escape(String(upperBound))
	}

The `init(_: String)` method would need to handle the escapes—hence the nontrivial-ness.

Technically possible, but probably unwise:

* ObjectIdentifier
* UnsafePointer and friends
* Set.Index and Dictionary.Index

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list