Skip to content

[SE-0089] [in progress] Rename string reflection init #3212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public func run_ObjectiveCBridgeFromNSStringForced(_ N: Int) {

@inline(never)
func testObjectiveCBridgeToNSString() {
let nativeString = String("Native")
let nativeString = "Native"

var s: NSString?
for _ in 0 ..< 10_000 {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/StdlibCoreExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public func == (lhs: TypeIdentifier, rhs: TypeIdentifier) -> Bool {
extension TypeIdentifier
: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
return String(value)
return String(describing: value)
}
public var debugDescription: String {
return "TypeIdentifier(\(description))"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/private/StdlibUnittest/StringConvertible.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ extension CustomPrintableValue : CustomDebugStringConvertible {
public func expectPrinted<T>(
expectedOneOf patterns: [String], _ object: T, ${TRACE}
) {
let actual = String(object)
let actual = String(describing: object)
if !patterns.contains(actual) {
expectationFailure(
"expected: any of \(String(reflecting: patterns))\n"
Expand Down
12 changes: 12 additions & 0 deletions stdlib/public/core/Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ extension Bool : Equatable, Hashable {
}
}

extension Bool : LosslessStringConvertible {
public init?(_ description: String) {
if description == "true" {
self = true
} else if description == "false" {
self = false
} else {
return nil
}
}
}

//===----------------------------------------------------------------------===//
// Operators
//===----------------------------------------------------------------------===//
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ public struct Character :
internal var _representation: Representation
}

extension Character : CustomStringConvertible {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this conformance? Character conforms to Streamable.

Copy link
Contributor

@beccadax beccadax Jun 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seemingly vacuous conformances of Character and String are meant to support end-user uses like:

struct Passport {
    var number: Int64
    var genderMarker: Character
    var surname: String}

passport.number = try prompt("Passport number?")
passport.genderMarker = try prompt("Gender marker?")
passport.surname = try prompt("Last name?")

func prompt<T: LosslessStringConvertible>(_ message: String) throws -> T {
    while true {
        print(message, terminator: "")
        guard let line = readLine() else { throw CustomsCheckpointError.noInput }
        if let value = T(line) { return value }
        print("I didn't understand; please try again.")
    }
}

Or (if it wants to go to the trouble of escaping and unescaping strings) conditional conformances like:

extension Range: LosslessStringConvertible where Bound: LosslessStringConvertible {}

The conformances are trivial, but they help make code which is generic on LosslessStringConvertible more useful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's because LosslessStringConvertible refines CustomStringConvertible.

public var description: String {
return String(self)
}
}

extension Character : LosslessStringConvertible {}

extension Character : CustomDebugStringConvertible {
/// A textual representation of the character, suitable for debugging.
public var debugDescription: String {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ImplicitlyUnwrappedOptional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension ImplicitlyUnwrappedOptional : CustomStringConvertible {
public var description: String {
switch self {
case .some(let value):
return String(value)
return String(describing: value)
case .none:
return "nil"
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ extension String {
/// }
///
/// let p = Point(x: 21, y: 30)
/// print(String(p))
/// print(String(describing: p))
/// // Prints "Point(x: 21, y: 30)"
///
/// After adding `CustomStringConvertible` conformance by implementing the
Expand All @@ -867,11 +867,11 @@ extension String {
/// }
/// }
///
/// print(String(p))
/// print(String(describing: p))
/// // Prints "(21, 30)"
///
/// - SeeAlso: `String.init<Subject>(reflecting: Subject)`
public init<Subject>(_ instance: Subject) {
public init<Subject>(describing instance: Subject) {
self.init()
_print_unlocked(instance, &self)
}
Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/core/OutputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public protocol CustomStringConvertible {
var description: String { get }
}

public protocol LosslessStringConvertible : CustomStringConvertible {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation to the protocol and the initializer requirement.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still relevant.

init?(_ description: String)
}

/// A type with a customized textual representation suitable for debugging
/// purposes.
///
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StaticString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public struct StaticString

extension StaticString {
public var customMirror: Mirror {
return Mirror(reflecting: String(self))
return Mirror(reflecting: String(describing: self))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, as a strength reduction, we can directly query .description, since we are in the same type:

return Mirror(reflecting: description)

}
}

Expand Down
17 changes: 17 additions & 0 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,23 @@ extension String {
return _nativeUnicodeUppercaseString(self)
#endif
}

public // @testable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initializer is just public, it is not @testable. @testable marks things that are public just for the sake of testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still applicable.

init<T: LosslessStringConvertible>(_ v: T) {
Copy link
Contributor

@gribozavr gribozavr Jun 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space before the colon. Could you also think of a better parameter name?

extension String {
  public init<Subject>(_ instance: Subject)
}

A doc comment would also be good to have.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still applicable.

self = v.description
}
}

extension String : CustomStringConvertible {
public var description: String {
return self
}
}

extension String : LosslessStringConvertible {
public init?(_ description: String) {
self = description
}
}

extension String {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringInterpolation.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension String : StringInterpolationConvertible {
///
/// - SeeAlso: `StringInterpolationConvertible`
public init<T>(stringInterpolationSegment expr: T) {
self = String(expr)
self = String(describing: expr)
}

% for Type in StreamableTypes:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringUTF16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ extension String {
return UTF16View(_core)
}
set {
self = String(newValue)
self = String(describing: newValue)
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringUTF8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ extension String {
return UTF8View(self._core)
}
set {
self = String(newValue)
self = String(describing: newValue)
}
}

Expand Down
13 changes: 12 additions & 1 deletion stdlib/public/core/UnicodeScalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public struct UnicodeScalar :
extension UnicodeScalar : CustomStringConvertible, CustomDebugStringConvertible {
/// An escaped textual representation of the Unicode scalar.
public var description: String {
return "\"\(escaped(asASCII: false))\""
return String(value)
}
/// An escaped textual representation of the Unicode scalar, suitable for
/// debugging.
Expand All @@ -258,6 +258,17 @@ extension UnicodeScalar : CustomStringConvertible, CustomDebugStringConvertible
}
}

extension UnicodeScalar : LosslessStringConvertible {
public init?(_ description: String) {
if let v = UInt32(description) where (v < 0xD800 || v > 0xDFFF)
&& v <= 0x10FFFF {
self = UnicodeScalar(v)
Copy link
Contributor

@gribozavr gribozavr Jun 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the intent of the LosslessStringConvertible protocol is that T(String(someInstanceOfT)) == someInstanceOfT. That is, this initializer should parse back what String initializer returns.

(swift) var c: UnicodeScalar = "a"
// c : UnicodeScalar = "a"
(swift) c.description
// r0 : String = "\"a\""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still relevant.

}

return nil
}
}

extension UnicodeScalar : Hashable {
/// The Unicode scalar's hash value.
///
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ extension Unsafe${Mutable}BufferPointer : CustomDebugStringConvertible {
/// A textual representation of `self`, suitable for debugging.
public var debugDescription: String {
return "Unsafe${Mutable}BufferPointer"
+ "(start: \(_position.map(String.init(_:)) ?? "nil"), count: \(count))"
+ "(start: \(_position.map(String.init(describing:)) ?? "nil"), count: \(count))"
}
}
%end
Expand Down