[swift-evolution] [Proposal draft] Disallow Optionals in String Interpolation Segments

Kevin Ballard kevin at sb.org
Mon Oct 3 16:41:39 CDT 2016


On Mon, Oct 3, 2016, at 10:52 AM, Harlan Haskins via swift-evolution wrote:
> Swift developers frequently use string interpolation as a convenient,
> concise syntax for interweaving variable values with strings. The
> interpolation machinery, however, has surprising behavior in one
> specific case: Optional<T>. If a user puts an optional value into a
> string interpolation segment, it will insert either
> "Optional("value")" or "nil" in the resulting string. Neither of these
> is particularly desirable, so we propose a warning and fix-it to
> surface solutions to these potential mistakes.

Is there any way we could instead allow Optionals but just print them
the way we print ImplicitlyUnwrappedOptionals? That's almost always how
I want my Optionals to work when interpolating. To be specific, this
means for .some(x) we just print x, and for .none we print "nil".

-Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161003/647ad22d/attachment.html>


More information about the swift-evolution mailing list