How Cappuccino handles pass-by-reference APIs in Cocoa.

I’ve been using Cappuccino at work recently and I came across a corner of Objective-J that I previously didn’t know about: how to use enumerateObjectsUsingBlock.

One of the nice things about Objective-C is that it has blocks.  Blocks allow you to make functions that take other functions as arguments, all while preserving scope. One of the most popular block-using APIs is the NSArray method enumerateObjectsUsingBlock. It takes a block that has three arguments: the current value of the iteration, the current index and a reference to a Boolean stop variable. If you set the variable to YES the loop will stop (much like the break keyword, which only works to break out of while and for loops.)

This is a problem for JavaScript because it always passes by value. Since it’s very cheap to create objects in JavaScript you could just pass an object with a property you can set, but that means that you have to remember the property name to set and the caller has to remember to copy that value back out. The Cappuccino developers thought of using a function that closes over an object in the parent scope via a macro called AT_REF.

To use a cappuccino AT_REF you simply call it and pass in the new value you want to set. Like so:

[array enumerateObjectsUsingBlock:function (item, i, stop) { 

In the implementation of enumerateObjectsUsingBlock you’ll see something like the following:

var stop = NO; 
for (var i = 0; i < length; i++) { 
    block(arr[i], i, BY_REF(stop)); 
    if (stop) 

The AT_REF macro expands to something like:

(function(val) { 
    if (arguments.length) 
        return (stop = val); 
    return val; 

That way you can both set the value by calling it with an argument and get the value by calling it with no arguments.

It’s brilliant if you ask me. But then again, pretty obvious when you think about it.

In the future the devs would like to integrate this into the language by using @ref() and @deref(), (hence the name of the macro).

To define methods that use AT_REF in your own code you can simply @import <Foundation/Ref.h>.  Clients of your API just need to know to call the argument to set and get it’s value.

Apple’s JIS Keyboard layout and the ADM3A

A few weeks ago a blog post on showed pictures of the old terminal that Bill Joy used when he created VI.  Terminals back then didn’t have arrow keys so if you wanted to move the cursor around you held down some kind of modifier and hit the H, J, K or L keys.  And they were labeled as such.  This news didn’t really interest me because I’ve read that somewhere before but when someone on my twitter stream posted a picture of the keyboard I noticed how much it resembles my Mac’s keyboard layout.

Since I live in Japan I use a Mac with a Japanese keyboard layout. The standard Japanese keyboard layout is a little hard to get used to at first because while it is a QWERTY keyboard some characters are in different places.  The placing of the double quote character and the left and right parenthesis eventually grew on me, but I always had a nagging feeling that the Japanese keyboard arbitrarily changed the locations of these very important for programming keys for no good reason.

When I saw the keyboard layout of the ADM3A I realized that it was in fact kind of the opposite.  The US keyboard layout has changed significantly since the 70s, but the Japanese keyboard didn’t.  Apple even keeps the position of the CTRL key where it should be (To the left of A).

The legendary ADM3A keyboard layout. (credit. Wikipedia)
The Apple Japanese keyboard. The Standard Japanese Keyboard layout has the exact same number row keys as the ADM3A.







I think this might be one of the reasons that I’ve grown to like the Japanese keyboard layout.