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

Conversation

frootloops
Copy link
Contributor

Hello @gribozavr

I just started work on the proposal and if you could please help me with it. Because I don't really understand how to implement LosslessStringConvertible protocol for String.* (String.UTF8View, String.UTF16View...) and for FloatingPoint and Integer.

Thanks for any help :)

@gribozavr
Copy link
Contributor

Thank you for working on this!

I'm not actually sure if LosslessStringConvertible is implementable for String.UTF8View and String.UTF16View. The reason is that the UTF-8 and UTF-16 views can be extracted from a string at arbitrary code unit boundaries, and String.init?(_: String.UTF8View) and String.init?(_: String.UTF16View) initializers are failable for that reason.

var s = "a\u{0301}"
String(s.utf8[s.utf8.index(s.utf8.startIndex, offsetBy: 2)..<s.utf8.endIndex]) // nil

@austinzheng @brentdax

@dabrahams
Copy link
Contributor

No, it shouldn't be implemented for those types. There should be explicit separate constructors for lossless one-way conversions.

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.

@gribozavr
Copy link
Contributor

@frootloops For FloatingPoint and Integer, just change the protocols to refine LosslessStringConvertible.

For String.CharacterView and String.UnicodeScalarView just implement the new initializer by taking the requested view from the string.

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

@beccadax
Copy link
Contributor

beccadax commented Jun 29, 2016

It's quite possible that UTF8View and UTF16View can't be done. Sorry about that—guess I wasn't thinking when I added them.

All existing FloatingPoint types already conform to CustomStringConvertible and offer a failable init(_: String) initializer. In the future, we might consider providing our own conversion in an extension on FloatingPoint so that "big float" packages don't have to provide one, but there's no need for that today.

As for Integer, all existing integer types support an init?(_:radix:) initializer, but it's not formalized by SE-0104. I've sent an email about that in the review thread. If it were added to Integer, we could provide a Lossless-compatible init?(_:) that called through to it.

@@ -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)

@moiseev
Copy link
Contributor

moiseev commented Jul 18, 2016

Superseded by #3574.

@moiseev moiseev closed this Jul 18, 2016
@frootloops frootloops deleted the SE-0089 branch August 10, 2016 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants