<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>clang &#8211; Wade Tregaskis</title>
	<atom:link href="https://wadetregaskis.com/tags/clang/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com</link>
	<description></description>
	<lastBuildDate>Mon, 01 Jan 2024 23:00:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wadetregaskis.com/wp-content/uploads/2016/03/Stitch-512x512-1-256x256.png</url>
	<title>clang &#8211; Wade Tregaskis</title>
	<link>https://wadetregaskis.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">226351702</site>	<item>
		<title>#if DEBUG in Swift</title>
		<link>https://wadetregaskis.com/if-debug-in-swift/</link>
					<comments>https://wadetregaskis.com/if-debug-in-swift/#comments</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sat, 14 Jan 2017 21:15:33 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Broken by design]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[Sad]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[Undocumented]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=3853</guid>

					<description><![CDATA[Sigh. The Swift team give an impeccable impression of a group of people who&#8217;ve never actually tried to use Swift. An incredibly basic compiler task is to provide code a way to distinguish between debug &#38; release builds, in order that it can&#160;behave accordingly (e.g. change the default logging verbosity, change asserts from fatal to&#8230; <a class="read-more-link" href="https://wadetregaskis.com/if-debug-in-swift/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Sigh.</p>



<p>The Swift team give an impeccable impression of a group of people who&#8217;ve never actually tried to use Swift.</p>



<p>An incredibly basic compiler task is to provide code a way to distinguish between debug &amp; release builds, in order that it can&nbsp;behave accordingly (e.g. change the default logging verbosity, change asserts from fatal to non-fatal, etc).</p>



<p>Long story short there is no way to do this that works correctly with <code>swift build</code>.</p>



<p>You can make it work with Xcode <em>only</em>&nbsp;by way of a simple workaround &#8211; you manually define a custom Swift flag in your target&#8217;s settings (<a href="https://stackoverflow.com/questions/24003291/ifdef-replacement-in-the-swift-language/36502874#36502874" data-wpel-link="external" target="_blank" rel="external noopener">here&#8217;s one</a> of a bajillion explanations of how do this). &nbsp;You end up with basically identical code to other C-family languages, e.g.:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">#</span><span style="color: #AF00DB">if</span><span style="color: #0000FF"> DEBUG</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #0000FF">let</span><span style="color: #000000"> logVerbosity = </span><span style="color: #098658">1</span></span>
<span class="line"><span style="color: #0000FF">#</span><span style="color: #AF00DB">else</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #0000FF">let</span><span style="color: #000000"> logVerbosity = </span><span style="color: #098658">0</span></span>
<span class="line"><span style="color: #0000FF">#</span><span style="color: #AF00DB">endif</span></span></code></pre></div>



<p>But there is no way, when using <code>swift build</code>, to specify custom Swift flags in your package config.</p>



<p>You can specify them manually with every single <code>swift build</code> invocation, e.g.:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><code>swift build -c debug -Xswiftc '-DDEBUG'</code></p>
</blockquote>



<p>But now you have extra work and the possibility of screwing it up (e.g. omitting the flag, or mismatching it to your actual build style).</p>



<p>The closest you can get is to use some undocumented, hidden Swift internal library functions:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">_isDebugAssertConfiguration</span><span style="color: #000000">() -&gt; </span><span style="color: #267F99">Bool</span></span>
<span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">_isFastAssertConfiguration</span><span style="color: #000000">() -&gt; </span><span style="color: #267F99">Bool</span></span></code></pre></div>



<p>These are defined in <a href="https://github.com/apple/swift/blob/adc54c8a4d13fbebfeb68244bac401ef2528d6d0/stdlib/public/core/AssertCommon.swift" data-wpel-link="external" target="_blank" rel="external noopener">swift/stdlib/public/core/AssertCommon.swift</a>.</p>



<p>Only the first is likely to be useful. &nbsp;The second applies in the case where you&#8217;re building not just for release but&nbsp;<em>unchecked</em> (<code>-Ounchecked</code>). &nbsp;If you just want to conditionalise on release builds generally, you have to do <code>!_isDebugAssertConfiguration()</code>.</p>



<p>The additional problem with this approach is that these are then, in your code,&nbsp;<em>runtime</em> checks. &nbsp;And the compiler then thinks it&#8217;s being helpful by pointing out that some of your code that uses them is unreachable.</p>



<p>And of course Swift has absolutely no way to silence compiler warnings, or otherwise tell the compiler not to trust its reachability checks.</p>



<p>Sigh.</p>



<h2 class="wp-block-heading">Update (February 2018)</h2>



<p>While the above method &#8211; using the <code>_isDebugAssertConfiguration()</code> method &#8211; does still work at time of writing, in Swift 4.1, there are some marginally better ways now available. &nbsp;There&#8217;s still not a&nbsp;<em>proper</em> solution, infuriatingly, but with some creativity, as you&#8217;ll see momentarily, you can get pretty close to expected functionality.</p>



<h3 class="wp-block-heading">First alternative</h3>



<p>This will only suit some cases, but its relative cleanliness &amp; simplicity makes it appealing when it is an option.</p>



<p>You can use&nbsp;<code>#if targetEnvironment(simulator)</code>. &nbsp;This of course only distinguishes between the simulator and a real iDevice environment, though that can often be useful too, perhaps orthogonally to DEBUG vs RELEASE concepts.</p>



<h3 class="wp-block-heading">Second alternative</h3>



<p>Wrap all uses of <code>_isDebugAssertConfiguration()</code> inside a minimal set of functions. &nbsp;All this gets you is a reduced (and fixed) number of unreachable code warnings, though.</p>



<p>For example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">inDebugBuilds</span><span style="color: #000000">(</span><span style="color: #795E26">_</span><span style="color: #000000"> </span><span style="color: #001080">code</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">) {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #AF00DB">if</span><span style="color: #000000"> </span><span style="color: #795E26">_isDebugAssertConfiguration</span><span style="color: #000000">() {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">code</span><span style="color: #000000">()</span></span>
<span class="line"><span style="color: #000000">    }</span></span>
<span class="line"><span style="color: #000000">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">inReleaseBuilds</span><span style="color: #000000">(</span><span style="color: #795E26">_</span><span style="color: #000000"> </span><span style="color: #001080">code</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">) {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #AF00DB">if</span><span style="color: #000000"> !</span><span style="color: #795E26">_isDebugAssertConfiguration</span><span style="color: #000000">() {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">code</span><span style="color: #000000">()</span></span>
<span class="line"><span style="color: #000000">    }</span></span>
<span class="line"><span style="color: #000000">}</span></span></code></pre></div>



<p>You can then use these in a fairly streamlined way:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #000000">inDebugBuilds {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">print</span><span style="color: #000000">(</span><span style="color: #A31515">&quot;Hello, I only greet in debug builds!&quot;</span><span style="color: #000000">)</span></span>
<span class="line"><span style="color: #000000">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #000000">inReleaseBuilds {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">print</span><span style="color: #000000">(</span><span style="color: #A31515">&quot;While I only greet in release builds - aloha!&quot;</span><span style="color: #000000">)</span></span>
<span class="line"><span style="color: #000000">}</span></span></code></pre></div>



<h3 class="wp-block-heading">Third alternative</h3>



<p>It&#8217;s possible to do one better than the above, and eliminate those unreachable code warnings entirely. &nbsp;Plus, doing so actually removes any use of unofficial APIs… but it requires abusing the official API a bit.</p>



<p>The built-in <code>assert()</code> method is implemented atop&nbsp;<code>_isDebugAssertConfiguration()</code> just the same as <code>inDebugBuilds()</code> is, above. &nbsp;However, because it&#8217;s part of the Swift standard library, <em>you</em> don&#8217;t have to be concerned about its implementation, its use of private / undocumented language, compiler, or library features &#8211; that&#8217;s up to the Swift compiler team. &nbsp;Most importantly, use of it in your code doesn&#8217;t emit an unreachable code warning.</p>



<p>So we can do something like:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">inDebugBuilds</span><span style="color: #000000">(</span><span style="color: #795E26">_</span><span style="color: #000000"> </span><span style="color: #001080">code</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">) {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">assert</span><span style="color: #000000">({ </span><span style="color: #795E26">code</span><span style="color: #000000">(); </span><span style="color: #AF00DB">return</span><span style="color: #000000"> </span><span style="color: #0000FF">true</span><span style="color: #000000"> }())</span></span>
<span class="line"><span style="color: #000000">}</span></span></code></pre></div>



<p>Ugly and obtuse, but functional and reasonably expected to work in all future versions of Swift.</p>



<p>You can similarly create a variant for release-build-only code, though it&#8217;s even hackier:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">inReleaseBuilds</span><span style="color: #000000">(</span><span style="color: #795E26">_</span><span style="color: #000000"> </span><span style="color: #001080">code</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">) {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #0000FF">var</span><span style="color: #000000"> skip: </span><span style="color: #267F99">Bool</span><span style="color: #000000"> = </span><span style="color: #0000FF">false</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">assert</span><span style="color: #000000">({ skip = </span><span style="color: #0000FF">true</span><span style="color: #000000">; </span><span style="color: #AF00DB">return</span><span style="color: #000000"> </span><span style="color: #0000FF">true</span><span style="color: #000000"> }())</span></span>
<span class="line"></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #AF00DB">if</span><span style="color: #000000"> !skip {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">code</span><span style="color: #000000">()</span></span>
<span class="line"><span style="color: #000000">    }</span></span>
<span class="line"><span style="color: #000000">}</span></span></code></pre></div>



<p>Icky, but it works. &nbsp;On the upside, you only have to define this ugliness in one place in your module, and then you can try to forget about its implementation.&nbsp;😝</p>



<p>One caveat is that I don&#8217;t know if the above approach will properly strip out the unreachable code from your built binaries.</p>



<p>Another caveat with these is that since you&#8217;re invoking a function, you can&#8217;t do a natural <code>if … else …</code> pattern &#8211; instead you need explicit, distinct <code>inDebugBuilds</code> &amp; <code>inReleaseBuilds</code> blocks. &nbsp;So it can&#8217;t be completely like the vanilla <code>#if DEBUG … #else …</code> that C-family languages have had since before the dinosaurs. &nbsp;You could create versions that take two closures as parameters &#8211; one for the affirmative case, one for the other… the trade-off is that your invocations are then a little more verbose and obviously function-cally, e.g.:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">func</span><span style="color: #000000"> </span><span style="color: #795E26">forBuildStyle</span><span style="color: #000000">(</span><span style="color: #795E26">debug</span><span style="color: #000000"> </span><span style="color: #001080">debugCode</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">,</span></span>
<span class="line"><span style="color: #000000">                    </span><span style="color: #795E26">release</span><span style="color: #000000"> </span><span style="color: #001080">releaseCode</span><span style="color: #000000">: () -&gt; </span><span style="color: #267F99">Void</span><span style="color: #000000">) {</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #0000FF">var</span><span style="color: #000000"> debugBuild: </span><span style="color: #267F99">Bool</span><span style="color: #000000"> = </span><span style="color: #0000FF">false</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">assert</span><span style="color: #000000">({ debugBuild = </span><span style="color: #0000FF">true</span><span style="color: #000000">; </span><span style="color: #AF00DB">return</span><span style="color: #000000"> </span><span style="color: #0000FF">true</span><span style="color: #000000"> }())</span></span>
<span class="line"></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #AF00DB">if</span><span style="color: #000000"> debugBuild {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">debugCode</span><span style="color: #000000">()</span></span>
<span class="line"><span style="color: #000000">    } </span><span style="color: #AF00DB">else</span><span style="color: #000000"> {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">releaseCode</span><span style="color: #000000">()</span></span>
<span class="line"><span style="color: #000000">    }</span></span>
<span class="line"><span style="color: #000000">}</span></span>
<span class="line"></span>
<span class="line"><span style="color: #795E26">forBuildStyle</span><span style="color: #000000">(</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">debug</span><span style="color: #000000">: {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">print</span><span style="color: #000000">(</span><span style="color: #A31515">&quot;I&#39;m built for debuggin&#39;!&quot;</span><span style="color: #000000">)</span></span>
<span class="line"><span style="color: #000000">    },</span></span>
<span class="line"><span style="color: #000000">    </span><span style="color: #795E26">release</span><span style="color: #000000">: {</span></span>
<span class="line"><span style="color: #000000">        </span><span style="color: #795E26">print</span><span style="color: #000000">(</span><span style="color: #A31515">&quot;I&#39;m built for the wild!&quot;</span><span style="color: #000000">)</span></span>
<span class="line"><span style="color: #000000">    }</span></span>
<span class="line"><span style="color: #000000">)</span></span></code></pre></div>



<p>Up to your personal preferences as to which API you adopt, and which implementation you choose (streamlined but private-Swift-bits-dependent with bonus unnecessary compiler warnings, or official-APIs-only but hacky).</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/if-debug-in-swift/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3853</post-id>	</item>
		<item>
		<title>Stupid Swift error message #a bajillion and one</title>
		<link>https://wadetregaskis.com/stupid-swift-error-message-a-bajillion-and-one/</link>
					<comments>https://wadetregaskis.com/stupid-swift-error-message-a-bajillion-and-one/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sat, 07 Jan 2017 19:27:42 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[Stupid Compiler Messages]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[What do you want?]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=3841</guid>

					<description><![CDATA[Input code: Push button. &#160;Expect results (or at least bacon). &#160;Get: Foo.swift:76:21: error: expected ',' separator .day: { String($0 + 1) }, ^ , Foo.swift:76:21: error: expected expression in list of expressions .day: { String($0 + 1) }, ^ Foo.swift:76:21: error: expected ',' separator .day: { String($0 + 1) }, ^ , Believe it or&#8230; <a class="read-more-link" href="https://wadetregaskis.com/stupid-swift-error-message-a-bajillion-and-one/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Input code:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">let</span><span style="color: #000000"> componentsOfPotentialInterest = [Calendar.</span><span style="color: #001080">Component</span><span style="color: #000000">: ((</span><span style="color: #267F99">Int</span><span style="color: #000000">) -&gt; </span><span style="color: #267F99">String</span><span style="color: #000000">)](</span></span>
<span class="line"><span style="color: #000000"> .</span><span style="color: #001080">day</span><span style="color: #000000">: { </span><span style="color: #267F99">String</span><span style="color: #000000">(</span><span style="color: #0000FF">$0</span><span style="color: #000000"> + </span><span style="color: #098658">1</span><span style="color: #000000">) },</span></span>
<span class="line"><span style="color: #000000">)</span></span></code></pre></div>



<p>Push button. &nbsp;Expect results (or at least bacon). &nbsp;Get:</p>



<pre class="wp-block-preformatted">Foo.swift:76:21: error: expected ',' separator
 .day: { String($0 + 1) },
     ^
     ,
Foo.swift:76:21: error: expected expression in list of expressions
 .day: { String($0 + 1) },
     ^
Foo.swift:76:21: error: expected ',' separator
 .day: { String($0 + 1) },
     ^
     ,</pre>



<p>Believe it or not, Swift, blindly&nbsp;repeating your obtuse error messages does not help.</p>



<p>What it&#8217;s&nbsp;<em>trying</em> but as usual failing miserably to tell me is that Dictionary doesn&#8217;t have an initialiser that takes key: value pairs (my mistake for writing straight-forward, Python-like code). &nbsp;You have to&nbsp;use the dictionary literal syntax instead:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #0000FF">let</span><span style="color: #000000"> componentsOfPotentialInterest: [Calendar.Component: ((</span><span style="color: #267F99">Int</span><span style="color: #000000">) -&gt; </span><span style="color: #267F99">String</span><span style="color: #000000">)] = [</span></span>
<span class="line"><span style="color: #000000"> .</span><span style="color: #001080">day</span><span style="color: #000000">: { </span><span style="color: #267F99">String</span><span style="color: #000000">(</span><span style="color: #0000FF">$0</span><span style="color: #000000"> + </span><span style="color: #098658">1</span><span style="color: #000000">) },</span></span>
<span class="line"><span style="color: #000000">]</span></span></code></pre></div>



<p>Now it merely complains about the expression being <a href="https://wadetregaskis.com/big-words-hurt-swifts-tiny-little-brain/" data-wpel-link="internal">too complex for its pathetic little brain</a>, rather than having no fucking clue what you&#8217;re doing to begin with.  Pick your utterly useless poison, I suppose.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/stupid-swift-error-message-a-bajillion-and-one/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3841</post-id>	</item>
		<item>
		<title>Big words hurt Swift&#8217;s tiny little brain</title>
		<link>https://wadetregaskis.com/big-words-hurt-swifts-tiny-little-brain/</link>
					<comments>https://wadetregaskis.com/big-words-hurt-swifts-tiny-little-brain/#comments</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sat, 07 Jan 2017 19:04:02 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[Stupid Compiler Messages]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[What do you want?]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=3838</guid>

					<description><![CDATA[Foo.swift:75:49: error: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions let componentsOfPotentialInterest = [(Calendar.Component, ((Int) -&#62; String))]( ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The fuck?]]></description>
										<content:encoded><![CDATA[<pre>Foo.swift:75:49: error: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
 <strong>let</strong> componentsOfPotentialInterest = [(Calendar.Component, ((Int) -&gt; String))](
                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</pre>
<p>The fuck?</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/big-words-hurt-swifts-tiny-little-brain/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3838</post-id>	</item>
		<item>
		<title>ambiguous reference to member &#8216;joined()&#8217;</title>
		<link>https://wadetregaskis.com/ambiguous-reference-to-member-joined/</link>
					<comments>https://wadetregaskis.com/ambiguous-reference-to-member-joined/#comments</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Sat, 17 Dec 2016 19:23:51 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[Stupid Compiler Messages]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[What do you want?]]></category>
		<guid isPermaLink="false">https://blog.wadetregaskis.com/?p=3807</guid>

					<description><![CDATA[You can readily tell that Swift was created by a C++ fanatic, by its fucking obtuse error messages. ⤹ Me &#160; &#160; &#160;&#160;&#160; &#160; &#160; Swift compiler ⤵︎ In today&#8217;s episode of &#8220;what the fuck do you want, compiler?&#8221;, we tackle: foo.swift:186:39: error: ambiguous reference to member 'joined()' log.debug("\(thingies.joined(separator: ",&#160;"))") ^~~~~~~~ Swift.BidirectionalCollection:27:17: note: found this&#8230; <a class="read-more-link" href="https://wadetregaskis.com/ambiguous-reference-to-member-joined/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>You can readily tell that Swift was created by a C++ fanatic, by its fucking obtuse error messages.</p>



<p class="has-text-align-center">⤹ Me &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; Swift compiler ⤵︎</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="459" height="195" src="https://wadetregaskis.com/wp-content/uploads/2016/12/What-Do-You-Want-Rachel-McAdams-Ryan-Gosling-In-The-Notebook-Gif.webp" alt="&quot;What do you want&quot; scene from The Notebook" class="wp-image-3810" srcset="https://wadetregaskis.com/wp-content/uploads/2016/12/What-Do-You-Want-Rachel-McAdams-Ryan-Gosling-In-The-Notebook-Gif.webp 459w, https://wadetregaskis.com/wp-content/uploads/2016/12/What-Do-You-Want-Rachel-McAdams-Ryan-Gosling-In-The-Notebook-Gif-256x109.webp 256w" sizes="(max-width: 459px) 100vw, 459px" /></figure>
</div>


<p>In today&#8217;s episode of &#8220;what the fuck do you want, compiler?&#8221;, we tackle:</p>



<pre class="wp-block-preformatted">foo.swift:186:39: error: ambiguous reference to member 'joined()'
       log.debug("\(thingies.joined(separator: ",&nbsp;"))")
                    ^~~~~~~~
Swift.BidirectionalCollection:27:17: note: found this candidate
 public func joined() -&gt; FlattenBidirectionalCollection&lt;Self&gt;
             ^
Swift.Sequence:27:17: note: found this candidate
 public func joined() -&gt; FlattenSequence&lt;Self&gt;
             ^
Swift.Sequence:18:17: note: found this candidate
 public func joined&lt;Separator : Sequence where Separator.Iterator.Element == Iterator.Element.Iterator.Element&gt;(separator: Separator) -&gt; JoinedSequence&lt;Self&gt;
             ^
Swift.Sequence:16:17: note: found this candidate
 public func joined(separator: String = default) -&gt; String
             ^
Swift.Collection:27:17: note: found this candidate
 public func joined() -&gt; FlattenCollection&lt;Self&gt;
             ^</pre>



<p>For context, &#8216;thingies&#8217; is an array of a custom type.</p>



<p>What the compiler wishes it could say, if it weren&#8217;t incompetent, is that every one of <code>Array</code>&#8216;s <code>joined</code> implementations are&nbsp;<em>conditional</em>. &nbsp;The one that I want is is the second last one, but it is only defined for <code>Array&lt;String&gt;</code> specifically. &nbsp;No other <code>Array</code> types.</p>



<p>Similarly&nbsp;<em>every other one</em> is conditional on the <code>Element</code> type within the <code>Array</code> being a specific type or protocol, none of which happen to apply to the types I&#8217;m using in my <code>Array</code>.</p>



<p>Now, my&nbsp;<em>intuition</em> is that since my type&nbsp;<em>is</em> <code>CustomDebugStringConvertible</code>, that Swift would know then how to convert my type to a <code>String</code> and then go from there. &nbsp;For better or worse, however, it does not. &nbsp;Instead you have to do it manually, e.g.:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki min-light" style="background-color: #ffffff" tabindex="0"><code><span class="line"><span style="color: #24292EFF">log.</span><span style="color: #6F42C1">debug</span><span style="color: #212121">(</span><span style="color: #22863A">&quot;</span><span style="color: #22863A">\(thingies.</span><span style="color: #6F42C1">map</span><span style="color: #212121">(</span><span style="color: #6F42C1">{ </span><span style="color: #1976D2">String</span><span style="color: #212121">(</span><span style="color: #6F42C1">describing</span><span style="color: #212121">:</span><span style="color: #6F42C1"> $0</span><span style="color: #212121">)</span><span style="color: #6F42C1"> }</span><span style="color: #212121">)</span><span style="color: #22863A">.</span><span style="color: #6F42C1">joined</span><span style="color: #212121">(</span><span style="color: #6F42C1">separator</span><span style="color: #212121">:</span><span style="color: #6F42C1"> </span><span style="color: #22863A">&quot;, &quot;</span><span style="color: #212121">)</span><span style="color: #22863A">)</span><span style="color: #22863A">&quot;</span><span style="color: #212121">)</span></span></code></pre></div>



<p>And you can probably tell from that alone that I&#8217;m very used to Objective-C, where it&#8217;s very easy to write what you intend <em>and</em> get the results you intend.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/ambiguous-reference-to-member-joined/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			<media:content url="https://wadetregaskis.com/wp-content/uploads/2016/12/What-Do-You-Want-Rachel-McAdams-Ryan-Gosling-In-The-Notebook-Gif.webp" medium="image" />
<post-id xmlns="com-wordpress:feed-additions:1">3807</post-id>	</item>
		<item>
		<title>Collection literals</title>
		<link>https://wadetregaskis.com/collection-literals/</link>
					<comments>https://wadetregaskis.com/collection-literals/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Wed, 14 Mar 2012 05:54:06 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[clang]]></category>
		<category><![CDATA[literals]]></category>
		<category><![CDATA[LLVM]]></category>
		<guid isPermaLink="false">http://blog.wadetregaskis.com/?p=2364</guid>

					<description><![CDATA[Last week Ted Kremenek added support for container literals and subscripting to Clang. This was noted in various places, though mostly only as a statement and maybe an example of the new container literals. But I wanted to know how they&#8217;re implemented. The answer is pretty easy to find. The relevant code follows a similar&#8230; <a class="read-more-link" href="https://wadetregaskis.com/collection-literals/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Last week <a href="https://github.com/llvm/llvm-project/commit/e65b086e07a6345d8eb85bf84f3899368c233348" data-wpel-link="external" target="_blank" rel="external noopener">Ted Kremenek added support for container literals and subscripting</a> to Clang. This was noted in various places, though mostly only as a statement and maybe an example of the new container literals. But I wanted to know how they&#8217;re implemented.</p>



<p>The answer is pretty easy to find. The relevant code follows a similar path within Clang as the existing <code>NSString</code> literals, which is to say that the guts of it live in <a href="https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CGObjC.cpp" data-wpel-link="external" target="_blank" rel="external noopener">CGObjC.cpp</a>. You&#8217;ll now find, in addition to the familiar <code>EmitObjCStringLiteral()</code>:</p>



<ul class="wp-block-list">
<li><code>EmitObjCNumericLiteral()</code></li>



<li><code>EmitObjCArrayLiteral()</code></li>



<li><code>EmitObjCDictionaryLiteral()</code></li>
</ul>



<p>What is unexpected is that they aren&#8217;t implemented the way you&#8217;d expect. Which is to say, the way <code>NSString</code> literals are implemented; as special, compile-time constructed instances that always exist. <code>EmitObjCStringLiteral()</code> ultimately just calls <code>GetAddrOfConstantCFString()</code> in <a href="https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp" data-wpel-link="external" target="_blank" rel="external noopener">CodeGenModule.cpp</a>, which:</p>



<ol class="wp-block-list">
<li>Checks that there&#8217;s not an existing string constant with the same value, in which case it returns the address of that.</li>



<li>Creates a global variable to be the <code>NSString</code> instance (actually an instance of whatever the constant string type is), and populates it with the class pointer, some flags indicating whether it&#8217;s UTF16 or not (in which case I presume it&#8217;s UTF8), the length, and a pointer to the actual string data&#8230;</li>



<li>Which is allocated as a separate global variable.</li>
</ol>



<p>So you end up with your actual string plus a 16-byte <code>NSString</code> instance in the read-only data section of your library. Pretty efficient at runtime.</p>



<p>Yet the new literals aren&#8217;t implemented like this at all. Numeric literals could in fact be implemented even more efficiently &#8211; <code>NSNumber</code> supports tagged pointers, which is where the actual pointer contains the entire object, rather than pointing to a heap-allocated instance. It doesn&#8217;t work for all numbers, of course, but it handles a lot of common cases, and a fallback to a compile-time constructed instance would be fine.</p>



<p>Instead, it generates a runtime call to the appropriate class constructor method on <code>NSNumber</code>. So you&#8217;re going to hit e.g. <code>+[NSNumber numberWithInt:]</code> every single time you execute a line that contains <code>@5</code>. Don&#8217;t forget that means it gets added to the autorelease pool. While <code>NSNumber</code> does keep a cache of likely-common instances (small integers near zero, for example), that&#8217;s not tuned to your particular code and it&#8217;s not going to cover many uses.</p>



<p>That&#8217;s all amazingly inefficient. I can only assume that this is the naive initial implementation, that will then be improved upon.</p>



<p>Likewise the <code>NSArray</code> and <code>NSDictionary</code> literals are also emitted as runtime calls to the appropriate class constructor method (either <code>+[NSArray arrayWithObjects:count:]</code> or <code>+[NSDictionary dictionaryWithObjects:forKeys:count:]</code>, by default).</p>



<p>For the collections at least I can see that being slightly more rational, given that the collections are significantly more complex than simple string or number literals, and those classes have been known to change their implementations at runtime depending on their capacity and other factors.</p>



<p>It&#8217;s also possible that it&#8217;s implemented this way, for now, in order to support all Objective-C runtimes, not just the Mac and iOS ones. This is in CGObjC.cpp after all, not CGObjCMac.cpp. Though it&#8217;s worth noting that string literals are handled differently between the Mac/iOS and the GNU runtimes.</p>



<p>I hope the implementation improves. Even performance aside, there&#8217;s some potential initialisation order problems that follow from invoking actual class methods to generate the literals. I don&#8217;t know if I&#8217;d shy away from using these new literals for the initialisation of static variables or constants, but I&#8217;d think twice about it.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/collection-literals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2364</post-id>	</item>
	</channel>
</rss>
