<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2011-02-18-compound-literals.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 17 Aug 2026 11:46:28 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Selvin - 2014-02-25 13:07:16</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>&lt;code&gt;
&lt;br /&gt;int *ptr;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;do {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ptr = (int []){ 42 };
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} while(0);
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSLog(@"%d", *ptr);
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Neither doesn't crash nor prints junk value in Xcode 5. It just prints 42. How could it work like this?</description><guid isPermaLink="true">c37abe2aeca264d4e8d2d7fb6ed1f16b</guid><pubDate>Tue, 25 Feb 2014 13:07:16 GMT</pubDate></item><item><title>Jean-Denis - 2012-03-29 14:41:18</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>I just discovered that the compiler accepts
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;    (CGRect){ x, y, w, h}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;instead of
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;    (CGRect){{x, y}, {w, h}}&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;So you can "flatten" the struct.
&lt;br /&gt;Hopefully they behave the same.</description><guid isPermaLink="true">e0d4092cc30604766506416c215349bf</guid><pubDate>Thu, 29 Mar 2012 14:41:18 GMT</pubDate></item><item><title>Pier-Olivier Thibault - 2011-04-29 18:18:37</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>I love how someone can give credential before lecturing someone to end up being completely wrong. I'd love to know where that leaks could come from. </description><guid isPermaLink="true">619b44389dc748407b489611c01d943e</guid><pubDate>Fri, 29 Apr 2011 18:18:37 GMT</pubDate></item><item><title>Steven Fisher - 2011-02-22 16:40:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>I don't like the &amp;amp;(int)(1), but &amp;amp;(int)(YES) is just as readable as the two step code. Slightly more so, actually, since I know even if the called function changes the value I don't care.
&lt;br /&gt;
&lt;br /&gt;(And yeah, there's no leak. Obviously.)</description><guid isPermaLink="true">e61f98384f3706bad98e5768cd0cd1ec</guid><pubDate>Tue, 22 Feb 2011 16:40:22 GMT</pubDate></item><item><title>mikeash - 2011-02-21 19:43:12</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Would &lt;code&gt;&amp;amp;(int){ YES }&lt;/code&gt; be more to your liking? I see no difference between declaring a temporary variable to hold the value and doing so for any function or method which takes an int by value. Do you declare temporary variables in all of those cases as well?
&lt;br /&gt;
&lt;br /&gt;Regarding NSError, you seem to have deeply misunderstood how the whole system works. It is the standard Cocoa convention that you can pass NULL to an NSError ** parameter if you don't care about the particulars of the error. Success or failure is still signaled by use of the return value. If the caller passel NULL, you are still supposed to indicate success or failure, and you are not supposed to fail just because you pass NULL. I have no idea why you think there would be a memory leak here. I can't even begin to correct it without knowing why you think there would be, but suffice it to say that there is not.
&lt;br /&gt;
&lt;br /&gt;No offense, but if you were interviewing me and you started going on about silent failures and memory leaks in a situation where they clearly do not apply, I would consider it a stroke of luck to not get the job.</description><guid isPermaLink="true">08a653f12a916445f307a4ab22f3599c</guid><pubDate>Mon, 21 Feb 2011 19:43:12 GMT</pubDate></item><item><title>jared - 2011-02-21 19:26:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Mike, I have deep respect for you as a developer.  
&lt;br /&gt;
&lt;br /&gt;As a potential influencer of C-writing youth, however, this post is a bull in a china shop.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &amp;amp;(int){ 1 }, sizeof(int));
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;is neither "cleaner" nor "more readable" than the two-step alternative.  The two-step example tells a maintainer what '1' means to the called function!  And sure, reuseaddr might be an easy-to-remember case, but what about o_nonblock?
&lt;br /&gt;
&lt;br /&gt;Your example with the NSError initializer creates a function that no-ops silently (and probably leads to a memory leak) instead of failing conspicuously.  If it's supposed to output an NSError but the incoming output location is NULL, you could scarcely do worse than creating a local, fake output address.
&lt;br /&gt;
&lt;br /&gt;Compound literals have a place in the world, but if I were interviewing someone and they tried to tell me how great this compound literal style is, and referenced these examples, I would consider it a mercy to yell at them before asking them to get the hell out of my office.</description><guid isPermaLink="true">6cc1f0a8c364b8ef00230e95ad0b254b</guid><pubDate>Mon, 21 Feb 2011 19:26:03 GMT</pubDate></item><item><title>Chris Ryland - 2011-02-21 17:27:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Jakob:
&lt;br /&gt;
&lt;br /&gt;Well, if I see
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[layer setFrame: (CGRect){ origin, size }];
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;then it's pretty clear that a rectangle is being constructed. If I'm not sure, then a quick click on setFrame: in Xcode will tell me.</description><guid isPermaLink="true">5f2aba0a193b7bb31997c9c7fbc2faf0</guid><pubDate>Mon, 21 Feb 2011 17:27:25 GMT</pubDate></item><item><title>mikeash - 2011-02-21 16:55:22</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>That is exactly why I wrote this post. People don't know about this syntax, despite the fact that it's useful and has been a standardized part of the language for over a decade. Now, a few more people know about it than before.
&lt;br /&gt;
&lt;br /&gt;I have seen this argument pop up before and to me it sounds like an excuse not to learn. If we can't use things that most people don't use regularly, then there is no room for change or growth.</description><guid isPermaLink="true">5b457e19552cdf9f7a9dd5de3cab7b48</guid><pubDate>Mon, 21 Feb 2011 16:55:22 GMT</pubDate></item><item><title>Jakob Egger - 2011-02-21 14:41:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>I think there's a good reason to use functions like NSMakeRect(): They make the code very readable. Compound literals are generally used very little. If I had to debug your code before reading this post, I'd have had no clue what this fancy syntax does. I wouldn't even have any idea where to look for the specific documentation that tells me about compound literals. If I see "NSMakeRect" somewhere, I can just click on it and XCode shows me the documentation.
&lt;br /&gt;
&lt;br /&gt;In an ideal world, every developer should know every detail about every language or API they use. But in practice, I keep forgetting stuff I don't use regularily.</description><guid isPermaLink="true">cbe35d5e7d8d0f5e61aa277ae09dbfe7</guid><pubDate>Mon, 21 Feb 2011 14:41:05 GMT</pubDate></item><item><title>Jordy/Jediknil - 2011-02-20 03:52:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Clearly the next step is to support this:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;error ?= &amp;amp;(NSError *){ nil };&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;But yeah, I love the field initialization syntax that Jonathan and Jared brought up.</description><guid isPermaLink="true">2d0e70fa7b895d3d77f2d3e1d873b87c</guid><pubDate>Sun, 20 Feb 2011 03:52:52 GMT</pubDate></item><item><title>mikeash - 2011-02-20 00:21:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>&lt;b&gt;Jared:&lt;/b&gt; That is nice. It should be especially nice for writing state transition tables. I have a chunk of hairy code that could be made significantly more clear using that.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Tommi:&lt;/b&gt; The subject of if statements and scopes came up on this blog in the comments to this post:
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://www.mikeash.com/pyblog/friday-qa-2010-01-15-stack-and-heap-objects-in-objective-c.html"&gt;http://www.mikeash.com/pyblog/friday-qa-2010-01-15-stack-and-heap-objects-in-objective-c.html&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;I had originally stated that the if statement had its own scope, and the comments said otherwise. However, now that I actually look it up, I believe you (and my original position) are right. From my copy of a draft C standard:
&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;A selection statement is a block whose scope is a strict subset of the scope of its enclosing block. Each associated substatement is also a block whose scope is a strict subset of the scope of the selection statement.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;The term "selection statement" covers if, if/else, and switch.
&lt;br /&gt;
&lt;br /&gt;Although it seems that gcc and clang will let you get away with this construct, to be completely safe you should probably write it using the conditional operator instead:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;error = error ? error : &amp;amp;(NSError *){ nil };&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Or using the gcc extension that lets you omit the middle:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;error = error ?: &amp;amp;(NSError *){ nil };&lt;/code&gt;</description><guid isPermaLink="true">777038098ca244f4411330a83dbd1ade</guid><pubDate>Sun, 20 Feb 2011 00:21:32 GMT</pubDate></item><item><title>Tommi - 2011-02-19 10:16:35</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Regarding scoping I was just going to ask the same thing. Comparing the assembly output between the two cases shows no differences (gcc -std=c99 -O0) so it looks to me either both create a new scope or neither one does.
&lt;br /&gt;
&lt;br /&gt;Then again, I tested with 'struct T { int i; }' rather than NSError*, maybe there's a difference there somewhere?</description><guid isPermaLink="true">2306bf6d1011e5d45ab89729d930743c</guid><pubDate>Sat, 19 Feb 2011 10:16:35 GMT</pubDate></item><item><title>Jared - 2011-02-19 08:17:41</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>Another great trick one in the vein of Jonathan's:
&lt;br /&gt;
&lt;br /&gt;int myarray[3] = { [1] = 3 }; //results in {0, 3, 0}
&lt;br /&gt;
&lt;br /&gt;beautiful when you're declaring something at file scope and using enum constants for indices, because it means no more counting the lines and then comparing that to the enum values</description><guid isPermaLink="true">fa7ed4f84dbd6cbc1cdab781a6b1ce50</guid><pubDate>Sat, 19 Feb 2011 08:17:41 GMT</pubDate></item><item><title>mikeash - 2011-02-18 21:25:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>&lt;b&gt;nevyn:&lt;/b&gt; I do read a lot, that is true. I think some of it is having a somewhat perverted mind when it comes to programming languages. I discovered the mutability of compound literals by just seeing if it worked or not, and being somewhat surprised when it did.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Johannes Fortmann:&lt;/b&gt; If statements don't actually create a new scope unless you also include explicit &lt;code&gt;{}&lt;/code&gt; after it. It's safe to take the address of compound literals (and make blocks without copying them) inside a bare if.</description><guid isPermaLink="true">ff7ae06ab1ddb3ecbb7247ff4c7a0d12</guid><pubDate>Fri, 18 Feb 2011 21:25:53 GMT</pubDate></item><item><title>Johannes Fortmann - 2011-02-18 20:55:06</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>In this example
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;if(!error)
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error = &amp;amp;(NSError *){ nil };
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;wouldn't the literal become invalid after the scope of the if clause is left, thus resulting in a dangling pointer just like in the "Scope" case above?</description><guid isPermaLink="true">3f6e3dfc9abb33a97b68bf6bcf4004b4</guid><pubDate>Fri, 18 Feb 2011 20:55:06 GMT</pubDate></item><item><title>nevyn - 2011-02-18 20:50:05</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>It's amazing how you manage to take a subject I think I know everything about, and teach me something new about it, over and over again. Every time I tell myself I should read more docs and specs :P Is that your secret to knowing every piece of programming related knowledge in the universe?
&lt;br /&gt;
&lt;br /&gt;(this time it was the mutability of the literals, that they're actually like anonymous variables. Thanks!)</description><guid isPermaLink="true">707c8c6141a5ea74430e47e96b03d153</guid><pubDate>Fri, 18 Feb 2011 20:50:05 GMT</pubDate></item><item><title>Jonathan Wight - 2011-02-18 19:29:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-02-18-compound-literals.html#comments</link><description>I think you missed my favourite trick:
&lt;br /&gt;
&lt;br /&gt;someView.frame = (CGRect){ .size = someSize };
&lt;br /&gt;
&lt;br /&gt;Any member you don't specify is zero-ed out for you.</description><guid isPermaLink="true">a58d3a9c69e9e85dcf72584a1848055f</guid><pubDate>Fri, 18 Feb 2011 19:29:58 GMT</pubDate></item></channel></rss>
