<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">

  <title><![CDATA[Toshok Electric Light & Manufacturing]]></title>
  
  <link href="http://toshok.github.com/" />
  <updated>2012-01-21T13:27:26-08:00</updated>
  <id>http://toshok.github.com/</id>
  <author>
    <name><![CDATA[Chris Toshok]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/ToshokElectricLightManufacturing" /><feedburner:info uri="toshokelectriclightmanufacturing" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <title type="html"><![CDATA[First CoffeeKit Release!]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/wYMFIQuBxhQ/" />
    <updated>2011-12-22T21:27:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/22/first-coffeekit-release</id>
    <content type="html">&lt;p&gt;Happy Holidays!&lt;/p&gt;

&lt;p&gt;CoffeeKit 0.1.1 is &lt;a href="http://toshok.github.com/coffeekit/download"&gt;ready for download&lt;/a&gt;.  Release
notes can be found &lt;a href="http://toshok.github.com/coffeekit/release_notes/0.1.1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I honestly never thought I&amp;#8217;d get this out the door.  I&amp;#8217;ve been swamped
with other bits of life, and I was getting &lt;em&gt;really&lt;/em&gt; bogged down in the
tooling story.&lt;/p&gt;

&lt;p&gt;The tooling story is pretty depressing, as there really is no
good/easy/popular path to IDE pickup.  There are plans in the works,
but I&amp;#8217;ve decided to punt on it for now and get this in the hands of
people who might have an interest (and better, usable feedback for me
:) So while the tooling story is less than stellar at the moment, I
hope you can see past it to what I consider to be the real interesting
bits of the project - the mashup of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;iOS Bindings: UIKit, CoreAnimation, CoreGraphics, GLKit, GameKit, StoreKit, Twitter, etc.&lt;/li&gt;
&lt;li&gt;Web Infrastructure: Web Workers, XmlHttpRequest, WebGL, and Typed Arrays, so far&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;All accessible using JavaScript.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s a whole &lt;a href="http://toshok.github.com/coffeekit"&gt;CoffeeKit&lt;/a&gt; area to the site now, which
will hopefully start looking less like the default octopress theme
sometime after the holidays.&lt;/p&gt;

&lt;p&gt;Definitely download it and let me know how it goes.  If you have any
questions or problems, don&amp;#8217;t hesitate to get in touch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On IRC: I&amp;#8217;m &lt;code&gt;toshok&lt;/code&gt; on GimpNET in all the &lt;code&gt;#mono&lt;/code&gt; channels, and I hang out in &lt;code&gt;#coffeescript&lt;/code&gt; on Freenode.&lt;/li&gt;
&lt;li&gt;Twitter: either &lt;a href="http://twitter.com/toshok"&gt;&lt;code&gt;@toshok&lt;/code&gt;&lt;/a&gt; or &lt;a href="http://twitter.com/toshokelectric"&gt;&lt;code&gt;@toshokelectric&lt;/code&gt;&lt;/a&gt; works.  Or you can just add &lt;code&gt;#CoffeeKit&lt;/code&gt; to your tweets and I&amp;#8217;ll see them.&lt;/li&gt;
&lt;li&gt;Github: If you run into anything, don&amp;#8217;t hesitate to file a &lt;a href="https://github.com/toshok/coffeekit/issues"&gt;github issue&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Email: &lt;code&gt;toshok at toshokelectric.com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/wYMFIQuBxhQ" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/22/first-coffeekit-release/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[MonoTouch - more Subviews woes]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/1M_7Qjg7v04/" />
    <updated>2011-12-13T10:07:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/13/monotouch-more-subviews-woes</id>
    <content type="html">&lt;p&gt;In my &lt;a href="http://toshokelectric.com/blog/2011/12/12/monotouch-paying-the-bills/"&gt;last post&lt;/a&gt;
I explained that it is important to handle memory manually for managed
classes that correspond to large resources, and made some pretty vague
claims as to potentially large time deltas before objects were
finalized.  I&amp;#8217;ll try to clear this up a bit here.&lt;/p&gt;

&lt;!-- more --&gt;


&lt;p&gt;But first a little background.&lt;/p&gt;

&lt;p&gt;Not all managed objects are created equal in MonoTouch.  There are
basically two &lt;em&gt;classes&lt;/em&gt; (not C# classes) of objects: Those that
correspond to built-in objective-C types (things like NSString,
UIView, UIImage), and those that correspond to user-defined types
(ToshokView, MySpecialViewController).  The latter class of objects
uses a mechanism known as a &lt;em&gt;Toggle Ref&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;Toggle References&lt;/h2&gt;

&lt;p&gt;Toggle references are a handy device for dealing with language
bindings where one side of the binding is reference counted and you
want the lifecycles of both objects to be the same.  In our case the
other side is managed by a GC, but there&amp;#8217;s no requirement for a GC.
The other side could be reference counted too, for instance.&lt;/p&gt;

&lt;p&gt;As far as I can figure out about the history, it was
&lt;a href="http://mail.gnome.org/archives/gtk-devel-list/2005-April/msg00095.html"&gt;introduced&lt;/a&gt;
by Owen Taylor for inclusion in glib (although in his mail he points
out the technique is substantially older), was then used by Mike
Kestner in the mono project&amp;#8217;s &lt;a href="https://github.com/mono/gtk-sharp/blob/master/glib/ToggleRef.cs"&gt;Gtk# bindings&lt;/a&gt;
, and has since been used everywhere managed code has to interact with
a reference counted system (in Moonlight for a long time, and
MonoTouch, probably elsewhere).&lt;/p&gt;

&lt;p&gt;Toggle references allow either side (managed or unmanaged) to
effectively be the owner of the other, depending entirely on the
reference count of the unmanaged peer.&lt;/p&gt;

&lt;p&gt;The managed peer always refs the unmanaged peer in its constructor,
and unrefs the unmanaged peer in its finalizer.  So a RC of 1 is
special.  It means that the only thing referencing the unmanaged peer
is the managed peer.&lt;/p&gt;

&lt;p&gt;There are just two states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If RC == 1, the toggle ref holds a weak reference on the managed peer.&lt;/li&gt;
&lt;li&gt;If RC &gt; 1, the toggle ref holds a strong reference on the managed peer.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Rule 1 handles the managed ownership case.  There are no further
unmanaged references to the unmanaged peer, so the unmanaged side of
things shouldn&amp;#8217;t keep the managed peer alive artificially.  There may
be other references to the managed peer from roots, and so it can stay
alive that way.  As long as this managed peer stays alive, the
unmanaged one will too.&lt;/p&gt;

&lt;p&gt;Rule 2 handles the unmanaged ownership case.  If there are unmanaged
references, then we know the unmanaged peer must remain alive.  This
means that the managed peer must also, so we create (in the monotouch
case) a GCHandle to keep it alive.&lt;/p&gt;

&lt;p&gt;The switching betweek weak and strong references is where the &amp;#8220;Toggle&amp;#8221;
in Toggle Reference comes from.&lt;/p&gt;

&lt;p&gt;Toggle References provide a very easy way to bridge between reference
counting and GC&amp;#8217;ed heaps, but they do have some problems.  Cycles are
difficult to detect, as they can cross the language boundary, and
coupled with C# delegates it&amp;#8217;s very easy to create cycles that can&amp;#8217;t
easily be seen or reasoned about by the user.  This was such a problem
in Moonlight that we eventually moved away from togglerefs almost
completely, and used the GC for everything.&lt;/p&gt;

&lt;p&gt;As I said above, MonoTouch uses Toggle References only for user
defined classes.  For built-in types it simply refs in the constructor
and unrefs in the finalizer.  There is no toggling.  This is due to
something else I&amp;#8217;m going to gloss over: MonoTouch doesn&amp;#8217;t replace the
implementation of retain/release for built-in types, but it does
supply them for user generated ones.&lt;/p&gt;

&lt;h2&gt;What&amp;#8217;s this got to do with Subviews?&lt;/h2&gt;

&lt;p&gt;If you have the following view hierarchy:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class=''&gt;&lt;span class='line'&gt;1:  Builtin UIView subclass -&amp;gt; 
&lt;/span&gt;&lt;span class='line'&gt;2:    Custom UIView subclass -&amp;gt;
&lt;/span&gt;&lt;span class='line'&gt;3:      Custom UIView subclass -&amp;gt;
&lt;/span&gt;&lt;span class='line'&gt;4:        Custom UIView subclass&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;and each of the .Subviews properties for each level in the tree has
been accessed or modified using the built-in monotouch
properties/methods (not the ones listed in my previous blog post),
then it will take 4 GC passes (including draining the finalizer queue)
for all of those objects to be cleaned up.&lt;/p&gt;

&lt;p&gt;Say you clear the last managed reference to the Root UIView subclass.
The first GC pass determines that it&amp;#8217;s collectible and eventually puts
it in the finalizer queue.  Or maybe you&amp;#8217;re feeling like a good GC
citizen and know the hierarchy is gone for good, and you call
.Dispose() on the root.&lt;/p&gt;

&lt;p&gt;When its finalizer is run it will unref the unmanaged peer.  When the
unmanaged peer&amp;#8217;s RC drops to 0 it will unref all of its subviews.
This is the important bit.  Since the root UIView has a cached
reference to the Custom UIView subclass in its .Subviews cache, the RC
of the Custom UIView&amp;#8217;s unmanaged peer will be 2.  1 for the managed
peer, and one by virtue of being in the unmanaged subviews array.
Actually it&amp;#8217;ll be 3.  The first fetch of the &amp;#8220;subviews&amp;#8221; property on a
UIView creates an NSArray that refs all the elements.  This NSArray
goes away when the parent object goes away, and as far as toggle refs
go, there is no difference between retainCounts of 2 and 3.&lt;/p&gt;

&lt;p&gt;When the root view deallocs, it destroys the cached subviews array and
unrefs its child view, so it drops from RC 3 to RC 1.  Only at this
point does the toggle ref for the Custom UIView subclass on line 2
toggle to weak, allowing it to be collected when there are no further
managed references to it.&lt;/p&gt;

&lt;p&gt;The same process occurs for levels 3 and 4.  The GC and toggle refs
proceed in lockstep.  Given that there are no hard guarantees on when
an object will be collected once it is collectible, this seems like a
bad situation.&lt;/p&gt;

&lt;p&gt;The situation is a bit rosier if the entire hiearchy were
builtin-types.  Since those types lack toggle refs, there&amp;#8217;s no
artificial strong references keeping any of them alive, and the GC
will collect the entire subtree&amp;#8217;s worth of managed objects at once.
The unmanaged peers will all be released when the finalizers run.&lt;/p&gt;

&lt;p&gt;Now as to how this will affect you: If your memory usage is high, iOS
will be nice and give you a memory warning, at which point you&amp;#8217;re
supposed to do everything you can (as &lt;em&gt;quickly&lt;/em&gt; as you can) to reduce
your memory usage.&lt;/p&gt;

&lt;p&gt;Part of this work is usually cleaning up potentially deep view
hierarchies that may be obscured or offcreen - basically you want to
clean up anything you can afford to get rid of, things that you can
regenerate when you need them again.&lt;/p&gt;

&lt;p&gt;MonoTouch is nice and does a GC pass when receiving a memory warning.
The problem is that it may take several GC passes to clean things up
because of the Subview cache.&lt;/p&gt;

&lt;h2&gt;One Further Note&lt;/h2&gt;

&lt;p&gt;The above assumes the boehm collector, where a GC pass scans the
entire heap every time.  Inefficiencies of that approach aside and
modulo conservative GC random bit pattern screwups, you&amp;#8217;re guaranteed
to clear 1 Subviews level per GC+finalizer pass.&lt;/p&gt;

&lt;p&gt;With the addition of Sgen, the situation is likely to become even
worse.  Assuming that the above hierarchy lives for any length of
time, it&amp;#8217;s entirely conceivable that the hierarchy will be promoted to
the tenured generation, which is collected less frequently, thereby
increasing the time it will take to fully reclaim that memory.&lt;/p&gt;

&lt;h2&gt;No real suggestion here&lt;/h2&gt;

&lt;p&gt;Unfortunately this post isn&amp;#8217;t like the last one, where I offered real,
useful alternatives.  There aren&amp;#8217;t really any good ones here..  I&amp;#8217;ve
tried coming up with some approaches, but all of them run afoul of the
fact that the refcount is always high enough to keep a strong ref
around until the parent object is gone, so they retain the lockstep
problem.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/1M_7Qjg7v04" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/13/monotouch-more-subviews-woes/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[MonoTouch - paying the bills]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/BvGq999atK4/" />
    <updated>2011-12-12T22:01:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/12/monotouch-paying-the-bills</id>
    <content type="html">&lt;p&gt;I worked recently on reducing memory usage in a large monotouch app.  Since then I&amp;#8217;ve been looking at other ways
monotouch either makes difficult or actively hampers memory management.&lt;/p&gt;

&lt;!-- more --&gt;


&lt;p&gt;MonoTouch is garbage collected, which is fantastic.  But GC isn&amp;#8217;t a panacea, especially in a resource constrained
environment like the iphone.  Your app can be killed if you use too much memory in the foreground (and don&amp;#8217;t
respond appropriately to memory warnings), but it can also be killed in the background if it has too many dirty
pages which the OS can&amp;#8217;t swap out, and the foreground app needs the memory.&lt;/p&gt;

&lt;p&gt;You shouldn&amp;#8217;t rely on the GC when you&amp;#8217;re dealing with large resources, such as images.  You should manage that memory
yourself, and .Dispose() of it when you&amp;#8217;re done.  Otherwise you can turn a momentary memory spike into a longer
lived plateau because the GC might not finalize the C# peer (and therefore free up the unmanaged resources) for
some time.&lt;/p&gt;

&lt;p&gt;But images aren&amp;#8217;t the only things that keep large blocks of dirty memory around.  Every CALayerBackingStore is
potentially large (especially if they&amp;#8217;re full screen), and UIViews have backing stores.  Now, you may say to yourself
&amp;#8220;Self, I&amp;#8217;ll just dispose of all of my UIViews when I&amp;#8217;m done with them.&amp;#8221;  That&amp;#8217;s a great goal, but it&amp;#8217;s not very
performant. And besides, the UIViews your code is dealing with aren&amp;#8217;t the whole story.&lt;/p&gt;

&lt;h2&gt;Caching harmful?&lt;/h2&gt;

&lt;p&gt;MonoTouch keeps the managed object graph around as long as the unmanaged peers are alive.  This is due to
some state (event handlers, for instance) existing only in the managed layer.  So, if a C# peer for a view is created,
it should live as long as the unmanaged peer does.  Indeed, there are a whole class of bugs/problems that new
monotouch users run into where they aren&amp;#8217;t keeping their locally created managed objects around as long as the unmanaged
objects.  Caching in this case is a necessity.&lt;/p&gt;

&lt;p&gt;The MonoTouch framework manages a cache of its own in UIKit (UIView.Subviews), and this is where the problem lies.&lt;/p&gt;

&lt;p&gt;The problem, simply, is that &lt;em&gt;any&lt;/em&gt; Subviews manipulation performed by the monotouch user (fetching, adding, removing, insertAbove/Below, etc)
results in MonoTouch caching references to all subviews.  This cache not only forces more objects to be surfaced from
unmanaged to managed than we likely need, but it also means that there is a larger delta of time where potentially expensive
backing stores (or UIImages if the views are UIImageViews) are kept around.  And given the conservative nature of the collector,
there is a chance that some large resources may never be cleared.&lt;/p&gt;

&lt;p&gt;Imagine loading a nib that has a view with 50 subviews.  You dynamically create another subview and add it to this parent view.
At that point, monotouch surfaces all 50 existing subviews to C#, even though you never asked for them, and you&amp;#8217;ll likely
never use them.  Caching the reference in managed land to mimic the object graph in objective C is great, but the only
reference that needs caching is from the parent to the subview you just added.&lt;/p&gt;

&lt;p&gt;Below is a set of operations expressed as extension methods on UIView, that only cache the references they should be caching
(the ones on the objects you&amp;#8217;re passing in.)  There are also a couple of utility methods, such as RemoveAllSubviews, because
the alternative (looping over .Subviews calling RemoveFromSuperview) is terrible, as it recaches the Subviews property after
every Remove.&lt;/p&gt;

&lt;p&gt;Accessing the Subviews property itself will still result in all references being cached, which is unfortunate, but fixing that
would require moving away from UIView[] for the type of the property.&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;

&lt;div&gt;&lt;script src='https://gist.github.com/67af83769236f5d74b14.js?file='&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;code&gt;static class UIViewHelpers {
    static UIView[] GetCachedSubviews (this UIView view)
    {
        FieldInfo fi = typeof(UIView).GetField(&amp;quot;__mt_Subviews_var&amp;quot;, BindingFlags.NonPublic | BindingFlags.Instance);
        UIView[] subviews = null;
        if (fi != null)
            subviews = (UIView[])fi.GetValue (view);
        return subviews;            
    }
        
    static void SetCachedSubviews (this UIView view, UIView[] subviews)
    {
        FieldInfo fi = typeof(UIView).GetField(&amp;quot;__mt_Subviews_var&amp;quot;, BindingFlags.NonPublic | BindingFlags.Instance);
        if (fi != null)
            fi.SetValue (view, subviews);
    }
        
    static void AddSubviewToCache (this UIView view, UIView subview)
    {
        UIView[] subviews = view.GetCachedSubviews ();
        
        UIView[] new_subviews;
        
        if (subviews == null)
            new_subviews = new UIView[] { subview };
        else {
            new_subviews = new UIView[subviews.Length + 1];
            Array.Copy (subviews, new_subviews, subviews.Length);
            new_subviews[new_subviews.Length - 1] = view;
            for (int j = 0; j &amp;lt; subviews.Length; j ++)
                subviews[j] = null;
        }
                    
        view.SetCachedSubviews (subviews);
    }
    
    static void RemoveSubviewFromCache (this UIView view, UIView subview)
    {
        UIView[] subviews = view.GetCachedSubviews ();
        
        if (subviews != null) {
            if (subviews.Length == 1) {
                if (subviews[0] == view)
                    view.SetCachedSubviews (subviews);
            }
            else {
                for (int i = 0; i &amp;lt; subviews.Length; i ++) {
                    if (subviews[i] == view) {
                        UIView[] new_subviews = new UIView[subviews.Length - 1];
                        for (int j = 0; j &amp;lt; i; j ++) 
                            new_subviews[j] = subviews[j];
                        for (int j = i + 1; j &amp;lt; subviews.Length; j ++)
                            new_subviews[j-1] = subviews[j];
                        view.SetCachedSubviews (new_subviews);
                        // null everything out to be a good gc citizen
                        for (int j = 0; j &amp;lt; subviews.Length; j ++)
                            subviews[j] = null;
                        return;
                    }
                }
            }
        }
    }
        
    public static void AddSubview_ (this UIView view, UIView subview)
    {
        Messaging.void_objc_msgSend_IntPtr (view.Handle, new Selector (&amp;quot;addSubview:&amp;quot;).Handle, subview.Handle);            
        view.AddSubviewToCache (subview);
    }
            
    public static void RemoveFromSuperview_ (this UIView view)
    {
        UIView superview = view.Superview;
        if (superview == null)
            return;
        Messaging.void_objc_msgSend (view.Handle, new Selector (&amp;quot;removeFromSuperview&amp;quot;).Handle);
        superview.RemoveSubviewFromCache (view);
    }
    
    public static void InsertSubview_ (this UIView view, UIView subview, int index)
    {
        Messaging.void_objc_msgSend_IntPtr_int (view.Handle, new Selector (&amp;quot;insertSubview:atIndex:&amp;quot;).Handle, subview.Handle, index);
        AddSubviewToCache (view, subview);          
    }

    public static void InsertSubviewAbove_ (this UIView view, UIView subview, UIView subview2)
    {
        Messaging.void_objc_msgSend_IntPtr_IntPtr (view.Handle, new Selector (&amp;quot;insertSubview:aboveSubview:&amp;quot;).Handle, subview.Handle, subview2.Handle);
        AddSubviewToCache (view, subview);          
    }

    public static void InsertSubviewBelow_ (this UIView view, UIView subview, UIView subview2)
    {
        Messaging.void_objc_msgSend_IntPtr_IntPtr (view.Handle, new Selector (&amp;quot;insertSubview:belowSubview:&amp;quot;).Handle, subview.Handle, subview2.Handle);
        AddSubviewToCache (view, subview);          
    }
        
    public static void RemoveAllSubviews (this UIView view)
    {
        UIView[] subviews = GetCachedSubviews (view);
        if (subviews == null) {
            // subviews was never created, make sure we don't create it here
            var array = NSArray.ArrayFromHandle&amp;lt;IntPtr&amp;gt; (Messaging.IntPtr_objc_msgSend (view.Handle, new Selector (&amp;quot;subviews&amp;quot;).Handle), delegate (IntPtr i) { return i; });
            foreach (IntPtr ip in array)
                Messaging.void_objc_msgSend (ip, new Selector (&amp;quot;removeFromSuperview&amp;quot;).Handle);
            view.SetCachedSubviews (null);
        }
        else {
            view.RemoveSubviews (subviews);
        }
    }
        
    public static void RemoveSubviews (this UIView view, UIView[] subviews_to_remove)
    {
        UIView[] subviews = GetCachedSubviews (view);
        int subviewsFoundInCachedField = 0;
            
        foreach (UIView v in subviews_to_remove) {
            if (Array.Find&amp;lt;UIView&amp;gt;(subviews, delegate (UIView obj) { return obj.Handle == v.Handle; }) != null)
                subviewsFoundInCachedField ++;
            Messaging.void_objc_msgSend (v.Handle, new Selector (&amp;quot;removeFromSuperview&amp;quot;).Handle);
        }
            
        if (subviews.Length == subviewsFoundInCachedField)
            view.SetCachedSubviews (null);
        else {
            // XXX punt here - we might end up surfacing more objects than we mean to..
            subviews = view.Subviews; // get the property so the cached property is regenerated.
        }
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/noscript&gt;&lt;/div&gt;



&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/BvGq999atK4" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/12/monotouch-paying-the-bills/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[CoffeeKit Internals Part 2: Autoboxing]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/Efvh7shxn-U/" />
    <updated>2011-12-07T12:17:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/07/coffeekit-internals-part-2-autoboxing</id>
    <content type="html">&lt;p&gt;CoffeeKit provides a method for expressing iOS/OSX delegates in a
minimal amount of user code, by dynamically creating proxy objects.
This process is called &amp;#8220;autoboxing&amp;#8221;.&lt;/p&gt;

&lt;!--more--&gt;


&lt;p&gt;First a little intro material:&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s difficult, if not impossible, to write a useful iOS or OSX
application without having to deal with &lt;em&gt;delegates&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;These delegates are not the same as those in .NET-land.  In .NET,
&amp;#8220;delegate&amp;#8221; essentially means &amp;#8220;anonymous function&amp;#8221; or &amp;#8220;function
pointer&amp;#8221;.  In iOS and OSX, a delegate for an object O refers to an
object that responds to messages that have to do with (and often can
change) O&amp;#8217;s operation.  These messages are usually callbacks to report
on changes in O&amp;#8217;s state, or methods that O will use to decide which
actions to perform.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s typical (though by no means required) to define a &lt;em&gt;Protocol&lt;/em&gt;
describing the interface this object O expects from its delegate.
Some types list &amp;#8220;delegate methods&amp;#8221; in their documentation, but don&amp;#8217;t
define an actual protocol.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Protocols&lt;/em&gt; in objective-c are a lot like interfaces in other
languages in that they define members but no actual code.  They differ
from interfaces in languages like java and C# because subtypes aren&amp;#8217;t
required to provide implementations for all methods.  Everything is
optional, even (it seems) when the osx/ios documentation list methods
as being required.&lt;/p&gt;

&lt;p&gt;So, in order to do anything in the common case, you need to either
define a second type (your delegate type) to deal with a given object
type (say, a UIAlertViewDelegate to work with a UIAlertView) or you
add the methods to the object that&amp;#8217;s going to pop up the alert view
(that is, you use &amp;#8220;self&amp;#8221; as the delegate.)&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s roughly what the first case would look like for a UITextView and its delegate:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;UITextView and delegate type&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;span class='line-number'&gt;19&lt;/span&gt;
&lt;span class='line-number'&gt;20&lt;/span&gt;
&lt;span class='line-number'&gt;21&lt;/span&gt;
&lt;span class='line-number'&gt;22&lt;/span&gt;
&lt;span class='line-number'&gt;23&lt;/span&gt;
&lt;span class='line-number'&gt;24&lt;/span&gt;
&lt;span class='line-number'&gt;25&lt;/span&gt;
&lt;span class='line-number'&gt;26&lt;/span&gt;
&lt;span class='line-number'&gt;27&lt;/span&gt;
&lt;span class='line-number'&gt;28&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='objective-c'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@interface&lt;/span&gt; &lt;span class="nc"&gt;MyTextViewDelegate&lt;/span&gt; : &lt;span class="nc"&gt;NSObject&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;UITextViewDelegate&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;textViewDidChange:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UITextView&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;textView&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;MyTextViewDelegate&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;textViewDidChange:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UITextView&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;textView&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="cm"&gt;/* then elsewhere */&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="n"&gt;MyController&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;doStuff&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="n"&gt;textView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;UITextView&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithFrame:&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="n"&gt;myDelegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;MyTextViewDelegate&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;textView&lt;/span&gt; &lt;span class="nl"&gt;setDelegate:&lt;/span&gt; &lt;span class="n"&gt;myDelegate&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The second possibility (using self instead of a separate delegate type) looks like:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;Same example, but using self as the delegate&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='objective-c'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@implementation&lt;/span&gt; &lt;span class="nc"&gt;MyController&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nl"&gt;textViewDidChange:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UITextView&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;textView&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;   &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;doStuff&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="n"&gt;textView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="n"&gt;UITextView&lt;/span&gt; &lt;span class="n"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nl"&gt;initWithFrame:&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;textView&lt;/span&gt; &lt;span class="nl"&gt;setDelegate:&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;@end&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This code is much more compact, clearly.  In this case it makes sense,
because there&amp;#8217;s only one text view.  Imagine a situation where you
have many text views, though, with different behavior when they
change.  You&amp;#8217;ll either have to add code to discriminate the instance
of textView in -textViewDidChange, or you&amp;#8217;ll need custom delegate
types.&lt;/p&gt;

&lt;p&gt;In CoffeeKit it&amp;#8217;s possible to do one better, you&amp;#8217;ll see something very
close to the second form:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;Equivalent form in CoffeeKit&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView.delegate =&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;didChangeText: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;much more compact, and the use of the anonymous function there allows
you to tailor behavior to the specific instance.  You can also share a
function between multiple delegates:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;CoffeeKit with function sharing&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;myDidChangeText = &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView.delegate =&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;didChangeText: &lt;/span&gt;&lt;span class="nx"&gt;myDidChangeText&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView2 = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView2.delegate =&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;didChangeText: &lt;/span&gt;&lt;span class="nx"&gt;myDidChangeText&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;or even share the delegate object itself:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;CoffeeKit with delegate object sharing&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;myDelegate =&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;didChangeText = &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView.delegate = &lt;/span&gt;&lt;span class="nx"&gt;myDelegate&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView2 = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;textView2.delegate = &lt;/span&gt;&lt;span class="nx"&gt;myDelegate&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# or more efficiently&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# textView2.delegate = textView.delegate&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The magic responsible for this compact code rests solely in the autobox function:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;autobox function &lt;/span&gt;&lt;a href='https://github.com/toshok/coffeekit/blob/master/coffee/coffeekit.coffee#L95'&gt;github&lt;/a&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;span class='line-number'&gt;14&lt;/span&gt;
&lt;span class='line-number'&gt;15&lt;/span&gt;
&lt;span class='line-number'&gt;16&lt;/span&gt;
&lt;span class='line-number'&gt;17&lt;/span&gt;
&lt;span class='line-number'&gt;18&lt;/span&gt;
&lt;span class='line-number'&gt;19&lt;/span&gt;
&lt;span class='line-number'&gt;20&lt;/span&gt;
&lt;span class='line-number'&gt;21&lt;/span&gt;
&lt;span class='line-number'&gt;22&lt;/span&gt;
&lt;span class='line-number'&gt;23&lt;/span&gt;
&lt;span class='line-number'&gt;24&lt;/span&gt;
&lt;span class='line-number'&gt;25&lt;/span&gt;
&lt;span class='line-number'&gt;26&lt;/span&gt;
&lt;span class='line-number'&gt;27&lt;/span&gt;
&lt;span class='line-number'&gt;28&lt;/span&gt;
&lt;span class='line-number'&gt;29&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="nv"&gt;autoboxCount = &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nv"&gt;autobox = &lt;/span&gt;&lt;span class="nf"&gt;(obj, protocol) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# check if the object (or its constructor) conforms to the protocol.  if it does&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# then we can just use the object, without the proxy&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ConformsToProtocolAttribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doesObjectConformTo&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;protocol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ConformsToProtocolAttribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doesObjectConformTo&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;protocol&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NSObject&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;constructor: &lt;/span&gt;&lt;span class="nf"&gt;() -&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allocInstance&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_register&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# now loop over the items that are in obj and match up the names to those in the protocol&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;pv = &lt;/span&gt;&lt;span class="nx"&gt;protocol&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;pv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nv"&gt;addProxyMethod = &lt;/span&gt;&lt;span class="nf"&gt;(k, v) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;          &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;adding proxy method for #{k}, v.length = #{v.length}&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;          &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;() -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;(a1) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                &lt;span class="p"&gt;.&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a10&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;addProxyMethod doesn&amp;#39;t support methods with #{v.length} arguments.&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;          &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;SelectorAttribute&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;pv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sig&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;        &lt;span class="nx"&gt;addProxyMethod&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;RegisterAttribute&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;CKProtocolProxy#{autoboxCount++}&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;First off, this method is invoked like this, with an object and the protocol type:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;autoboxedObj = &lt;/span&gt;&lt;span class="nx"&gt;autobox&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UITextViewDelegate&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;A little explanation is likely required.&lt;/p&gt;

&lt;p&gt;autobox first checks to see if the object (or its constructor) has a
ConformsToAttribute with the protocol listed already.  If it does, it
just returns the object.  This facilitates the first objective-c style
listed way above, with the separate Delegate object; that pattern
still works in CoffeeKit.&lt;/p&gt;

&lt;p&gt;If the object doesn&amp;#8217;t conform to the protocol, we know we need to
build up a proxy object for it.  We loop over all properties of the
object you pass in, and if the property has the same name as that of
method listed in the protocol type, we create a proxy method to invoke
the one on the object.  The switch statement over the number of args
on the method may seem rather gross, and it may seem like all we need
to do is:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;ProtocolProxy&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;(args...) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This doesn&amp;#8217;t work, though, because of an implementation detail in the
coffeescript runtime.  If a javascript function is invoked from
objective-c, only the parameters declared in the JS function are
marshaled.  so, the methods:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nf"&gt;(v) -&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;and&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;differ in that the first one requires marshalling the first arg, while
the second doesn&amp;#8217;t.  They can be surfaced to objective-c as methods
that take any number of arguments, though.  The rest are just ignored
in marshalling.&lt;/p&gt;

&lt;p&gt;Remember in &lt;a href="http://localhost:4000/blog/2011/12/06/coffeekit-internals-part-1-attributes/"&gt;the last entry in this series&lt;/a&gt;
where I said that the real magic of class registration actually
happens in the RegisterAttribute constructor?  The last line before we
return the proxy is where the objective-c type for this proxy is
created from all of our proxy methods.&lt;/p&gt;

&lt;p&gt;One last thing: here&amp;#8217;s the UITextView binding where you see the actual
usage of the autobox function:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;UITextView &lt;/span&gt;&lt;a href='https://github.com/toshok/coffeekit/blob/master/coffee/UIKit/uitextview.coffee'&gt;github&lt;/a&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;UITextView&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;UIScrollView&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# Accessing the Delegate&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;ck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addProperty&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;delegate&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;set: &lt;/span&gt;&lt;span class="nf"&gt;(v) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;objc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;invokeSelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;setDelegate:&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;autobox&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UITextViewDelegate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;

&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/Efvh7shxn-U" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/07/coffeekit-internals-part-2-autoboxing/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[CoffeeKit Internals Part 1:  Attributes]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/aiR8kxPLMnc/" />
    <updated>2011-12-06T10:23:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/06/coffeekit-internals-part-1-attributes</id>
    <content type="html">&lt;p&gt;&lt;em&gt;warning: these internals posts are going to be fairly heavy with the
 code blocks and assume a fair amount of knowledge of how
 javascript/coffeescript work.  where things are potential unclear (or
 very, very cool) I&amp;#8217;ll add a lot more text, but (hopefully) I&amp;#8217;m not
 writing a book.&lt;/em&gt;&lt;/p&gt;

&lt;!--more--&gt;


&lt;h2&gt;Attributes in .NET&lt;/h2&gt;

&lt;p&gt;I started work on &lt;a href="http://github.com/toshok/coffeekit"&gt;CoffeeKit&lt;/a&gt;
after a long stint in the .NET world.  One of things I loved about
.NET &amp;#8211;  and C# in particular &amp;#8211; was its use of (and syntax for)
attributes.&lt;/p&gt;

&lt;p&gt;Take the following code:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='c#'&gt;&lt;span class='line'&gt;&lt;span class="na"&gt;[MySpecialClass]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="na"&gt;  [ASpecialMethod]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DoSomethingSpecial&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="na"&gt;  [ANotSoSpecialMethod (&amp;quot;The reason it&amp;#39;s not so special is rather boring as well&amp;quot;)]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DoSomethingNotWorthMentioning&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This would create (at compile time) metadata which is attached to the
type Foo (and its methods) inside the generated assembly.  The
metadata can be retrieved at runtime using reflection apis, and after
the right incantation you&amp;#8217;d have in your hands a nice
MySpecialClassAttribute instance.  They&amp;#8217;re used in many ways, such as
directing serialization to include particular members of types,
telling the IDE that certain properties should be hidden in the UI, or
even signaling tools to ignore particular warnings when performing
static analysis on the generated assemblies.&lt;/p&gt;

&lt;p&gt;The important bit to remember here is that there&amp;#8217;s a separation of
code and metadata.  The attribute can have properties and constructor
arguments that are serialized out to the metadata tables, but the
attributes don&amp;#8217;t actually perform any action themselves since they
don&amp;#8217;t have a way to get at the type they were applied to.  They are
later queried/consumed by other code which performs the action.&lt;/p&gt;

&lt;h2&gt;Attributes in CoffeeKit&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Another note: the syntax shown in this segment is pretty gross, and I
consider it to be the biggest wart in CoffeeKit. That said, it&amp;#8217;s not
everywhere, and I have ideas (which I&amp;#8217;ll go into toward the end of the
post) for additional new syntax to deal with it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;JavaScript is not a precompiled language, so there isn&amp;#8217;t really a
useful distinction between code and metadata.  While you can come up
with code that &lt;em&gt;looks&lt;/em&gt; a lot like metadata (and indeed, CoffeeKit
strives to make the bindings as declarative and metadata-like as
possible), the code is still executed.  This affords us a special
opportunity to make attributes that don&amp;#8217;t act &lt;em&gt;quite&lt;/em&gt; like .NET
attributes.&lt;/p&gt;

&lt;p&gt;The way CoffeeKit would express the above C# example is:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;DoSomethingSpecial: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ASpecialMethodAttribute&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;DoSomethingSpecial&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;DoSomethingNotWorthMentioning: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ANotSoSpecialMethodAttribute&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;DoSomethingNotWorthMentioning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The reason it&amp;#39;s not so special is rather boring as well&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MySpecialClassAttribute&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;&lt;em&gt;see?  told you it was a wart.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Note how the attributes come after the definition.  This allows us to
access the actual object we&amp;#8217;re applying the attribute to (we pass it
as the first argument to each of the attribute constructors).  This
syntax also relies on the fact that the attribute constructor will do
2 things, both optional:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the attribute to the object&amp;#8217;s list of attributes so that it can be found later.&lt;/li&gt;
&lt;li&gt;Any further work at attribute create/apply time the attribute might specify.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Let&amp;#8217;s look at a real CoffeeKit example:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;A coffeekit type to proxy from an objective-c target/action pair to a JS function&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;TargetActionProxy1&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NSObject&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;constructor: &lt;/span&gt;&lt;span class="nf"&gt;(fn) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                 &lt;span class="k"&gt;super&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;objc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allocInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;                 &lt;span class="vi"&gt;@fn = &lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;proxyAction: &lt;/span&gt;&lt;span class="nf"&gt;(a1) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;@fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SelectorAttribute&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nx"&gt;proxyAction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;action&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;v@:@&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ck&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RegisterAttribute&lt;/span&gt; &lt;span class="nx"&gt;TargetActionProxy1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;TargetActionProxy1&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;The target/action pattern is pervasive in objective-c.  Instead of a
callback function pointer, you register a callback object as well as
the selector to invoke on that object.  The above class provides an
easy way to map from that pattern to a more natural Coffeescript usage - the
anonymous function:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;tapProxy = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TargetActionProxy1&lt;/span&gt; &lt;span class="nf"&gt;(v) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;you tapped!&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addGestureRecognizer&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;UITapGestureRecognizer&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithTarget&lt;/span&gt; &lt;span class="nx"&gt;tapProxy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tapProxy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;proxyAction&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This syntax is still a little on the objective-c side of things.  A
better binding would be to expose a &amp;#8220;Tapped&amp;#8221; event which hides all the
proxy stuff from the rest of your code.  Several CoffeeKit controls
already do this.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s look at the code for RegisterAttribute:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;RegisterAttribute &lt;/span&gt;&lt;a href='https://github.com/toshok/coffeekit/blob/master/coffee/coffeekit.coffee#L21'&gt;github&lt;/a&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;RegisterAttribute&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Attribute&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;constructor: &lt;/span&gt;&lt;span class="nf"&gt;(obj, name) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;super&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;obj._ck_register = &lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;objc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;internalRegisterJSType&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_register&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__super__&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__super__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_register&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This is where a lot of the real plumbing in the bindings happens.
objc.internalRegisterJSType is a method defined in the CoffeeKit
runtime that dynamically allocates an objective-c type and populates
it with methods and fields that have been defined in the Coffeescript
class.  The methods it picks are those decorated with the
SelectorAttribute as seen above in the TargetActionProxy1 code, so you
can have both methods you expose through objective-c and methods you
don&amp;#8217;t living in the same class.&lt;/p&gt;

&lt;p&gt;The RegisterAttribute code takes care of the second thing attributes
are supposed to do (&amp;#8220;Any further work&amp;#8230;&amp;#8221;), but let&amp;#8217;s look at its
superclass to see how it handles the first thing:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;Attribute &lt;/span&gt;&lt;a href='https://github.com/toshok/coffeekit/blob/master/coffee/coffeekit.coffee#L5'&gt;github&lt;/a&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;span class='line-number'&gt;12&lt;/span&gt;
&lt;span class='line-number'&gt;13&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Attribute&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;constructor: &lt;/span&gt;&lt;span class="nf"&gt;(@obj) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;Attribute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="vi"&gt;@add: &lt;/span&gt;&lt;span class="nf"&gt;(obj, attr) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_attributes&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="nv"&gt;obj._ck_attributes = &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="vi"&gt;@find: &lt;/span&gt;&lt;span class="nf"&gt;(obj, attrType) -&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nv"&gt;attrs = &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_attributes&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_ck_attributes&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="nx"&gt;attr&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;attr&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;attrs&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="nx"&gt;attr&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;attrType&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;Here&amp;#8217;s where the list of attributes is built up.  The Attribute class
also provides a handy method (find) which will return all the
instances of the supplied attribute type that have been applied to a
given object.&lt;/p&gt;

&lt;p&gt;Since the attribute constructor is just code, there&amp;#8217;s no limit to what
it can do.  CoffeeKit contains attributes which actually manipulate
the type to which they&amp;#8217;re applied (MixinProtocolAttribute and
ConformsToProtocolAttribute), which provides for much more compact
binding code.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s also no limit to &lt;em&gt;when&lt;/em&gt; the attribute constructor can do its
work.  Attributes don&amp;#8217;t have to be applied to toplevel classes.
Indeed, classes in coffeescript don&amp;#8217;t have to be at the toplevel
either.  But more on that when we get to autoboxing.&lt;/p&gt;

&lt;p&gt;This post is already getting rather long, but definitely check out
&lt;a href="https://github.com/toshok/coffeekit/blob/master/coffee/coffeekit.coffee"&gt;coffeekit.coffee&lt;/a&gt;
in git, where all the Attribute subclasses (and a lot other cool
plumbing) are defined.&lt;/p&gt;

&lt;h2&gt;A note on syntax&lt;/h2&gt;

&lt;p&gt;As I said above, I consider this syntax to be wart.  But really, it&amp;#8217;s
the destination syntax for a new piece of sugar.  Now it&amp;#8217;s just a
matter of figuring out which tastes the sweetest.&lt;/p&gt;

&lt;p&gt;I have a few ideas bouncing around in my head.  The .NET syntax won&amp;#8217;t
fly because [Foo] is a perfectly legal array in javascript, regardless
of where it&amp;#8217;s placed.&lt;/p&gt;

&lt;p&gt;maybe something like:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;attribute syntax experiment #1&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;span class='line-number'&gt;11&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;# I think the ()&amp;#39;s help set it off a bit?&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;@attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MySpecialClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# so meybe instead of the ()&amp;#39;s here&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;@attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ASpecialMethod&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;DoSomethingSpecial: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# we could do them like?&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;@attr&lt;/span&gt; &lt;span class="nx"&gt;ANotSoSpecialMethodAttribute&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;The reason it&amp;#39;s not so special is rather boring as well&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;DoSomethingNotWorthMentioning: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;that has issues with the @ syntax because attr could be a function
defined at the toplevel scope, which @ would resolve to in the outer
context (but not in the inner, where it would result to Foo - ugh).&lt;/p&gt;

&lt;p&gt;Or maybe something like (the #[] borrowed from &lt;a href="http://rustlang.org"&gt;Rust&lt;/a&gt;, but it runs afoul of coffeescript&amp;#8217;s comment character):&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;attribute syntax experiment #2&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="c1"&gt;#[MySpecialClass]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;# or maybe...&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ASpecialMethod&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nv"&gt;DoSomethingSpecial: &lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;I haven&amp;#8217;t even begun to play with the ideas seriously, more than just
pushing symbols around.  whatever syntax is chosen it can be made to
work, and the transformation is very easily understood from the sugar
to the resulting form.  Please leave comments if you have feelings
for/against these ideas, or if you have a better one - or even
pointers to other languages that have similar constructs.&lt;/p&gt;

&lt;p&gt;ciao for now!&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/aiR8kxPLMnc" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/06/coffeekit-internals-part-1-attributes/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[A CoffeeKit Introduction]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/BAjt6M4Xjfc/" />
    <updated>2011-12-06T08:19:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/06/a-coffeekit-introduction</id>
    <content type="html">&lt;p&gt;&lt;a href="http://github.com/toshok/coffeekit"&gt;CoffeeKit&lt;/a&gt; is where I&amp;#8217;ve been
putting a lot of my energy recently.  It&amp;#8217;s a project with a rather
simple aim: Let web developers take advantage of native OSX and iOS
apis at the same time as they&amp;#8217;re able to use the existing browser (and
node.js) infrastructure apis they already know.&lt;/p&gt;

&lt;!--more--&gt;


&lt;p&gt;For example, you can fetch data from webservices the same way you
would in a web app, and populate a UITableView with it.  You can draw
to a canvas using webgl, except that canvas is now a fully-fledged
UIView in a native iOS app and can respond to gestures and make use of
UIAnimations.&lt;/p&gt;

&lt;p&gt;You also open your code up to an exciting amount of reuse.  The same
business logic can exist in three different places:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On the server, running under node.js&lt;/li&gt;
&lt;li&gt;In the browser.&lt;/li&gt;
&lt;li&gt;In natively built mobile apps, selling like hotcakes in app stores.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;You get to code in JavaScript or &lt;a href="https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS"&gt;anything that transpiles to it&lt;/a&gt;
(including my personal choice at the moment: &lt;a href="http://coffeescript.org"&gt;coffeescript&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a list of the web apis presently in CoffeeKit:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="http://www.w3.org/TR/XMLHttpRequest"&gt;XmlHttpRequest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://dev.w3.org/html5/workers/"&gt;WebWorkers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.khronos.org/registry/webgl/specs/latest/"&gt;WebGL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.khronos.org/registry/typedarray/specs/latest"&gt;TypedArrays&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;And a couple of things that are coming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="http://wiki.ecmascript.org/doku.php?id=harmony:binary_data"&gt;Binary Data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.w3.org/TR/IndexedDB/"&gt;IndexedDB&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;enough talk, let&amp;#8217;s look at a tiny snippet:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;A small example snippet&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='coffeescript'&gt;&lt;span class='line'&gt;&lt;span class="vi"&gt;@primeButton = &lt;/span&gt;&lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;UIButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonWithType&lt;/span&gt; &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;UIButtonType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;roundedRect&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;@primeButton&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setTitle&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Click to generate primes&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;UIControlState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;normal&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="vi"&gt;@primeTextField = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;UITextField&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;initWithFrame&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NSRect&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;screenBounds&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="err"&gt;/2+50, screenBounds.width, 50&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="vi"&gt;@primeCount = &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="vi"&gt;@primeButton.clicked = &lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="vi"&gt;@worker = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Worker&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;./prime-worker&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="vi"&gt;@worker.onmessage = &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="vi"&gt;@primeTextField.text = &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Prime #{++@primeCount}: #{msg}&amp;quot;&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This is a pretty simple example that creates a native button and
textfield.  When the button is clicked a Web Worker is spawned, and
whenever that worker sends us a message we update the text in the
textfield.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s the JavaScript for the worker:&lt;/p&gt;

&lt;figure class='code'&gt;&lt;figcaption&gt;&lt;span&gt;prime-worker.js&lt;/span&gt;&lt;/figcaption&gt;&lt;div class="highlight"&gt;&lt;table&gt;&lt;tr&gt;&lt;td class="gutter"&gt;&lt;pre class="line-numbers"&gt;&lt;span class='line-number'&gt;1&lt;/span&gt;
&lt;span class='line-number'&gt;2&lt;/span&gt;
&lt;span class='line-number'&gt;3&lt;/span&gt;
&lt;span class='line-number'&gt;4&lt;/span&gt;
&lt;span class='line-number'&gt;5&lt;/span&gt;
&lt;span class='line-number'&gt;6&lt;/span&gt;
&lt;span class='line-number'&gt;7&lt;/span&gt;
&lt;span class='line-number'&gt;8&lt;/span&gt;
&lt;span class='line-number'&gt;9&lt;/span&gt;
&lt;span class='line-number'&gt;10&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;td class='code'&gt;&lt;pre&gt;&lt;code class='javascript'&gt;&lt;span class='line'&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;      &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;    &lt;span class="c1"&gt;// found a prime!&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="c1"&gt;// postMessage(n); coffeekit only supports string messages for now&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;  &lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;span class='line'&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/figure&gt;


&lt;p&gt;This code was lifted directly from a W3C
&lt;a href="http://dev.w3.org/html5/workers/#a-background-number-crunching-worker"&gt;example for web workers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the next post I&amp;#8217;ll go over how the bindings are written, and show
some of the novel (all JavaScript) goodness CoffeeKit brings to the
party.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/BAjt6M4Xjfc" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/06/a-coffeekit-introduction/</feedburner:origLink></entry>
  
  <entry>
    <title type="html"><![CDATA[Writing about writing code]]></title>
    <link href="http://feedproxy.google.com/~r/ToshokElectricLightManufacturing/~3/M0Pi6ihjDUI/" />
    <updated>2011-12-05T23:35:00-08:00</updated>
    <id>http://toshok.github.com/blog/2011/12/05/writing-about-writing-code</id>
    <content type="html">&lt;p&gt;It&amp;#8217;s time to start blogging again.  I&amp;#8217;ve been far too quiet on this front for too long,
mostly because all time spent in front of the computer has been taken up with generating
code, or generating posts that have to fit within 140 characters.&lt;/p&gt;

&lt;p&gt;And now that I have an interesting and useful project in the works,
expect to see a lot about it here.&lt;/p&gt;
&lt;img src="http://feeds.feedburner.com/~r/ToshokElectricLightManufacturing/~4/M0Pi6ihjDUI" height="1" width="1"/&gt;</content>
  <feedburner:origLink>http://toshok.github.com/blog/2011/12/05/writing-about-writing-code/</feedburner:origLink></entry>
  
</feed>

