<div dir="ltr"><div class="gmail_extra"><div><div class="gmail_signature"><div dir="ltr"><div>On Fri, Mar 10, 2017 at 4:44 PM, Brent Royal-Gordon via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">&gt; On Mar 10, 2017, at 8:49 AM, Joe Groff &lt;<a href="mailto:jgroff@apple.com">jgroff@apple.com</a>&gt; wrote:<br>
&gt;<br>
&gt; I think there&#39;s a more powerful alternative design you should also consider. If the protocol looked like this:<br>
&gt;<br>
&gt; protocol ExpressibleByStringInterpolati<wbr>on: ExpressibleByStringLiteral {<br>
&gt;   associatedtype LiteralSegment: ExpressibleByStringLiteral<br>
&gt;   associatedtype InterpolatedSegment<br>
&gt;   init(forStringInterpolation: Void)<br>
&gt;<br>
&gt;   mutating func append(literalSegment: LiteralSegment)<br>
&gt;   mutating func append(interpolatedSegment: InterpolatedSegment)<br>
&gt; }<br>
&gt;<br>
&gt; Then an interpolation expression like this in `Thingy` type context:<br>
&gt;<br>
&gt; &quot;foo \(bar) bas \(zim: 1, zang: 2)\n&quot;<br>
&gt;<br>
&gt; could desugar to something like:<br>
&gt;<br>
&gt; {<br>
&gt;   var x = Thingy(forStringInterpolation: ())<br>
&gt;   // Literal segments get appended using append(literalSegment: &quot;literal&quot;)<br>
&gt;   x.append(literalSegment: &quot;foo &quot;)<br>
&gt;   // \(...) segments are arguments to a InterpolatedSegment constructor<br>
&gt;   x.append(interpolatedSegment: Thingy.InterpolatedSegment(<wbr>bar))<br>
&gt;   x.append(literalSegment: &quot; bas &quot;)<br>
&gt;   x.append(interpolatedSegment: Thingy.InterpolatedSegment(<wbr>zim: 1, zang: 2))<br>
&gt;<br>
&gt;   return x<br>
&gt; }()<br>
&gt;<br>
&gt; This design should be more efficient, since there&#39;s no temporary array of segments that needs to be formed for a variadic argument, you don&#39;t need to homogenize everything to Self type up front, and the string can be built up in-place. It also provides means to address problems 3 and 4, since the InterpolatedSegment associated type can control what types it&#39;s initializable from, and can provide initializers with additional arguments for formatting or other purposes.<br>
<br>
</span>On the other hand, you end up with an `init(forStringInterpolation: ())` initializer which is explicitly intended to return an incompletely initialized instance. I don&#39;t enjoy imagining this. For instance, you might find yourself having to change certain properties from `let` to `var` so that the `append` methods can operate.<br>
<br>
If we *do* go this direction, though, I might suggest a slightly different design which uses fewer calls and makes the finalization explicit:<br>
<br>
        protocol ExpressibleByStringLiteral {<br>
                associatedtype StringLiteralSegment: ExpressibleByStringLiteral<br>
<br>
                init(startingStringLiteral: ())<br>
                func endStringLiteral(with segment: StringLiteralSegment)<br>
        }<br>
        protocol ExpressibleByStringInterpolati<wbr>on: ExpressibleByStringLiteral {<br>
                associatedtype StringInterpolationSegment<br>
<br>
                func continueStringLiteral(with literal: StringLiteralSegment, followedBy interpolation: StringInterpolationSegment)<br>
        }<br>
<br>
Your `&quot;foo \(bar) bas \(zim: 1, zang: 2)\n&quot;` example would then become:<br>
<br>
        {<br>
                var x = Thingy(startingStringLiteral: ())<br>
                x.continueStringLiteral(with: &quot;Foo &quot;, followedBy: .init(bar))<br>
                x.continueStringLiteral(with: &quot; bas &quot;, followedBy: .init(zim: 1, zang: 2))<br>
                x.endStringLiteral(with: &quot;\n&quot;)<br>
                return x<br>
        }<br>
<br>
While a plain old string literal would have a more complicated pattern than they do currently, but one which would have completely compatible semantics with an interpolated string:<br>
<br>
        {<br>
                var x = Thingy(startingStringLiteral: ())<br>
                x.endStringLiteral(with: &quot;Hello, world!&quot;)<br>
                return x<br>
        }<br>
<br>
* * *<br>
<br>
Another possible design would separate the intermediate type from the final one. For instance, suppose we had:<br>
<br>
        protocol ExpressibleByStringInterpolati<wbr>on: ExpressibleByStringLiteral {<br>
                associatedtype StringInterpolationBuffer = Self<br>
                associatedtype StringInterpolationType<br>
<br>
                static func makeStringLiteralBuffer(<wbr>startingWith firstLiteralSegment: StringLiteralType) -&gt; StringLiteralBuffer<br>
                static func appendInterpolationSegment(_ expr: StringInterpolationType, to stringLiteralBuffer: inout StringLiteralBuffer)<br>
                static func appendLiteralSegment(_ string: StringLiteralType, to stringLiteralBuffer: inout StringLiteralBuffer)<br>
<br>
                init(stringInterpolation buffer: StringInterpolationBuffer)<br>
        }<br>
        // Automatically provide a parent protocol conformance<br>
        extension ExpressibleByStringInterpolati<wbr>on {<br>
                init(stringLiteral: StringLiteralType) {<br>
                        let buffer = Self.makeStringLiteralBuffer(<wbr>startingWith: stringLiteral)<br>
                        self.init(stringInterpolation: buffer)<br>
                }<br>
        }<br>
<br>
Then your example would be:<br>
<br>
        {<br>
                var buffer = Thingy.<wbr>makeStringLiteralBuffer(<wbr>startingWith: &quot;foo &quot;)<br>
                Thingy.<wbr>appendInterpolationSegment(<wbr>Thingy.<wbr>StringInterpolationSegment(<wbr>bar), to: &amp;buffer)<br>
                Thingy.appendLiteralSegment(&quot; bas &quot;, to: &amp;buffer)<br>
                Thingy.<wbr>appendInterpolationSegment(<wbr>Thingy.<wbr>StringInterpolationSegment(<wbr>zim: 1, zang: 2), to: &amp;buffer)<br>
                Thingy.appendLiteralSegment(&quot;\<wbr>n&quot;, to: &amp;buffer)<br>
<br>
                return Thingy(stringInterpolation: x)<br>
        }()<br>
<br>
For arbitrary string types, `StringInterpolationBuffer` would probably be `Self`, but if you had something which could only create an instance of itself once the entire literal was gathered together, it could use `String` or `Array` or whatever else it wanted.<br>
<br>
* * *<br>
<br>
One more design possibility. Would it make sense to handle all the segments in a single initializer call, instead of having one call for each segment, plus a big call at the end? Suppose we did this:<br>
<br>
        protocol ExpressibleByStringInterpolati<wbr>on: ExpressibleByStringLiteral {<br>
                associatedtype StringInterpolationType<br>
<br>
                init(stringInterpolation segments: StringInterpolationSegment...)<br>
        }<br>
        @fixed_layout enum StringInterpolationSegment&lt;<wbr>StringType: ExpressibleByStringInterpolati<wbr>on&gt; {<br>
                case literal(StringType.<wbr>StringLiteralType)<br>
                case interpolation(StringType.<wbr>StringInterpolationType)<br>
        }<br>
        extension ExpressibleByStringInterpolati<wbr>on {<br>
                typealias StringInterpolationSegment = Swift.<wbr>StringInterpolationSegment&lt;<wbr>Self&gt;<br>
<br>
                init(stringLiteral: StringLiteralType) {<br>
                        self.init(stringInterpolation: .literal(stringLiteral))<br>
                }<br>
        }<br>
<br>
Code pattern would look like this:<br>
<br>
        Thingy(stringInterpolation:<br>
                .literal(&quot;Foo &quot;),<br>
                .interpolation(.init(bar)),<br>
                .literal(&quot; bas &quot;),<br>
                .interpolation(.init(zim: 1, zang: 2)),<br>
                .literal(&quot;\n&quot;)<br>
        )<br>
<br>
I suppose it just depends on whether the array or the extra calls are more costly. (Well, it also depends on whether we want to be expanding single expressions into big, complex, multi-statement messes like we discussed before.)<br>
<br>
(Actually, I realized after writing this that you mentioned a similar design downthread. Oops.)<br></blockquote><div><br></div><div>This seems friendlier to me than the other designs. Not that it&#39;s a huge deal, but passing the segments one at a time introduces an unnecessary requirement that the construction happen left-to-right.</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">
<br>
* * *<br>
<br>
As for validation, which is mentioned downthread: I think we will really want plain old string literal validation to happen at compile time. Doing that in a general way means macros, so that&#39;s just not in the cards yet.<br>
<br>
However, once we *do* have that, I think we can actually handle runtime-failable interpolated literals pretty easily. For this example, I&#39;ll assume we adopt the `StringInterpolationSegment`-<wbr>enum-based option, but any of them could be adapted in the same way:<br>
<br>
        protocol ExpressibleByFailableStringInt<wbr>erpolation: ExpressibleByStringLiteral {<br>
                associatedtype StringInterpolationType<br>
<br>
                init(stringInterpolation: StringInterpolationSegment...)<br>
        }<br>
        extension ExpressibleByFailableStringInt<wbr>erpolation {<br>
                typealias StringInterpolationSegment = Swift.<wbr>StringInterpolationSegment&lt;<wbr>Self?&gt;<br>
<br>
                init(stringLiteral: StringLiteralType) {<br>
                        self.init(stringInterpolation segments: .literal(stringLiteral))<br>
                }<br>
        }<br>
        extension Optional: ExpressibleByStringInterpolati<wbr>on where Wrapped: ExpressibleByFailableStringInt<wbr>erpolation {<br>
                typealias StringLiteralType = Wrapped.StringLiteralType<br>
                typealias StringInterpolationType = Wrapped.<wbr>StringInterpolationType<br>
<br>
                init(stringInterpolation segments: StringInterpolationSegment...) {<br>
                        self = Wrapped(stringInterpolation: segments)<br>
                }<br>
        }<br>
<br>
If we think we&#39;d rather support throwing inits instead of failable inits, that could be supported directly by `<wbr>ExpressibleByStringInterpolati<wbr>on` if we get throwing types and support `Never` as a &quot;doesn&#39;t throw&quot; type.<br></blockquote><div><br></div><div>I&#39;m confused by this example — was ExpressibleByFailableStringInterpolation&#39;s init() supposed to be failable here?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
* * *<br>
<br>
Related question: Can the construction of the variadic parameter array be optimized? For instance, the arity of any given call site is known at compile time; can the array buffer be allocated on the stack and somehow marked so that attempting to retain it (while copying the `Array` instance) will copy it into the heap? (Are we doing that already?) I suspect that would make variadic calls a lot cheaper, perhaps enough so that we just don&#39;t need to worry about this problem at all.<br>
<div class="gmail-HOEnZb"><div class="gmail-h5"><br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</div></div></blockquote></div><br></div><div class="gmail_extra">Just to throw out another idea, what about keeping the entirety of the string in one contiguous block and providing String.Indexes to the initializer?</div><div class="gmail_extra"><br></div><div class="gmail_extra">protocol ExpressibleByStringInterpolation {</div><div class="gmail_extra">    associatedtype Interpolation</div><div class="gmail_extra">    init(_ string: String, with interpolations: (String.Index, Interpolation)...)</div><div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">On the other hand, unless I&#39;ve missed something, it seems like most of the suggestions so far are assuming that for any ExpressibleByStringInterpolation type, the interpolated values&#39; types will be homogeneous. In the hypothetical printf-replacement case, you&#39;d really want the value types to depend on the format specifiers, so that a Float couldn&#39;t be passed to %d without explicitly converting to an integer type.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Although I suppose that could simply be achieved with a typealias Interpolation = enum { case f(Float), d(Int), ... }</div></div>