[swift-evolution] multi-line string literals.

Charlie Monroe charlie at charliemonroe.net
Mon Apr 3 14:32:13 CDT 2017


> On Apr 3, 2017, at 9:25 PM, Ben Rimmington via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On 3 Apr 2017, at 17:55, Tony Allevato wrote:
>> 
>> I just checked with -O and without and was surprised to find that `let x = "abc" + "def" + "ghi"` wasn't collapsed into a single string literal "abcdefghi" in the generated assembly code. Maybe it's more difficult than it is in some other languages because of operator overloads and different kinds of text literals (strings, extended grapheme clusters, Unicode scalars)?
> 
> Is this a regression since Swift 2.0 added the optimization?

I'd say it's a regression since 3.0 since I remember testing the optimizer even being able to put together this during compile time:

struct URLString {
	let urlString: String

	init(host: String, path: String, query: String) {
		self.urlString = "http://" + host + path + "?" + query
	}
}

URLString(host: "apple.com <http://apple.com/>", path: "/mac", query: "target=imac")

This produced a single string literal - I confirmed this using MachOView on the final binary...

> 
> 	* Concatenation of Swift string literals, including across multiple lines, is
> 	  now a guaranteed compile-time optimization, even at `-Onone`. **(19125926)**
> 
> <https://github.com/apple/swift/blame/97db3931f2c5a21ea87ad6e71cdecbec325bff91/CHANGELOG.md#L1329-L1330>
> 
> -- Ben
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170403/a46366c8/attachment.html>


More information about the swift-evolution mailing list