<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 04:28:11 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Clay Bridges - 2011-12-09 20:28:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>I created a hack version of this for enum, and put the code on  github (link below). I use a struct to mirror the enum, and use that to access the enum values using dot syntax. The include side would look like:
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;// your usual enum
&lt;br /&gt;typedef enum StoogeType {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeTypeMoe = 0,
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeTypeLarry = 1,
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeTypeCurly = 2
&lt;br /&gt;} StoogeType;
&lt;br /&gt;
&lt;br /&gt;// a mirroring struct
&lt;br /&gt;struct StoogeStruct {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeType moe;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeType larry;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StoogeType curly;
&lt;br /&gt;};
&lt;br /&gt;
&lt;br /&gt;// how it gets accessed
&lt;br /&gt;extern const struct StoogeStruct StoogeTypes;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;Then you could write something like 
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;StoogeType stooge = StoogeTypes.moe;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;&amp;nbsp;Right now, it seems like a lot of work for little benefit, so I doubt the practical use, but ... good to know.
&lt;br /&gt;
&lt;br /&gt;Link: &lt;a href="https://github.com/claybridges/EnumDotSyntax"&gt;https://github.com/claybridges/EnumDotSyntax&lt;/a&gt;</description><guid isPermaLink="true">93de790fa30365777f7e4580fc995f7c</guid><pubDate>Fri, 09 Dec 2011 20:28:11 GMT</pubDate></item><item><title>alexey - 2011-08-28 14:34:55</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Wrong example, your forgot that notification should have full name to differ with other and a struct doesn't help with it.
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const struct MANotifyingArrayNotifications MANotifyingArrayNotifications = {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.didAddObject = @"MANotifyingArrayDidAddObject",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.didChangeObject = @"MANotifyingArrayDidChangeObject",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.didRemoveObject = @"MANotifyingArrayDidRemoveObject",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.keys = {
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.indexChanged = @"MANotifyingArrayIndexChanged",
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.objectChanged = @"MANotifyingArrayObjectChanged"
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;};
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;And after that I think struct doesn't give any pros.</description><guid isPermaLink="true">c0fc7845a7e50fd0c91ae654653783d8</guid><pubDate>Sun, 28 Aug 2011 14:34:55 GMT</pubDate></item><item><title>Jean-Daniel - 2011-08-28 08:48:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>I like this idea, but I see a little drawback. 
&lt;br /&gt;When you add a new value in the header and you forget to add it in the implementation file, the compiler will no detect the error until runtime. If you use standard globals, the linker will tell you there is a missing symbol.
&lt;br /&gt;
&lt;br /&gt;You may expect a "missing field initialization" warning in the struct definition, but unfortunately, using the c99 syntax inhibits such warning.
&lt;br /&gt;</description><guid isPermaLink="true">817bd8895be03f264ccb668be5dc2125</guid><pubDate>Sun, 28 Aug 2011 08:48:45 GMT</pubDate></item><item><title>mikeash - 2011-08-27 02:21:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>It works fine in ARC, you just need to prefix any ObjC object members with __unsafe_unretained. Regarding GC, constant objects (the only kind you can put inside these structs) are not a problem.</description><guid isPermaLink="true">2cd6933f1ff651c71736ed8bee10fe21</guid><pubDate>Sat, 27 Aug 2011 02:21:13 GMT</pubDate></item><item><title>Sean M - 2011-08-27 00:55:42</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>objects inside structs can be a problem for GC too, depending on wether the struct is allocated in scannable memory.</description><guid isPermaLink="true">751c604ff3f0469bc86452f05b4fa172</guid><pubDate>Sat, 27 Aug 2011 00:55:42 GMT</pubDate></item><item><title>Andrey Mishanin - 2011-08-26 11:27:36</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Mike, your solution is fine except for it won't interplay nicely with ARC since you're declaring objects inside a struct.</description><guid isPermaLink="true">ed84a48b44971debde3d20e9577a025c</guid><pubDate>Fri, 26 Aug 2011 11:27:36 GMT</pubDate></item><item><title>Lane Roathe - 2011-08-23 14:28:50</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>For what it's worth, Mike's solution is, at the most basic level, nearly identical to a namespace. ie:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;    namespace MANotifyingArrayNotifications
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSString *didAddObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSString *didChangeObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MANotifyingArrayNotifications::didAddObject&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;is equivalent (more or less) to:
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;    struct MANotifyingArrayNotifications
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSString *didAddObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NSString *didChangeObject;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&lt;br /&gt;
&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MANotifyingArrayNotifications.didAddObject&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Of course, namespaces are more flexible, but are not less (or more) prone to collision (ie, in either you can name an object at level X the same as another object at level X), although the flexibility of namespaces does make it easier to contain disparate items vs using a struct (ie, no need for a function to assign the values for instance).</description><guid isPermaLink="true">86a8eb69d9d88e40cab811f6a1d9dc22</guid><pubDate>Tue, 23 Aug 2011 14:28:50 GMT</pubDate></item><item><title>mikeash - 2011-08-20 23:29:13</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>They would be a great solution if I could pull them in without getting any other C++ stuff, or if C++ were a real superset of C. :-)</description><guid isPermaLink="true">dfcdc1db6570248353350b3bb9758aa0</guid><pubDate>Sat, 20 Aug 2011 23:29:13 GMT</pubDate></item><item><title>Kentzo - 2011-08-20 22:20:12</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>I know you hate c++ Mike, but its namespaces are really good solution.</description><guid isPermaLink="true">5e41a98d5050d19e8cf2324772ca0750</guid><pubDate>Sat, 20 Aug 2011 22:20:12 GMT</pubDate></item><item><title>mikeash - 2011-08-20 17:19:04</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>I'm glad you enjoy the blog!
&lt;br /&gt;
&lt;br /&gt;Regarding namespaced functions, I completely agree that writing classes is generally going to be a better approach. Namespaced functions aren't a replacement for classes, but rather a small improvement on regular C functions when used in this context. As you've no doubt noticed, C functions aren't very commonly used in Cocoa code. Where they *are* used, this technique could come in handy, but it's not going to happen very often.
&lt;br /&gt;
&lt;br /&gt;If you wanted to go completely overboard, you could actually do a sort of namespaced class by putting a &lt;code&gt;Class&lt;/code&gt; variable in a struct and then assigning it to a real class. But, probably not a good idea there.</description><guid isPermaLink="true">32b28e2fac07950b9c7b9fdc211bd456</guid><pubDate>Sat, 20 Aug 2011 17:19:04 GMT</pubDate></item><item><title>Danny - 2011-08-20 03:16:02</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>First up, I love NSBlog. Been with ObjC for 5 months now and learned so much from this site. Thank you so very much for sharing.
&lt;br /&gt;
&lt;br /&gt;As usual, this is another excellent post! Very intriguing. Now I want to refactor all my #defines and consts with namespaced constants! :) (Honestly, NSBlog is one of my main sources where I find inspirations/motivations for refactoring my code. I learn the techniques here and apply them to my code. It's the Friday I most look forward to :)
&lt;br /&gt;
&lt;br /&gt;In regards to Namespaced Functions, I have a question for Mike and all you experts:
&lt;br /&gt;
&lt;br /&gt;Although "namespaced functions" seem cool,  I wonder if it's all that practical in the world of Cocoa. If I'm writing a project mostly in C (not taking advantage of the Cocoa framework), I can definitely see the benefit of namespaced functions. But if I'm writing an object-oriented solution, wouldn't it be better to simply write a wrapper class? Even though a class in ObjC doesn't give you the namespace notation, it offers so much more, especially when considering the principle of Separation of Concern? I guess my real question is: is there a scenario where I can use this technique (namespaced functions with struct) even in a project that's entirely composed in classes?  Please enlighten me :) Thanks!</description><guid isPermaLink="true">a284c128ddad1426834440fb286b16e3</guid><pubDate>Sat, 20 Aug 2011 03:16:02 GMT</pubDate></item><item><title>elliottcable - 2011-08-19 19:16:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Yeah, when &lt;i&gt;you&lt;/i&gt; demonstrate the idea tentatively, it gets called “really cool.”
&lt;br /&gt;
&lt;br /&gt;When I &lt;i&gt;do it in practice&lt;/i&gt;, and have been for years, I get called “insane,” “iditotic,” and other insulting intonations starting with “i.”
&lt;br /&gt;
&lt;br /&gt;But, onto the subject; here’s quite a few examples of this applied in real-life:
&lt;br /&gt;
&lt;br /&gt;My equivalent of a “header”: &lt;a href="https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L33-63"&gt;https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L33-63&lt;/a&gt;
&lt;br /&gt;The runtime initialization of the struct with the relevant functions: &lt;a href="https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L83-127"&gt;https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L83-127&lt;/a&gt;
&lt;br /&gt;Finally, the implementation of the functions themselves: &lt;a href="https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L128-229"&gt;https://github.com/elliottcable/Paws.c/blob/323628/Source/Types/fork/LL.c#L128-229&lt;/a&gt;</description><guid isPermaLink="true">308a03dc23fcb52d33896c3e8656340e</guid><pubDate>Fri, 19 Aug 2011 19:16:39 GMT</pubDate></item><item><title>mikeash - 2011-08-19 16:30:47</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>&lt;b&gt;Someone:&lt;/b&gt; I don't understand what you're getting at. Even with true namespaces, those can still collide if they have the same name too.</description><guid isPermaLink="true">c0b54ba5393455a1052b3e761737f7b5</guid><pubDate>Fri, 19 Aug 2011 16:30:47 GMT</pubDate></item><item><title>Someone - 2011-08-19 15:54:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>...but the whole point of a namespace is to prevent collisions.  While the struct idea prevents collisions within itself, you still end up naming the struct itself.  The name of the struct may collide.</description><guid isPermaLink="true">b4706e6a8266a712e2d0f50f80459275</guid><pubDate>Fri, 19 Aug 2011 15:54:21 GMT</pubDate></item><item><title>Jamie - 2011-08-19 15:51:03</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Unfortunately you still don't get the best part of namespaces, the resolution of unprefixed symbols.
&lt;br /&gt;
&lt;br /&gt;Why Apple went to all the trouble of adding KVC dot-notation when they could have added namespaces I just don't know. I'm sure they had their reasons, but on Tiger I don't recall ever saying to myself "Gah I hate having to type 'objjctForKey:' over and over!" but I DO still say to myself "Gah what should I prefix my class names with on THIS project?"</description><guid isPermaLink="true">2fde4b49dfab79f471643843e211394e</guid><pubDate>Fri, 19 Aug 2011 15:51:03 GMT</pubDate></item><item><title>Brian Mastenbrook - 2011-08-19 15:50:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>I have a &lt;code&gt;#define&lt;/code&gt; for &lt;code&gt;__attribute__ ((unused))&lt;/code&gt; that I use on static consts.</description><guid isPermaLink="true">f9bc7b96e013a8b696e1569c45af3ab5</guid><pubDate>Fri, 19 Aug 2011 15:50:52 GMT</pubDate></item><item><title>Bill - 2011-08-19 15:37:51</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>But the namespaced constants trick with structs is really cool - thanks!</description><guid isPermaLink="true">725494a04a6edabf6b74c2af535a52d7</guid><pubDate>Fri, 19 Aug 2011 15:37:51 GMT</pubDate></item><item><title>Bill - 2011-08-19 15:18:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>#define constants are only a problem in the debugger if you're stuck with Xcode. I use them quite happily in IntelliJ AppCode.</description><guid isPermaLink="true">ed3f54dce545e457ec2e1d73bff883b1</guid><pubDate>Fri, 19 Aug 2011 15:18:19 GMT</pubDate></item><item><title>mikeash - 2011-08-19 15:04:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Is there a way to use static const globals in your headers without getting warnings all over the place about them not being used in every single file where they're imported?</description><guid isPermaLink="true">d88bb1597fd38fb7b847ad4ed82482a3</guid><pubDate>Fri, 19 Aug 2011 15:04:07 GMT</pubDate></item><item><title>Brian Mastenbrook - 2011-08-19 14:46:32</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2011-08-19-namespaced-constants-and-functions.html#comments</link><description>Maybe I'm alone in this, but I tend to use static const for most of my constants. It's a good middle ground between preprocessor defines and extern const (which can't be constant folded by the compiler).
&lt;br /&gt;
&lt;br /&gt;Regarding your crazy structs idea, I think it'd be a good idea to fold this kind of thing into the language. Maybe such a language could feature both an explicit namespace feature and an enhanced form of structs with inheritance, visibility control, etc. Someone should get on that!</description><guid isPermaLink="true">b42452ee3fad6cc6a70112e443d68a76</guid><pubDate>Fri, 19 Aug 2011 14:46:32 GMT</pubDate></item></channel></rss>
