<?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/"
	>

<channel>
	<title>Orange Juice Liberation Front &#187; programming</title>
	<atom:link href="http://orangejuiceliberationfront.com/tag/programming-45/feed/" rel="self" type="application/rss+xml" />
	<link>http://orangejuiceliberationfront.com</link>
	<description>Uli&#039;s citrussy fresh thoughts on technical stuff</description>
	<lastBuildDate>Mon, 13 May 2013 20:59:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Typesafe typecasts</title>
		<link>http://orangejuiceliberationfront.com/typesafe-typecasts/</link>
		<comments>http://orangejuiceliberationfront.com/typesafe-typecasts/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 20:14:58 +0000</pubDate>
		<dc:creator>uliwitness</dc:creator>
				<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[toll free bridging]]></category>
		<category><![CDATA[typecast]]></category>

		<guid isPermaLink="false">http://blog.orangejuiceliberationfront.com/?p=9</guid>
		<description><![CDATA[This is probably obvious to everyone else out there, but since I only just realized this, I thought others may be similarly dense, so I&#8217;d like to point out a little bit of Pascal ingenuity that can be applied to a common Mac programming problem. There are many classes and structs on the Mac that &#8230;  <a href="http://orangejuiceliberationfront.com/typesafe-typecasts/">Continue reading</a>]]></description>
				<content:encoded><![CDATA[<p><img style="display: block; margin-left: auto; margin-right: auto;" title="[The minimal sample code in an Xcode window]" src="http://orangejuiceliberationfront.com/wp-content/uploads/2010/11/IMG_0358.jpg" border="0" alt="[The minimal sample code in an Xcode window]" width="480" /></p>
<p><em>This is probably obvious to everyone else out there, but since I only just realized this, I thought others may be similarly dense, so I&#8217;d like to point out a little bit of Pascal ingenuity that can be applied to a common Mac programming problem.</em></p>
<p>There are many classes and structs on the Mac that exist in duplicates. For example, CoreGraphics has <tt>CGRect</tt> to represent Quartz coordinates, while AppKit uses <tt>NSRect</tt>. They&#8217;re exactly the same in layout, but have separate names. Similarly, there are many <em>toll-free bridged</em> classes between CoreFoundation and Foundation. For example a <tt>CFURLRef</tt> can be used wherever an <tt>NSURL*</tt> is needed, as long as you typecast between the types so the compiler doesn&#8217;t whine.</p>
<p>Now, typecasts are dangerous: Assume you have an <tt>NSString*</tt> storing a file path, and now you decide to change it into an <tt>NSURL*</tt>, because that&#8217;s Apple&#8217;s new recommended type for file references. You change it in the class header, and all call sites start giving you type errors, which you fix.</p>
<p><em>All call sites?</em> Nope. In one spot you were typecasting your <tt>NSString*</tt> into a <tt>CFStringRef</tt>. Now you&#8217;re typecasting an <tt>NSURL*</tt> to a <tt>CFStringRef</tt>, which is obviously wrong, but will only give you runtime errors. Ouch. So, what can you do? Well, there&#8217;s a neat trick that Pascal programmers used to use to get around Pascal&#8217;s refusal to typecast: They used a <tt>union</tt>. Unions are usually considered as type-unsafe, but in this case, they&#8217;re <em>just type-safe enough</em> for our purposes:</p>
<pre>CFStringRef  UKNSToCFString( NSString* str )
{
    union ConversionUnion
    {
        NSString*      mNSString;
        CFStringRef    mCFString;
    } myUnion;<p>    myUnion.mNSString = str;</p><p>    return myUnion.mCFString;
}</p></pre>
<p>The neat thing is that this function type-checks the input and output, but since it&#8217;s a union, it lets us set an NSString* and get a CFStringRef. Our above change would cause a compiler warning now since we&#8217;re passing an NSURL* into a function that takes an NSString*. It encodes the fact that an NSString and a CFString are the same, but <em>only those</em>.</p>
<p>Apple even does that. Look at <tt>NSRectFromCGRect()</tt> (I slightly cleaned up the code):</p>
<pre>NSRect NSRectFromCGRect( CGRect cgrect )
{
    union ConversionUnion
    {
        NSRect ns;
        CGRect cg;
    };
    return ((union ConversionUnion *)&amp;cgrect)-&gt;ns;
}</pre>
<p>They&#8217;re even a tad more efficient: Since the input parameter already is a CGRect, they just typecast it to a <tt>union ConversionUnion</tt>, saving you that one extra copy to an actual temporary local union variable. You could probably even get rid of the union completely and just typecast inside the function, as its input parameter type and return type already perfectly codify the conversion in a type-safe manner.</p>
<p>A neat trick for which I&#8217;ve created a header for a bunch of commonly bridged types and put it on <a href="http://github.com/uliwitness/UliKit/blob/master/UKTypecastMacros.h">my github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://orangejuiceliberationfront.com/typesafe-typecasts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

 Served from: orangejuiceliberationfront.com @ 2013-05-25 16:09:30 by W3 Total Cache -->