-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,6 +164,10 @@ public protocol CustomStringConvertible { | |
var description: String { get } | ||
} | ||
|
||
public protocol LosslessStringConvertible : CustomStringConvertible { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add documentation to the protocol and the initializer requirement. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,7 +259,7 @@ public struct StaticString | |
|
||
extension StaticString { | ||
public var customMirror: Mirror { | ||
return Mirror(reflecting: String(self)) | ||
return Mirror(reflecting: String(describing: self)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, as a strength reduction, we can directly query return Mirror(reflecting: description) |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -973,6 +973,23 @@ extension String { | |
return _nativeUnicodeUppercaseString(self) | ||
#endif | ||
} | ||
|
||
public // @testable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This initializer is just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still applicable. |
||
init<T: LosslessStringConvertible>(_ v: T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the intent of the (swift) var c: UnicodeScalar = "a"
// c : UnicodeScalar = "a"
(swift) c.description
// r0 : String = "\"a\"" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
/// | ||
|
There was a problem hiding this comment.
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 toStreamable
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
andString
are meant to support end-user uses like:Or (if it wants to go to the trouble of escaping and unescaping strings) conditional conformances like:
The conformances are trivial, but they help make code which is generic on LosslessStringConvertible more useful.
There was a problem hiding this comment.
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
refinesCustomStringConvertible
.