<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ahruman’s Webthing</title>
	<atom:link href="http://jens.ayton.se/blag/feed/" rel="self" type="application/rss+xml" />
	<link>http://jens.ayton.se/blag</link>
	<description>Cocoa coding stuff, when I can be bothered.</description>
	<lastBuildDate>Thu, 15 Jul 2010 09:01:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Black Tuesday</title>
		<link>http://jens.ayton.se/blag/black-tuesday/</link>
		<comments>http://jens.ayton.se/blag/black-tuesday/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 11:06:27 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[FRA]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=87</guid>
		<description><![CDATA[<span style="color: rgba(0,0,0,0)"><span style="background-color: black">█████</span> <span style="background-color: black">███</span> <span style="background-color: black">██████</span> <span style="background-color: black">███</span></span> FRA <span style="color: rgba(0,0,0,0)"><span style="background-color: black">████</span> <span style="background-color: black">███████</span> <span style="background-color: black">██████</span></span>. <a href="http://jens.ayton.se/blag/black-tuesday/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="censored">
<p><span>███ ██ ███ ████ ██</span>, <span>██████ ███████ ███</span>. <span>██ █ ███ ███</span>. <span>████ ███ ███</span>, <span>█ █████ ███</span>. <span>████ █ ██ █ ███  ███ ██ ██  ██</span>. <span>██ ███ ██ ███ █ █████</span>. <span>███ ████ ███ ██</span>, <span>██ ████ ███ ███</span> FRA <span>████</span>. <span>███ ████ █████ ██</span>, <span>█ ███ ██ ███ █</span>. <span>█████ ███ ██ ██ ███ ███ █ ████ ██ ███</span>. <span>███ █ ██ ███</span>, <span>█ ███ ██</span>. <span>██ ███ ████ ███</span>. <span>███ ██████ ███ ██</span>, <span>██ ███ █ ███ ██</span>.</p>
<p><span>████ █████ ███ ███</span>. <span>██ █ █</span>, <span>███ █ ██ ███ █</span>, <span>███ █ ██</span>. <span>████ ██████ ██ █ ███ ███ ███████</span>. <span>███ █ ██ ██ ██ ████ ███ █ █ ██</span>. <span>████ ██████</span>, <span>██ █████ ███ ████</span>, <span>█ ██ ████ ██</span>, <span>█ ██████ ██ ███ █ ██</span>. <span>██ ████ █████ ██ █ █████</span>. <span>███ ███ ██ █ ██</span>, <span>██████ █████ █</span>. <span>███ ██ ████ ██</span>. <span>██ ████ ███ ███</span>. <span>████ █ ████ ██</span>. <span>█ ███ ████ ███</span>, <span>█ ██ ██████ █ ███ ████</span>. <span>███ █ ██ ██ ██ ███ ████ █ ██ ███</span>. <span>███ ████ ██ ███ ███ ████ ████</span>. <span>██ ██ ██ █ ███ ████ ███</span>.</p>
<p><span>███ █ ███ █</span>. <span>████ ██ ███ █ ███ ███ ███ █ █ ██</span>. <span>██ ████ █████ ████</span>. <span>██ ███ ██ ██ ██ ████ ██ █████ ███ ███</span>. <span>███ ██ ██</span>, <span>███ █ ██ █</span>, <span>█████ ██ ███</span>. <span>██ ███ ██ ████ ███ ████ █ ███ ██ █████</span>. <span>██ █ ██ █ ██ ███ ████</span>. <span>███ ██ ██████ ██</span>, <span>█ ████ ███ ███ ██</span>. <span>█████ ████</span>, <span>██ █ ████ ████</span>, <span>███ ██ █████ ██</span>, <span>█ █████ ███ ███ █ ██</span>. <span>███ ██ ██</span>, <span>███ ████ ████ █</span>, <span>███ ██ ███</span>. <span>█ █ █████ ███</span>. <span>████ ████ █████ ███ ██ ████</span>. <span>█████ ██ ███ ███ █ ████ ██ ███ █ ███ ████ ███ ███</span>; <span>█████ █████</span>, <span>██ ██ ████ █████</span>, <span>███ ██ █████ █</span> FRA <span>█ ██ ████ ███ ██ ██ ███</span>. <span>██ █ ██ ██</span>. <span>██ █ ████ ███</span>. <span>██ ██ █</span>, <span>█████ █ █████ █ ██</span>, <span>████ ██ ███</span>. <span>██ ███</span>. <span>███ █ ██ ███</span>, <span>█ ████ ██</span>.</p>
<p><span>█████ █ ██ ██ ██ ███ █████</span>. <span>██ ████ ███████ ███ █ ████</span>. <span>███ ███ ██ █ ██ █████ ██ ████ ██ ████</span>. <span>██████ ████ ███ ██ ██ ██</span>. <span>██ █ █████ ██</span>. <span>██ █████ ████ ███</span>. <span>███ ████ █████ ██</span>, <span>█ ████ ███ ███ █</span>. <span>██ █████ ████ ██ ██ █████</span>. <span>██ ██████</span>, <span>█ █ █████ ███</span>, <span>██ ██ █████ ██</span>, <span>██ ████ ███ █ █ ██ █</span>. <span>██ ██ ██</span>, <span>████ ██ ███ █ ██</span>, <span>█████ █ ███</span>. <span>██ ████</span>, <span>██ █ ██████ ███</span>, <span>█ ██ ████ ███</span>, <span>█████ ███ ███ ██ █████ ██</span>. <span>██ █ ███ █ ██ ███ ████</span>. <span>██ ███ ███</span>, <span>████ █</span> FRA <span>███ ██</span>, <span>█████ █ ██</span>. <span>███ ████ ████ ██ █ ████</span>. <span>████ ██ ███ ██</span>, <span>██ ████ ██</span>. <span>██████ ████ ███ ██ ██ ████</span>. <span>██ █████ ███ ████</span>.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/black-tuesday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Proposal: generics (and some other stuff) for Objective-C</title>
		<link>http://jens.ayton.se/blag/generics-for-objective-c/</link>
		<comments>http://jens.ayton.se/blag/generics-for-objective-c/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 20:38:04 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[flamebait]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[notc++]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tl;dr]]></category>
		<category><![CDATA[type system]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=78</guid>
		<description><![CDATA[Some time ago, Greg Parker asked the Twitternets what we’d like to see in a purely hypothetical Objective-C-without-the-C language. Someone — I believe it was Landon Fuller — pointed at an article about the Strongtalk type system for Smalltalk. I &#8230; <a href="http://jens.ayton.se/blag/generics-for-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, <a href="http://twitter.com/gparker/">Greg Parker</a> asked the Twitternets what we’d like to see in a purely hypothetical Objective-C-without-the-C language. Someone — I believe it was <a href="http://twitter.com/landonf">Landon Fuller</a> — pointed at an article about <a href="http://bracha.org/nwst.html">the Strongtalk type system for Smalltalk</a>. I quite like the idea of Objective-C-without-the-C (i.e., a language that is native to the Objective-C object system and runtime without the baggage of C), but after reading that article I found myself asking why we couldn’t do something similar in Objective-C.</p>
<p>I don’t think my random musings have much influence on the design of the language, but if I don’t write it down nobody’s going to know how nuts I am, so here’s a semi-concrete proposal for contextual types and generics for Objective-C. Since anyone even mentioning generics in the vicinity of Objective-C will inevitably be flamed for trying to turn it into C++, this is followed by an aside entitled <i>Why This is Not the Baby-eating Spawn of Bjarne Stroustrup</i>. (Nothing personal, Bjarne.)</p>
<p><span id="more-78"></span><br />
<h2 id="types-in-objc">Types in Objective-C</h2>
<p>A fundamental characteristic of Objective-C is that it has two separate type systems: the dynamic type system, which applies to objects, and the static type system, which applies to variables (which may or may not refer to objects).</p>
<p>As far as objects are concerned, the static type system is optional — you can refer to any object with the type <code class="objc code">id</code>, except when calling a method whose type may be ambiguous. The static type system is also advisory — it suggests to programmers, the compiler and other tools such as the <a href="http://developer.apple.com/tools/Xcode/">IDE</a> and <a href="http://clang-analyzer.llvm.org/">analyzer</a> what the class of an object may be at runtime, but doesn’t constrain the object. A variable of type <code>NSString&nbsp;*</code> may actually refer to an <code>NSArray</code> at runtime, and method calls will dynamically go to <code>NSArray</code>’s implementation.</p>
<p>My proposal deals only with the static type system. The idea is to provide information that helps the compiler and analyzer check your logic, and the IDE to provide better suggestions. This is done by replacing <code>id</code> with more specific static types in most of the situations it’s used in. The generated code is not affected in any way. The proposal does not introduce bondage and discipline on the language; the new types can always be cast away.</p>
<h2 id="contextual-types">Contextual Types</h2>
<p>By far the most common use of <code>id</code> is as the return type for methods that may return an instance of “this” class, or of the subclass it’s called on. The obvious examples are <code>+alloc</code> and <code>-init</code>.</p>
<p>The IDE and, I believe, the analyzer already use heuristics to determine the return type of <code>+alloc</code> and <code>-init</code>, but I propose formalizing this in code. It would look something like this:</p>
<pre class="objc code" id="contextual-example">@interface NSObject &lt;NSObject&gt;
{
    Class   isa;
}

+ (void)load;

+ (void) initialize;
- ([:Self]) init;

+ ([:Self]) new;
+ ([:Self]) allocWithZone:(NSZone *)zone;
+ ([:Self]) alloc;

// ...

+ ([:Superclass]) superclass;
+ ([:Class]) class;
- ([:Superclass]) superclass;
- ([:Class]) class;

@end</pre>
<p>When calling a class method, <code>[:Class]</code> resolves to the receiver (or, type-equivalently, a pointer to an instance the receiver’s metaclass), and <code>[:Self]</code> resolves to a pointer to an instance of the receiver. <code>[:Superclass]</code> resolves to the superclass of the receiver. For instances, they resolve as for class methods on the class of the instance.</p>
<p>I’m sure some people will object to the conceptual purity of this design, and possibly the names and syntax. The colon is a bit odd — it’s there to avoid ambiguity with generics (see <a href="#generics">below</a>). All of these are minor quibbles; the syntax would need to be reviewed if actually implementing it.</p>
<p>So what’s the point? Consider the following code:</p>
<pre class="objc code">NSString *s = [[NSArray alloc] init];</pre>
<p>As it stands, this is perfectly valid and doesn’t generate a compiler diagnostic. With the addition of contextual types it would, because:
<ul>
<li> The type of <code>+[NSArray alloc]</code> (inherited from <code>NSObject</code>) is <code>[:Self]</code>, which resolves to <code>NSArray&nbsp;*</code>.</li>
<li> The receiver of the <code>-init</code> is thus known to be an <code>NSArray</code>.</li>
<li> The type of <code>-[NSArray init]</code> (inherited from <code>NSObject</code>) is <code>[:Self]</code>, which again resolves to <code>NSArray&nbsp;*</code>.</li>
<li> Therefore, the right hand side of the assignment is of type <code>NSArray&nbsp;*</code>, and the assignment is invalid.</ul>
<p>If, for some reason, you really wanted to do that, you could use an explicit cast to get rid of the diagnostic.</p>
<h2 id="generics">Generics</h2>
<p>The other major use of <code>id</code> is for polymorphic collections. True polymorphic collections are great! But some of the time, you only want to put one kind of object in your collection, and would appreciate the computer doing the drudge work of checking that you didn’t put the wrong stuff in the wrong place.</p>
<p>From the perspective of the previous section, generics are a simple extension to contextual types. Instead of restricting you to <code>[:Self]</code> and the highly specialized <code>[:Class]</code> and <code>[:Superclass]</code>, you can provide one or more class names as a parameter to a type declaration. Example time again:</p>
<pre class="objc code" id="mythingholder-example">@interface MyThingHolder[ThingType = id] : NSObject
{
    [ThingType]     thing;
}

- ([:Self]) initWithThing:([ThingType])thing;

- (void) setThing:([ThingType])thing;
- ([ThingType]) thing;

// Or, for modernists:
@property (readonly, nonatomic) [ThingType] thing;

@end

// ...
MyThingHolder[NSString] *holder = [[MyThingHolder[NSString] alloc] initWithThing:@"foo"];
holder.thing = [NSNumber numberWithBool:MAYBE];      // Warning: type mismatch
holder.thing = (id)[NSNumber numberWithBool:MAYBE];  // OK. (an issue here would be inconsistent with general Objective-C behaviour.)

// Or, equivalently:
typedef MyThingHolder[NSString] MyStringHolder;
MyStringHolder *holder = [[MyStringHolder alloc] initWithThing:@"foo"];</pre>
<p>Some notes: the type parameter can only be a class, since specialized code is not generated for each type. (See <a href="#bjarnespawn">Why This is Not the Baby-eating Spawn of Bjarne Stroustrup</a> below.) Since the type parameter is always a class, I have made the <code>*</code> implicit. This is cleaner, but could well lead to confusion and is quite likely a bad idea. (If only I had a time machine…)</p>
<p>The type parameter has a default value, previously unheard of in Objective-C, so that you can ignore generics and create a “vanilla” <code>MyThingHolder</code> that works just like in traditional Objective-C. This provides an upgrade path for existing classes:</p>
<pre class="objc code" id="collections-example">@interface NSArray[Item &lt;NSObject&gt; = id &lt;NSObject&gt;] : NSObject &lt;NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration&gt;

- (NSUInteger) count;
- ([Item]) objectAtIndex:(NSUInteger)index;

@end

@interface NSArray[Item] (NSExtendedArray)

- (NSArray[Item] *) arrayByAddingObject:([Item])anObject;
- (NSArray[Item] *) arrayByAddingObjectsFromArray:(NSArray[Item] *)otherArray;
// ...
- (BOOL)containsObject:([Item])anObject;
// ...
<span id="decl-arrayWithObject">+ ([:Self[Item]]) arrayWithObject:([Item])anObject;</span>
// ...
<br/>
@interface NSDictionary[Key &lt;NSCopying, NSObject&gt; = id &lt;NSCopying, NSObject&gt;, Value &lt;NSObject&gt; = id &lt;NSObject&gt;] : NSObject &lt;NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration&gt;

- (NSUInteger) count;
- ([Value]) objectForKey:([Key])aKey;
- (NSEnumerator[Key] *)keyEnumerator;

@end

@interface NSDictionary[Key, Value] (NSExtendedDictionary)

- (NSArray[Key] *) allKeys;
- (NSArray[Key] *) allKeysForObject:([Value])anObject;
- (NSArray[Value] *) allValues;
// ...
- (BOOL) isEqualToDictionary:(NSDictionary[Key, Value] *)otherDictionary;
- (NSEnumerator[Value] *) objectEnumerator;
- (NSArray[Value] *) objectsForKeys:(NSArray[Key] *)keys notFoundMarker:([Value])marker;
// ...
- (void) getObjects:([Value] *)objects andKeys:([Key] *)keys;
// ...</pre>
<p>These examples introduce an additional concept: restrictions on type parameters, in this case protocol requirements. The other obvious type of restriction would be a superclass requirement, such as <code>[Type: NSString = NSString]</code>.</p>
<p>Using these genericized versions in the same manner as the existing versions would require no code changes – as long as you’re using them with objects that fulfil the protocol requirements, which already exist but aren’t explicit in code.</p>
<h2 id="type-conformance">Type Conformance</h2>
<p>A fundamental question for static type systems with inheritance is when implicit casts are allowed. There are subtleties here, some of which I probably haven’t considered, and I have a feeling I’ve considered some and then forgotten about them. The most obvious case is when the base type and each type parameter could be validly cast:</p>
<pre class="objc code" id="basic-implicit-casts-example">NSMutableArray[NSMutableString] *a = whatever;
NSMutableArray *b = a;            // OK, b is NSMutableArray[id &lt;NSObject&gt;]
NSMutableArray[NSString] *c = a;  // OK
a = b;                            // Not OK, implicit cast from NSMutableArray[NSString] to NSMutableArray[NSMutableString]
NSArray[NSMutableString] *d = a;  // OK
NSArray[NSString] *e = a;         // OK
id f = a;                         // OK
</pre>
<p>As indicated <a href="#mythingholder-example">above</a>, passing an <code>id</code> when a parameter type is expected is necessary for consistency with Objective-C in general:</p>
<pre class="objc code" id="id-implicit-cast-example">NSMutableArray[NSString] *a = whatever;
[a addObject:[NSNumber numberWithInt:42]];      // Type mismatch, assuming +numberWithInt: is declared to return [:Self]
[a addObject:(id)[NSNumber numberWithInt:42]];  // OK</pre>
<p>How about the case where a method returns an unadorned type?</p>
<pre class="objc code" id="legacy-example">@interface LegacyThing: NSObject
- (NSArray *) legacyListOfStrings;
@end

LegacyThing *l = whatever;
NSArray[NSString] *list = [l legacyListOfStrings];</pre>
<p>In order to minimize the burden of adopting generic syntax, I’d suggest explicitly permitting this, with an optional warning. (The non-generic equivalent, assigning an <code>id&nbsp;&lt;NSObject&gt;</code> to an <code>NSString&nbsp;*</code>, generates the somewhat unexpected warning <i>“type &#8216;id &lt;NSObject&gt;&#8217; does not conform to the &#8216;NSCopying&#8217; protocol”</i> in GCC and nothing in Clang. Assigning an <code>NSObject&nbsp;*</code> to an <code>NSString&nbsp;*</code> generates warnings in both. My proposal is that assigning a <code>T</code> to a <code>T[P]</code> should work without warning by default, even if the default type parameter is a class rather than <code>id</code> with or without protocols.)</p>
<h2 id="bjarnespawn">Why This is Not the Baby-eating Spawn of Bjarne Stroustrup</h2>
<p>Many Objective-C programmers are refugees from the blasted wasteland of C++, and will reflexively cringe at the similarity with templates:</p>
<pre class="objcpp code" id="cpp-vs-objc-example">std::vector&lt;Duck&gt; ducks;
Chicken chicken;
ducks.push_back(chicken);             // Error
ducks.push_back(*(Duck *)chicken);    // Horrible crash here or some time in the future, maybe.

NSArray[Duck] *ducks = [NSArray new];
Chicken *chicken = [Chicken new];
[ducks addObject:chicken];            // Warning: type mismatch
[ducks addObject:(Duck *)chicken];    // No problem, unless you call -quack on it.
</pre>
<p>While the difference should hopefully be clear by now, I’ll spell it out: in C++, <code>std::vector&lt;Duck&gt;</code> creates an entirely new class (with bits of <code>Duck</code> inlined into it). The Objective-C-with-generics version only provides hints to the compiler, so it can catch mistakes. It doesn’t stop you from putting chickens among your ducks, or make the duck array reject chickens at runtime, or generate a new array-of-chickens class.</p>
<p>In <a href="http://lists.apple.com/archives/objc-language/2003/Dec/msg00084.html">earlier discussions</a> of generics, it has been stated that this type of mistake is rare in practice and should be caught with unit tests. If you feel that way, you’re welcome to stick to your current approach, and the introduction of generics, as described, will not affect you.</p>
<p>To avoid ballooning side effects of generics, I’ve deliberately avoided suggesting generic functions and methods (i.e., ones whose return type is dependent on one or more argument types, independent of their class in the case of methods).</p>
<h2 id="one-last-thing">One Last Thing</h2>
<p>An effect of the above is that I want the language to contain types like <code>NSArray[NSMutableDictionary[NSString, NSSet[NSMutableArray[NSNumber]]]] *</code>. That doesn’t mean I want to spend my time <em>typing</em> <code>NSArray[NSMutableDictionary[NSString, NSSet[NSMutableArray[NSNumber]]]] *</code>, even with autocomplete. Fortunately, there exists a well-known solution to this problem: type inference.</p>
<p>If I access a member of an array of the aforementioned sesquipedalian type, I get an <code>NSMutableDictionary[NSString, NSSet[NSMutableArray[NSNumber]]] *</code>. What’s more, the compiler knows this. The benefit of static typing lies primarily in checking what I do with my dictionary, rather than checking that the item I retrieved is what I thought it was, so I should be able to ask it to type a variable appropriately.</p>
<p>I quite like C++1x’s solution of recycling the <code>auto</code> keyword for this use, but that would conflict with Objective-C’s goal of being a strict superset of C. There are various other choices, such as <code>var</code> or <code>any</code> — or maybe <code>ego</code>. In any case, type inference would lead to code like:</p>
<pre class="objcpp code" id="type-inference-example">typedef NSArray[NSMutableDictionary[NSString, NSSet[NSMutableArray[NSNumber]]]] MyHorribleNestedType;  // TODO: replace with sensible model classes.

MyHorribleNestedType *array = whatever;
var element = [array objectAtIndex:0];              // Type is inferred as NSMutableDictionary[NSString, NSSet[NSMutableArray[NSNumber]]]*
var subEntry = [NSArray arrayWithObject:@"bloop"];  // Type is inferred as NSArray[NSString]
[element setObject:subEntry forKey:@"moop"];        // Type mismatch: expected NSSet[NSMutableArray[NSNumber]], got NSArray[NSString]</pre>
<p>A subtlety here: the return type of <code>[NSArray arrayWithObject:@"bloop"]</code> (declared <a href="#decl-arrayWithObject">previously</a>) is <code>[:Self[Item]]</code>, which is inferred from the receiver (the <code>NSArray</code> class object) and argument to resolve to <code>NSArray[NSString]</code>. Instead of <code>typedef</code>ing <code>MyHorribleNestedType</code>, we could have constructed the type implicitly in the same way. like everything else in the proposal, this is optional; if you used <code>id</code> or explicit, unparameterized types in this example, the type mismatch would not be detected, but the generated code would be identical.</p>
<h2 id="summary">Summary</h2>
<ul>
<li> The <code>id</code> type is very powerful and flexible. However, most of the time you don’t need this flexibility, and type checking is helpful — this is why Objective-C has optional static type checking in the first place, and almost all Objective-C code opts in to it.</li>
<li> The proposal would not impose any new restrictions, or any new guarantees. It would only extend (optional) static type checking to cases which are currently not covered.</li>
<li> It would not involve any implicit code generation, and no runtime overhead.</li>
<li> Combined with type inference, it could catch errors without additional code.</li>
<li> There would be no new collections, and no implementation changes to existing ones other than updated type declarations. (Updated headers would work with the existing implementations.)</li>
<li> If you really don’t want static type checking, you can continue using <code>id</code> everywhere.</li>
<li> I mean it about the “optional”. The proposed change would have no effect unless adopted both by a class and its clients. A parameterized collection pointer can be cast to plain one at any time with no cost.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/generics-for-objective-c/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Multi-type Save Panel Controller</title>
		<link>http://jens.ayton.se/blag/multi-type-save-panel-controller/</link>
		<comments>http://jens.ayton.se/blag/multi-type-save-panel-controller/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 19:35:20 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=68</guid>
		<description><![CDATA[As a change of pace, I thought I’d post some code that doesn’t go out of its way to be bad. JAMultiTypeSavePanelController is a class (abstracted from ImageIO Export for Acorn) to handle the case where you want to offer &#8230; <a href="http://jens.ayton.se/blag/multi-type-save-panel-controller/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As a change of pace, I thought I’d post some code that doesn’t go out of its way to be bad.</p>
<p><code>JAMultiTypeSavePanelController</code> is a class (abstracted from <a href="http://jens.ayton.se/acorn/">ImageIO Export for Acorn</a>) to handle the case where you want to offer the user a choice of formats to save in.</p>
<p><img src="http://jens.ayton.se/blag/wp-content/uploads/2009/08/JAMultiTypeSavePanelController.png" alt="Save panel" border="0" style="max-width: 100%" /><br />
<span id="more-68"></span>
<p>It wraps an <code>NSSavePanel</code> and handles all the UTI and extension details. You can provide a customised <code>NSSavePanel</code> or let the controller create one. The only restriction is that the controller appropriates the accessory view. Usage is similar to <code>NSSavePanel</code> itself:</p>
<pre class="objc-code">NSArray *types =
    [NSArray arrayWithObjects:@"com.apple.rtfd", @"public.rtf", @"public.plain-text", nil];
JAMultiTypeSavePanelController *controller =
    [JAMultiTypeSavePanelController controllerWithSupportedUTIs:types];
controller.autoSaveSelectedUTIKey = @"document format"; // Optional: preferences key for selected type.
controller.sortTypesByName = NO; // Optional: show in specified order instead of alphabetically by display name.
[controller beginSheetForDirectory:nil
                              file:@"untitled 42"
                    modalForWindow:docWindow
                     modalDelegate:self
                    didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
                       contextInfo:nil];</pre>
<p><code>-runModalForDirectory:file:</code> is also supported.</p>
<p><a href="http://code.google.com/p/ahruman/source/browse/#svn/trunk/Widgets/JAMultiTypeSavePanelController">Code</a> (MIT/X11 license)</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/multi-type-save-panel-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wanted: a Right-Handed Keyboard</title>
		<link>http://jens.ayton.se/blag/right-handed-keyboard/</link>
		<comments>http://jens.ayton.se/blag/right-handed-keyboard/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 23:49:06 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=61</guid>
		<description><![CDATA[My current keyboard looks like this: (Well, roughly. It actually looks like this, but I couldn’t find a decent-resolution Swedish one.) This was a sensible design for the right-handed majority when it was introduced, some time in the stone age. &#8230; <a href="http://jens.ayton.se/blag/right-handed-keyboard/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My current keyboard looks like this:</p>
<p><img src="http://jens.ayton.se/blag/wp-content/uploads/2009/06/left-handed-keyboard.jpg" alt="Left-handed keyboard: keypad on right, empty space on left." border="0" style="max-width: 100%" /></p>
<p>(Well, roughly. It actually looks like <a href="http://jens.ayton.se/blag/wp-content/uploads/2009/06/swedish-keyboard.jpg">this</a>, but I couldn’t find a decent-resolution Swedish one.)</p>
<p>This was a sensible design for the right-handed majority when it was introduced, some time in the stone age. However, since then, something quite important has happened: the keyboard has been joined by another input device, which I suspect is used more than the keypad by most people who are not cursed with an Excel-dominated career.</p>
<p><span id="more-61"></span>
<p>The ergonomics of the extended keyboard + mouse combination is quite, quite horrible. The human arm is not optimized for sitting turned out 30&nbsp;degrees for hours at a time.</p>
<p>Apple has tried at least twice to solve this in one obvious way: remove all or part of the junk on the right. This has been widely, or at least loudly, decried, leading to the Extended ADB Keyboard for high-end Macs in the deep past and to extended keyboards for all in the USB era. There is another solution, though:</p>
<p><img src="http://jens.ayton.se/blag/wp-content/uploads/2009/06/right-handed-keyboard.jpg" alt="Right-handed keyboard: keypad on left, empty space on right." border="0" style="max-width: 100%" /></p>
<p>Unfortunately, day-to-day conservatism means that people, in the aggregate, will loudly complain about having to adopt a superior solution if it involves half an hour of retraining. I’m sure many would point out that this is why we still have rectangular QWERTY keyboards in the first place. Still, if anyone can force change down our throats and make us like it, it’s Apple.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/right-handed-keyboard/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Net Neutrality (EU edition)</title>
		<link>http://jens.ayton.se/blag/net-neutrality/</link>
		<comments>http://jens.ayton.se/blag/net-neutrality/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 23:28:54 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[Net Neutrality]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=54</guid>
		<description><![CDATA[Network neutrality in Europe is under threat. Not in some vague possible future; right now. In one week, on Tuesday the fifth of May, the European Parliament will vote on the second reading of the Telecoms Package and amendments. I &#8230; <a href="http://jens.ayton.se/blag/net-neutrality/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Network neutrality in Europe is under threat. Not in some vague possible future; <em>right now</em>. In one week, on Tuesday the fifth of May, the European Parliament will vote on the second reading of the Telecoms Package and amendments.</p>
<p>I urge all EU citizens to contact an MEP, via e-mail or (preferably) phone, and encourage them to support the Citizens’ Rights Amendments, which reaffirm net neutrality and anti-censorship positions adopted by the European Parliament in the first reading but subsequently removed by the Council of Ministers in their “common position”.</p>
<p>The primary purpose of these amendments is to make it explicit that the European Convention on Human Rights applies to internet and telecoms legislation. This should be obvious, but it is clear from the actions of politicians that it is not; it is all too clear that many see the internet as a frivolous toy, and civil rights obviously do not apply to toys. (It occurs to me that politicians are probably among those in the western world least affected by the digital communications revolution; if they need to communicate, they talk to their secretaries.)</p>
<p>Draft texts of the amendments (not yet numbered) can be found here: <a href="http://www.erikjosefsson.eu/sites/default/files/Citizens_Rights_Amendments_(Part_I).pdf">part I</a>, <a href="http://www.erikjosefsson.eu/sites/default/files/Citizens_Rights_Amendments_(Part_II).pdf">part II</a>, <a href="http://www.erikjosefsson.eu/sites/default/files/Citizens_Rights_Amendments_(Part_III).pdf">part III</a>. There’s a (rather bad) campaign site <a href="http://www.blackouteurope.eu/act/tell-the-parliament-to-vote-yes.html">here</a>, and a less bad editorial – for all that it’s in a pointy-haired e-mag – in Computer World UK <a href="http://www.computerworlduk.com/community/blogs/index.cfm?entryid=2140&#038;blogid=14">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/net-neutrality/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Constant objects for fun and non-profit</title>
		<link>http://jens.ayton.se/blag/objc-constant-objects/</link>
		<comments>http://jens.ayton.se/blag/objc-constant-objects/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 02:27:05 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[evil]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=45</guid>
		<description><![CDATA[The @&#34;foo&#34; operator for constant NSString objects in Objective-C is extremely convenient. Indeed, if it wasn’t there, programming with Cocoa would be a royal pain. Many of us have at various points wished there was equivalent syntax for NSNumbers, and &#8230; <a href="http://jens.ayton.se/blag/objc-constant-objects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <code>@&quot;foo&quot;</code> operator for constant <code>NSString</code> objects in Objective-C is extremely convenient. Indeed, if it wasn’t there, programming with Cocoa would be a royal pain. Many of us have at various points wished there was equivalent syntax for <code>NSNumber</code>s, and possibly collections.</p>
<p>As an early Christmas present to fellow lovers of twisted, evil code that should never have seen the light of day under any circumstances whatsoever, I hereby present an implementation of the first half of that wish.</p>
<p><span id="more-45"></span><br />
<h2>Caveat lector</h2>
<p>Now, when I say it’s evil, I really, really mean it. We’re talking about building objects in the data section of your code by packing structs, thus depending on data layout (and as such, depending on the old 32-bit runtime). We’re talking about nested macros with lots of <code>?:</code>s in them. We’re talking about gcc’s built-in parser pseudo-functions. Hearken to the words of Greg Parker, official Objective-C Runtime Wrangler at Apple:</p>
<p style="margin:0 3em">@<a href="https://twitter.com/gparker/status/1051085472">gparker</a>: @ahruman JANumberLiteral would break in several exciting ways in 64-bit. Might almost be safe enough in 32-bit, though.<br />
@<a href="https://twitter.com/ahruman/status/1051088370">ahruman</a>: @gparker Yeah, I know. I’ll be pasting “If you do this in real life, I’ll have to track you down and shoot you” warnings all over it. :-)<br />
@<a href="https://twitter.com/gparker/status/1052054292">gparker</a>: @ahruman Don&apos;t shoot &apos;em, just maim &apos;em a bit. I want my turn after you&apos;re done.<br />
@<a href="https://twitter.com/ahruman/status/1052079228">ahruman</a>: @gparker That’s what kneecaps were invented for.</p>
<p>Be told.</o></p>
<h2>Overview</h2>
<p>So, all that aside… what we’re looking at here is a macro <code>$N(n)</code> which takes a number and returns an <code>NSNumber</code>. (The convention of using dollar signs for macros implementing desired language features was introduced by <a href="http://mooseyard.com/Jens/">the other Jens A</a>.) If passed a compile-time constant, it will produce a constant object, i.e. a single, immortal instance based in the data section with no instantiation overhead, just like a string object constant. If passed a non-constant, it will give you a normal autoreleased <code>NSNumber</code> instead.</p>
<p>Constant <code>NSString</code>s work by creating a struct with the following layout: <code>{&nbsp;&amp;__CFConstantStringClassReference, 1992, &quot;foo&quot;, 3&nbsp;}</code>, where 1992 is a flag field and 3 is the length of the string. (Note that this is not the <code>NSConstantString</code> from the bottom of NSString.h. I believe this change was made in the OS X 10.2 SDK.) <code>__CFConstantStringClassReference</code> is a symbol exported from CoreFoundation.framework and implicitly declared in all Objective-C or Objective-C++ files by the compiler.<small><sup><a href="#objc-constant-objects-footnote-1" name="objc-constant-objects-footnote-ref-1">1</a></sup></small> My task, once I chose to accept it, was to do something similar for <code>NSNumber</code>.</p>
<p>Trying to build actual <code>NSCFNumber</code>s would be an unnecessary extra evil. Instead, I subclassed the abstract <code>NSNumber</code> twice, once for <code>long&nbsp;long</code>s and once for <code>double</code>s, giving me a known layout to target. The subclasses also override <code>-retain</code>, <code>-release</code> and <code>-autorelease</code> to do nothing, in the manner of a singleton. Then, building an instance is conceptually simple:</p>
<pre class="objc-code">const static struct { Class isa; double value; } fakeObj =
{ [JAFloatNumber class], 42.0 };
NSNumber *number = (NSNumber *)&fakeObj;</pre>
<p>The structure needs to be declared <code>static</code> so that it doesn’t evaporate at the end of the containing scope. This presents a dilemma, however: static initializers must be constant expressions, and a method call doesn’t qualify. Besides, we want the object to be constant data.</p>
<p>Luckily for us, classes are represented by global symbols like any other. Well, almost like any other. Unlike symbols based on C identifiers, their linker symbols don’t start with an underscore but with a dot, meaning you can’t reference them the usual way. Fortunately, gcc provides us with a solution in the form of <a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/Asm-Labels.html">asm labels</a>:</p>
<pre class="objc-code">extern const struct objc_class JAFloatLiteralClassObject
        __asm__(&quot;.objc_class_name_JAFloatNumber&quot;);</pre>
<p>The basic implementation then becomes:</p>
<pre>#define JANUMBERLITERAL_CONSTANT_FLOAT(n) \
        ({ static const struct { const struct objc_class *isa; double value; } \
        object = { &#038;JAFloatLiteralClassObject, (n) }; \
        (NSNumber *)&object; })</pre>
<p>A problem here is that, despite all those <code>const</code>s, a non-const value can be used in Objective-C++. (In plain Objective-C, you get a moderately helpful warning about non-const initializers.) If you write a function that returns <code> JANUMBERLITERAL_CONSTANT_FLOAT(random());</code>, each instance will return the same object but its value will change, which is rather bad. The fix is to wrap this up in a macro that uses <code><a href="http://gcc.gnu.org/onlinedocs/gcc-4.2.0/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fconstant_005fp-2458">__builtin_constant_p()</a></code> to ensure constant objects are only created with constant values. For non-constant values, normal autoreleased <code>NSNumber</code>s are created instead.</p>
<h2>Results</h2>
<p>The above pieces, along with a bunch of extra preprocessor stuff to handle the distinctions between Objective-C and Objective-C++ and between integer and floating point values gives us a <code>$N(n)</code> macro which works as advertised: if given an integer constant it returns a <code>JAIntegerNumber</code>, for a floating point constant it returns a <code>JAFloatNumber</code>, and for a non-constant it returns an autoreleased <code>NSCFNumber</code>. But is it really working as intended? The following code:</p>
<pre class="objc-code">static NSNumber *GetNumber(void)
{
    return $N(42.0);
}</pre>
<p>disassembles to:</p>
<pre class="asm-code">    .const_data
    .align 2
_object.67163:
    .long   .objc_class_name_JAFloatNumber  // isa
    .long   0           // low bytes of 42.0
    .long   1078263808  // high bytes of 42.0</pre>
<p>…with the actual use of the object inlined (in an optimized build) to <code>movl $_object.67163, (%esp)</code>. The values 0 and 1078263808 correspond to the little-endian hex value of 42.0: <code>00000000 00004540</code>. In other words, this really is an object as constant data. For comparison, the <code>NSString</code> literal used in the sample code disassembles to:</p>
<pre class="asm-code">    .cstring
LC0:
    .ascii &quot;Hello, World! %@ = %li (%@)\0&quot;
    .section __DATA, __cfstring
    .align 2
LC1:
    .long   ___CFConstantStringClassReference // isa
    .long   1992  // flags
    .long   LC0   // bytes pointer
    .long   27    // length</pre>
<h2>Conclusion</h2>
<p>So there you have it: fully-functional, constant <code>NSNumber</code> objects, as long as you don’t use the 64-bit runtime. Now don’t go actually using it, or I’ll have to shoot your kneecaps off and leave you to Greg.</p>
<p>Oh yeah, the code is <a href="http://code.google.com/p/ahruman/source/browse/#svn/trunk/Stupid/NumberLiteral">on Google Code</a>. For a much simpler macro that just gives you an <code>NSNumber</code>, automatically choosing between integer and floating-point representations, try <a href="http://www.extinguishedscholar.com/wpglob/?p=346">natevw’s</a>.</p>
<hr />
<p><small><sup><a href="#objc-constant-objects-footnote-ref-1" name="objc-constant-objects-footnote-1">^1</a></sup></small> Its value, on my system, is 0xa00034a0. Traditionally (pre Leopard), a value above 0xFFF would mean it’s a real Objective-C class rather than a CF type tag, but that test is no longer in the <a href="http://www.opensource.apple.com/darwinsource/10.5.5/CF-476.15/">CFLite source</a> in Leopard, so it may have changed.</p>
<p>If you declare <code>extern const Class __CFConstantStringClassReference;</code> in an Objective-C++ file, you’ll get the error “&apos;__CFConstantStringClassReference&apos; has a previous declaration as &apos;int __CFConstantStringClassReference []&apos;”. In Objective-C, you won’t be told a type, just that it was “previously declared here”, referring to the location “&lt;built-in>:0”.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/objc-constant-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Fun with the Objective-C Runtime</title>
		<link>http://jens.ayton.se/blag/fun-with-the-objc-runtime/</link>
		<comments>http://jens.ayton.se/blag/fun-with-the-objc-runtime/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 21:32:32 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=41</guid>
		<description><![CDATA[Ever since Leopard came out, I’ve wanted to do something useful with resolveInstanceMethod:. The opportunity has yet to present itself. However, I have done a couple of really silly things with it, which have until now languished in obscurity in &#8230; <a href="http://jens.ayton.se/blag/fun-with-the-objc-runtime/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ever since Leopard came out, I’ve wanted to do something useful with <code>resolveInstanceMethod:</code>. The opportunity has yet to present itself. However, I have done a couple of really silly things with it, which have until now languished in obscurity in the depths of <a href="http://paste.lisp.org/">paste.lisp.org</a>. So here they are.<span id="more-41"></span><br />
<h2>Easy Dictionary</h2>
<p>This allows you to use arbitrary accessors on dictionaries, or arbitrary properties if you declare them first (no definition necessary). Example:</p>
<pre class="objc-code">@interface NSDictionary (MyProperties)
@property (retain) NSString *fruit;
@end
...
NSMutableDictionary *myDict = [NSMutableDictionary dictionary];
dict.fruit = @"apple";  // -setFruit: method is generated dynamically.</pre>
<p><a href="http://code.google.com/p/ahruman/source/browse/#svn/trunk/Stupid/EasyDictionary">Google Code</a>, <a href="http://paste.lisp.org/display/55627">original lisppaste</a></p>
<h2>Silly String</h2>
<p>This defines a new string concatenation operator, <code>:</code>. Well, almost. It allows you to append any number of strings (or arbitrary object descriptions) to a string using the following syntax:</p>
<pre class="objc-code">[@"string " : @"other string " : arbitraryObject : @" yet another string "]</pre>
<p>Unfortunately, a header is required in this case to avoid unknown selector warnings.<br />
<a href="http://code.google.com/p/ahruman/source/browse/#svn/trunk/Stupid/SillyString">Google Code</a>, <a href="http://paste.lisp.org/display/71193">original lisppaste</a></p>
<h2>Disclaimer</h2>
<p>As noted in each of the files, I don’t recommend actually using these. They’re exercises in understanding the mechanics of the language rather than actually useful utilities. In particular, if you use Easy Dictionary you’re in for a world of hurt when you try to use a property that <code>NSDictionary</code> already has, like <code>count</code>, or <code>description</code>, or <code>release</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/fun-with-the-objc-runtime/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Migratory Code</title>
		<link>http://jens.ayton.se/blag/migratory-code/</link>
		<comments>http://jens.ayton.se/blag/migratory-code/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 20:09:21 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[google code]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=39</guid>
		<description><![CDATA[Of the two people I know of who have tried to get at some of my code releases in the past year, a full 50&#160;% have given up and written a new implementation from scratch because downloading and opening a &#8230; <a href="http://jens.ayton.se/blag/migratory-code/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Of the two people I know of who have tried to get at some of my code releases in the past year, a full 50&nbsp;% have given up and written a new implementation from scratch because <a href="http://laprej.blogspot.com/2008/07/k-one-more-problem.html">downloading and opening a zip file is too much work</a> (as is actually complaining to someone who can fix it). On top of that, some form of version control is good to have, and my local set-ups keep breaking when I do silly things like getting a new computer or installing a new OS. So now most of my released code lives at <a href="http://ahruman.googlecode.com/">Google Code</a>. Yay and stuff.</p>
<p>Specifically, the projects in question are:</p>
<ul>
<li><a href="http://jens.ayton.se/blag/blag/glsl-uniform-bindings-for-cocoa/">GLSL Bindings Sample</a></li>
<li><a href="http://jens.ayton.se/acorn/">ImageIO Export plug-in</a> for <a href="http://www.flyingmeat.com/acorn/">Acorn</a>.</li>
<li><a href="http://jens.ayton.se/blag/blag/priority-queue/">JAPriorityQueue</a></li>
<li><a href="http://jens.ayton.se/blag/property-list-extractors/">JAPropertyListAccessors</a>
<li><a href="http://jens.ayton.se/blag/blag/key-to-qwerty/">KeyFromQWERTY</a></li>
<li><a href="http://jens.ayton.se/code/keynaming/">KeyNaming</a></li>
<li>S3TCDecompression</li>
<li>SCDialControl</li>
<li>SCSliderWithTextField</li>
</ul>
<p>For information about the various code bits and bobs, see <a href="http://jens.ayton.se/code/">here</a>.</p>
<p>I fully expect that the next person who tries to grab some of my code will find it to onerous to dig through the subversion repo. To that person, my message is: tough.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/migratory-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Key to QWERTY</title>
		<link>http://jens.ayton.se/blag/key-to-qwerty/</link>
		<comments>http://jens.ayton.se/blag/key-to-qwerty/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 11:29:13 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=34</guid>
		<description><![CDATA[Recently on the Twitterwebs, Peter Hosey asked for a library that takes a character and returns the corresponding virtual key code (implicitly, in the U.S. keyboard layout). The intention was to use it to specify default key bindings when binding &#8230; <a href="http://jens.ayton.se/blag/key-to-qwerty/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently on the Twitterwebs, Peter Hosey <a href="https://twitter.com/boredzo/statuses/915231478">asked</a> <a href="https://twitter.com/boredzo/statuses/915251187">for</a> a library that takes a character and returns the corresponding virtual key code (implicitly, in the U.S. keyboard layout). The intention was to use it to specify default key bindings when binding to physical keys rather than characters.</p>
<p>Now personally, I like to specify my defaults in plists, and if I was hard-coding them I’d use an enum. However, I thought of a mildly amusing implementation, so I <a href="http://code.google.com/p/ahruman/source/browse/#svn/tags/Misc/KeyFromQWERTY/v1.0">wrote it</a> (<a href="http://opensource.org/licenses/mit-license.html">MIT/X11 license</a>). It turned out to be less interestingly ugly than I expected, but did give me an opportunity to play with <code><a href="http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Other-Builtins.html#index-g_t_005f_005fbuiltin_005fconstant_005fp-2757">__builtin_constant_p</a>()</code>. This means that (if you’re using gcc) <code>KEY_FROM_QWERTY_BODY(ex)</code>, where <code>ex</code> is a constant expression, will itself be a constant expression, and if <code>ex</code> is otherwise known at compile time to be constant (e.g. a locally-defined <code>const int</code>, which is not a constant expression in C) it will be constant-folded; in other cases, a function call will be generated.</p>
<p>Also included is code to generate the header, specifically the big <code>?:</code>-based expression that does the work, using whatever keyboard layout happens to be active. Note, however, that the current implementation only includes keys that generate a single ASCII character, and skips dead keys. It includes capital letters as aliases for their lowercase equivalents, but not other shifted keys.</p>
<p>Conveniently, I’ve also written <a href="http://jens.ayton.se/code/keynaming/" title="KeyNaming">a library</a> to get a display string for a virtual key code. That one <em>does</em> use the user’s current keyboard layout.</p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/key-to-qwerty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Ubiquitous Wiretapping Bill</title>
		<link>http://jens.ayton.se/blag/lex-orwell/</link>
		<comments>http://jens.ayton.se/blag/lex-orwell/#comments</comments>
		<pubDate>Fri, 06 Jun 2008 11:41:20 +0000</pubDate>
		<dc:creator>Jens Ayton</dc:creator>
				<category><![CDATA[Politics]]></category>

		<guid isPermaLink="false">http://jens.ayton.se/blag/?p=23</guid>
		<description><![CDATA[This is not about programming. It’s about something that actually matters. There’s been a little media coverage recently about a Swedish law that’s been in the works for a while. (The Register, Slashdot on The Register, The Local Edit: and &#8230; <a href="http://jens.ayton.se/blag/lex-orwell/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="float:right"><a href="http://www.stoppafralagen.nu/"><img src="http://jens.ayton.se/globalimage/fra-avlyssning.jpg" alt="Krig är inte fred. Slaveri är inte frihet. Avlyssning är inte integritet. 17 juni." title="War is not peace. Slavery is not freedom. Surveillance is not personal integrity. 17 June"></a></div>
<p>This is not about programming. It’s about something that actually matters.</p>
<p>There’s been a little media coverage recently about a Swedish law that’s been in the works for a while. (<a href="http://www.theregister.co.uk/2008/06/04/sweden_wiretap_bill/">The Register</a>, <a href="http://yro.slashdot.org/article.pl?sid=08/06/05/037201">Slashdot on The Register</a>, <a href="http://www.thelocal.se/12252/20080605/">The Local</a> <b>Edit:</b> and <a href="http://www.pcworld.com/businesscenter/article/146750/swedish_govt_to_vote_on_allowing_email_phone_monitoring.html">IDG/PCWorld</a>, <a href="http://www.edri.org/edrigram/number6.11/nsa-fra-sweden"> European Digital Rights</a>, <a href="http://arstechnica.com/news.ars/post/20080606-swedish-government-may-soon-get-power-to-spy-on-its-citizens.html">Ars Technica</a>; new <a href="http://www.stoppafralagen.nu/">portal site</a> in Swedish). The law will permit the National Defense Radio Establishment (FRA, Försvarets radioanstalt) to carry out surveillance over the air and in cables crossing the Swedish border.</p>
<p>Surveillance over the air has been carried out since the 1940s, and satellite surveillance since 1976. This has been unconstitutional since 1995, when the European Convention on Human Rights was adopted as part of the Swedish constitution. The former second-in-command of FRA, Anders Wik, admitted this in a tape recording made public <a href="http://english.rickfalkvinge.se/2008/06/04/more-on-the-ubiquitous-wiretapping-bill/">last saturday</a>. This has received almost no attention in the Swedish press. Creating a law that permits the activity will make it legal under the EHCR<a class="footnote-ref" href="#lex-orwell-footnote-1" id="lex-orwell-footnote-ref-1">¹</a>.</p>
<p>The law was originally drafted by the Social Democratic Party, but withdrawn because it “received an unfortunate amount of attention”. It was reintroduced by the current four-party alliance in late 2006 (with a quick consultation carried out in the week between Christmas and New Year), but was suspended for one year by a rare minority veto instigated by the Social Democrats, who suddenly felt it should be adjusted to provide more checks and balances.</p>
<p>Defenders of the new law point out that it specifies that “military intelligence activity” [försvarsunderrättelseverksamhet] may only target “foreign matters” [utrikes förhållanden]. That restriction is actually already in place. However, it is the stated legal opinion of FRA that when they work for non-military authorities, such as the police, the customs office or the tax board, this is not “military intelligence activity” and is therefore not restricted to “foreign matters”. Besides that, determining that, for instance, an e-mail message is to or from a Swede involves analyzing it; as pointed out by the <a href="http://en.wikipedia.org/wiki/Council_on_Legislation_%28Sweden%29">Council on Legislation</a>, personal integrity is violated by opening the envelope, not just by copying the contents.</p>
<p>The obvious problem with this type of legislation is that it can easily be abused, especially when it’s written in very broad terms. However, there is another problem: even if it never is abused, the <em>very existence</em> of surveillance is oppressive and changes people’s patterns of communication.</p>
<p>A <a href="http://www.kreativrauschen.com/blog/2008/06/04/data-retention-effectively-changes-the-behavior-of-citizens-in-germany/">recent survey</a> from Germany shows that a large portion of the population has changed or under certain circumstances would change their behaviour due to Germany’s implementation of the <a href="http://en.wikipedia.org/wiki/Data_retention#Data_retention_in_the_European_Union">data retention directive</a>. This “only” involves storing information about the participants in communication and their location. A law that allows essentially arbitrary civil authorities to order searches of the <em>contents</em> of people’s communication can be reasonably expected to affect behaviour more extensively. As such, citizens’ freedom of expression is curtailed. Without a functioning freedom of expression, democracy means nothing.</p>
<p>The proposed law is not a technical adjustment of foreign intelligence activity, as its defenders wish us to believe. It is a direct assault on democracy itself. It is expected to pass with a seven-vote majority eleven days from now, unless we can once again raise an “unfortunate amount of attention.”</p>
<p><b>Footnote:</b> I know that we’re already being spied on by foreign agencies, but that’s less important. Foreign governments are not in a position to oppress us, except by taking over the government of Sweden. This is reflected by the <a href="http://www.riksdagen.se/templates/R_PageExtended____6319.aspx">Swedish bill of rights</a>, which in several cases protects<a class="footnote-ref" href="#lex-orwell-footnote-2" id="lex-orwell-footnote-ref-2">²</a> the citizen “in his relations with the public institutions” [gentemot det allmänna].</p>
<p><small><a class="footnote" href="#lex-orwell-footnote-ref-1" id="lex-orwell-footnote-1">¹</a> If you accept that it is “necessary in a democratic society in the interests of national security, public safety or the economic well-being of the country, for the prevention of disorder or crime, for the protection of health or morals, or for the protection of the rights and freedoms of others.”<br />
<a class="footnote" href="#lex-orwell-footnote-ref-2" id="lex-orwell-footnote-2">²</a> Sort of. Except for the EHCR, the Swedish constitution is not enforceable.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://jens.ayton.se/blag/lex-orwell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
