<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 24, 2016 at 12:01 PM, Charlie Monroe <span dir="ltr"><<a href="mailto:charlie@charliemonroe.net" target="_blank">charlie@charliemonroe.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>Hi Nicola, thanks for the feedback.<br>
<br>
> I assume you mean "at runtime" here?<br>
<br>
That's probably my wrong wording. :) The unexpected result is at runtime, but we'd like to catch this at compile time. I've updated it to say<br>
<br>
"in order to prevent unexpected results already at compile time" - hopefully that's a clearer wording.<br></div></div></blockquote><div><br></div><div>Aha, now I understand :-)<br><br></div><div>Perhaps "in order to detect potentially undesired behavior at compile time" would be even clearer?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>
<br>
> I think the detailed design needs some more thought. The "Uninterpolable"<br>
> protocol, and suggesting to cast "as Any" in a Fix-it both seem hacks.<br>
<br>
Originally, the proposal was simply to emit a warning for interpolation of Optionals, but several people made good points:<br>
<br>
- there may be other types you may not want to use for interpolation - as mentioned in the proposal, e.g. private data structures that would expose something you want to keep private, various enum values, etc. Which is why I've started thinking about making a protocol that would indicate the type is discouraged being "interpoled". I've thought about this and decided to make a more robust and customizable solution.<br></div></div></blockquote><div><br></div><div>I'm not sure if this is a strong enough motivation.<br>The customization point already provided by the language is CustomStringConvertible. That's the place where one can provide a readable description, hiding implementation details.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>
<br>
An alternative to this would be to use annotations or just drop this customizability completely. But I think with Swift and its protocol-driven development, marking the type with this protocol is the most robust way to go.<br>
<br>
- both .description and .debugDescription are mentioned in alternatives for the Fix-It.<br></div></div></blockquote><div><br></div><div>The direct use of .description and .debugDescription is discouraged in the API docs, so I don't think this is a viable option.<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>
<br>
"as Any" seemed, however, the cleanest and most robust solution to me, since then the Uninterpolable protocol can be applied to any type without forcing the type to conform to CustomStringConvertible as well. I agree that it's kind of a hack, though.<br>
<br>
> I'm not even sure if the general direction of a compile time warning is the<br>
> right one, and if the problem wouldn't be better solved by simply not making<br>
> Optional put "Optional()" around the value in its .description.<br>
<br>
There are many people oposing this and expecting the Optional() wrap around the value, indicating the actual type. Actually, including me - I agree it can be useful for some types of debugging since in what you wrote further, there'd be no difference between description of [1, 2, 3] (i.e. [Int]) and Optional([1, 2, 3]) (i.e. [Int]?).<br></div></div></blockquote><div><br></div><div>There's also no difference between print(Int(1)) and print(UInt(1)): they both output just "1".<br></div><div><br>I think "debugging" is the key word here. If the distinction between CustomStringConvertible and CustomDebugStringConvertible is supposed to be between providing a user-friendly description and a description useful for debugging, I think it makes sense to make the former as terse as possible and the latter as informative as possible.<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div>
<br>
There are legitimate usecases where the current behavior is correct, but in most of cases, having an optional in string interpolation will lead either to unnecessary clutter in the log/console or bugs - in which case even "nil" is not correct to be used for the interpolation. Which is the basis for this proposal.<br>
<br>
> print("\(o)") // "Optional(1)", why??<br>
<br>
String has several overloads for the init(stringInterpolationSegment:) initiailizer. Optional falls into the generic <T> category, which will call String(optional) - which most likely uses debugDescription.<br>
<br>
</div></div></blockquote></div><br></div><div class="gmail_extra">This is the relevant code:<br><a href="https://github.com/apple/swift/blob/cf73dd9177c231a15429b08ae889e94f20e53f50/stdlib/public/core/OutputStream.swift#L322-L331" target="_blank">https://github.com/apple/swift/blob/cf73dd9177c231a15429b08ae889e94f20e53f50/stdlib/public/core/OutputStream.swift#L322-L331</a><br><br></div><div class="gmail_extra">I presume that it shouldn't be a problem to change it to use CustomStringConvertible instead.<br><br></div><div class="gmail_extra">The above code also hints that there's another odd behavior in the current implementation:<br><br>struct Foo: CustomStringConvertible, CustomDebugStringConvertible {<br> var description: String { return "normal" }<br> var debugDescription: String { return "debug" }<br>}<br><br>let f = Foo()<br>let of = Optional(f)<br><br>print(f) // "normal": ok<br>debugPrint(f) // "debug": ok<br>print(of) // "Optional(debug)": unexpected<br>debugPrint(of) // "Optional(debug)": ok<br><br>--<br></div><div class="gmail_extra">Nicola<br><br></div></div>