<?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>Cappuccino Blog &#187; Objective-J</title>
	<atom:link href="http://cappuccino.org/discuss/category/objective-j/feed/" rel="self" type="application/rss+xml" />
	<link>http://cappuccino.org/discuss</link>
	<description>Home of Cappuccino and Objective-J</description>
	<lastBuildDate>Thu, 19 Aug 2010 01:56:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Solving the JavaScript Memory Management Problem</title>
		<link>http://cappuccino.org/discuss/2010/04/01/solving-the-javascript-memory-management-problem/</link>
		<comments>http://cappuccino.org/discuss/2010/04/01/solving-the-javascript-memory-management-problem/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 00:01:58 +0000</pubDate>
		<dc:creator>ross</dc:creator>
				<category><![CDATA[Cappuccino]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Objective-J]]></category>

		<guid isPermaLink="false">http://cappuccino.org/discuss/?p=606</guid>
		<description><![CDATA[JavaScript has some real problems. Anyone who has programmed for the web can attest to that. And anyone familiar with what we&#8217;re doing with Cappuccino will recognize that we&#8217;re not afraid to tackle those problems head on. When we created Objective-J we set out to create a set of true enhancements to the JavaScript language [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript has some real problems. Anyone who has programmed for the web can attest to that. And anyone familiar with what we&#8217;re doing with Cappuccino will recognize that we&#8217;re not afraid to tackle those problems head on. When we created Objective-J we set out to create a set of true enhancements to the JavaScript language that would add powerful new dynamic features to the language, like advanced automatic dependency resolution, truly dynamic message passing, and familiar classical inheritance. By all accounts it has been a tremendous success.</p>
<p>Lately, though, we&#8217;re beginning to realize that we didn&#8217;t quite go far enough. Memory issues have long plagued JavaScript developers. Because the garbage collector is opaque to the developer, and nothing like &#8220;finalize&#8221; is provided by the language, programmers often find themselves in situations where they are forced to hold on to an object reference for too long (or forever) creating a memory leak.</p>
<p>Just as we&#8217;ve done before, when we tackled this problem we decided to look at what has worked in the past. Finding inspiration in the elegant simplicity of C, we knew we&#8217;d found the right solution. And so, we&#8217;re pleased to announce the immediate availability of a manual memory management system in Cappuccino.</p>
<p>A lot of Cocoa developers disparaged garbage collection when it came to Objective-C, and we&#8217;ve come to realize they were absolutely right! Why rely on machines to solve problems for us when we can do it by hand. So, we&#8217;ve taken the Cocoa reference counting system and implemented it in JavaScript. We&#8217;ve replaced the existing no-op -retain, -release, and -autorelease methods with fully working implementations. When an object&#8217;s release count reaches 0, the object&#8217;s dealloc method will be called, and the memory will be able to be reclaimed. Now you have a clear way to clean up unwanted references when an object is no longer being used. Just make sure you do your reference counting correctly!</p>
<p>To do this right, we&#8217;ve created a global object table. These objects aren&#8217;t going anywhere on their own! If you don&#8217;t release an object, it will stick around forever, ensuring you&#8217;ll always have it when you need it. This global table acts as a lookup table for any object, allowing us to finally implement pointers in JavaScript. Since $ has become the coolest way to do completely non-standard lookups in an almost indecipherable way, we thought we&#8217;d jump on the bandwagon. Pass any pointer to $ and you&#8217;ll get the associated object! How do you get a pointer you ask? $$ of course! For example:</p>
<pre><code>
var o = [CPobject new]; // +new returns with a retain count of 1!
var p = $$(o); // returns a pointer to o

o === $(p); // we dereference the pointer and get the right object!</code></pre>
<p>Zombies come standard, too! Just set OBJJ_ZOMBIE_DETECTION to true and objj_msgSend will throw an exception if you ever message an object that&#8217;s been dealloc&#8217;d. </p>
<p>We&#8217;ve even take the time to properly -autorelease all objects returned from class methods in Foundation, but we&#8217;ve made absolutely no effort to properly retain those objects in Cappuccino code that uses them yet. So we need your help. Every line must be scrutinized (but we shouldn&#8217;t to automate this process, as it would surely be too error prone). Rather than focus on developing new features or fixing bugs in the issue tracker, we hope that all Cappuccino contributors will spend their time writing memory management code so that we can fully realize this web development revolution!</p>
<p>We&#8217;ll be including these exciting new features in a future release, so you should get started adding manual memory management code to every single line of Cappuccino you&#8217;ve ever written! In the meantime, check out the <a href="http://github.com/280north/cappuccino/commit/6678bba94f7778b245d8219292b3a6ac85d31678">working code on Github</a>.</p>
<p><em>Please note this is an April Fools joke!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://cappuccino.org/discuss/2010/04/01/solving-the-javascript-memory-management-problem/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Announcing Atlas</title>
		<link>http://cappuccino.org/discuss/2009/02/28/announcing-atlas/</link>
		<comments>http://cappuccino.org/discuss/2009/02/28/announcing-atlas/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 03:55:48 +0000</pubDate>
		<dc:creator>tlrobinson</dc:creator>
				<category><![CDATA[280 North]]></category>
		<category><![CDATA[Cappuccino]]></category>
		<category><![CDATA[Objective-J]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[280north]]></category>
		<category><![CDATA[atlas]]></category>

		<guid isPermaLink="false">http://cappuccino.org/discuss/?p=301</guid>
		<description><![CDATA[Over at 280 North, we announced our next product, called Atlas, at the Future of Web Apps conference in Miami this week. Atlas is a visual development tool for creating web applications using the Cappuccino framework. The best way to explain Atlas is to show it: Think Vitamin has an article discussing Atlas in more [...]]]></description>
			<content:encoded><![CDATA[<p>Over at <a href="http://280north.com">280 North</a>, we announced our next product, called Atlas, at the <a href="http://events.carsonified.com/fowa">Future of Web Apps</a> conference in Miami this week.</p>
<p>Atlas is a visual development tool for creating web applications using the <a href="http://cappuccino.org">Cappuccino</a> framework. The best way to explain Atlas is to show it:</p>
<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="545" height="361" id="viddler_1db9bf4d"><param name="wmode" value="transparent" /><param name="movie" value="http://www.viddler.com/simple/1db9bf4d/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/simple/1db9bf4d/" width="545" height="361" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_1db9bf4d" wmode="transparent"></embed></object></p>
<p><a href="http://thinkvitamin.com/features/atlas-under-the-hood/">Think Vitamin has an article</a> discussing Atlas in more detail.</p>
<p>We&#8217;re really excited about the ways Atlas could change the process of developing web applications. Atlas will allow developers to worry less about boilerplate user interface and glue code, just as the Cappuccino framework gives developers many common features expected by users, letting them focus on the ones specific to their applications.</p>
<p>Furthermore, Atlas allows non-programmers, such as many graphic designers, to join in on the process of actually building an application, rather than just providing mockups that must then be replicated in code by a developer.</p>
<p>Atlas will be available this summer. Sign up for updates on <a href="http://280atlas.com">280atlas.com</a> and we&#8217;ll let you know of the progress on Atlas.</p>
]]></content:encoded>
			<wfw:commentRss>http://cappuccino.org/discuss/2009/02/28/announcing-atlas/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>On Leaky Abstractions and Objective-J</title>
		<link>http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/</link>
		<comments>http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 18:29:47 +0000</pubDate>
		<dc:creator>tolmasky</dc:creator>
				<category><![CDATA[Cappuccino]]></category>
		<category><![CDATA[Objective-J]]></category>

		<guid isPermaLink="false">http://cappuccino.org/discuss/?p=159</guid>
		<description><![CDATA[In a recent post by John Resig, and in many of the comments, there seems to be the mistaken belief that Objective-J was designed to allow existing Objective-C programmers to write code that runs on the web. It&#8217;s been compared to GWT, where developers program almost exclusively in Java and are allowed to “circumvent” JavaScript. [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://ejohn.org/blog/javascript-language-abstractions/">recent post</a> by John Resig, and in many of the comments, there seems to be the mistaken belief that Objective-J was designed to allow existing Objective-C programmers to write code that runs on the web. It&#8217;s been compared to GWT, where developers program almost exclusively in Java and are allowed to “circumvent” JavaScript. This however is not the case with Objective-J at all. For starters, Objective-J is simply a language addition to JavaScript, and exists separately from the actual Cappuccino framework (which I&#8217;ll discuss a little later). It does not directly have anything to do with the DOM or AJAX, etc. The purpose behind Objective-J was to facilitate the development of Cappuccino, and when we originally set out to do that we simply wanted to add a few key missing features to the existing JavaScript “standard”. In other words, Objective-J is our take on JavaScript 2.0.</p>
<p>Now, this is usually the point at which someone chimes in that JavaScript is <strong>perfect</strong>, and that I just don&#8217;t understand it (you know, despite the fact that I&#8217;ve been using it for upwards of 10 years and have worked on its implementation in a major shipping browser). However, I think most people in the community can agree that there are some pretty big holes remaining in JavaScript. JavaScript is being used in increasingly larger projects, and in these projects some of its weaknesses are beginning to show. This is precisely why we have things like the (now dead) ECMAScript 4 proposal, Harmony, and Adobe&#8217;s ActionScript. And the truth is, had JavaScript 2.0 come around and been widely implemented and supported, we would have built Cappuccino without Objective-J. But the problem is that JavaScript 2.0 didn&#8217;t come around. I started working on what would eventually become Objective-J and Cappuccino <strong>three years ago</strong>, and in that time there have been no significant improvements or changes to the JavaScript language. Yes, certain browsers have taken it upon themselves to add a few nice features here and there, but for people writing real applications that need to run on most browsers, its simply not practical to rely on them. For the most part, we&#8217;re writing the same JavaScript today (language-wise at least) that we were 5 years ago. And that doesn&#8217;t seem like its going to change anytime soon.</p>
<h3><strong>Everybody is writing &#8220;abstractions&#8221; around JavaScript<br />
</strong></h3>
<p>And I don&#8217;t just mean projects like Pyjamas and GWT. I mean <strong>EVERYBODY</strong>, including Prototype, Dojo, and jQuery.  Yes, you read correctly, all the major JavaScript frameworks are essentially doing the same thing we are to some degree or another &#8212; we&#8217;re really not that original. The sole difference between their approach and ours is that when they add new language features to JavaScript, they&#8217;re not taking the extra step of introducing <em>new syntax</em>. Don&#8217;t believe me? Take a quick look at an example right out of the Prototype tutorials:</p>
<link rel="stylesheet" type="text/css" media="screen" href="/includes/tutorial.css"/>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;"><span class="comments">// when subclassing, specify the class you want to inherit from</span>
<span class="keyword">var</span> Pirate = Class.create(Person,
{
     <span class="comments">// redefine the speak method</span>
    <span class="selector">say</span>: <span class="keyword">function</span>($super, message)
    {
          <span class="keyword">return</span> $super(message) + ', yarr!';
    }
});</div>
</pre>
<p>What we&#8217;re seeing here is the introduction of classical inheritance to the JavaScript language. This has always traditionally been a language feature, but due to its absence in JavaScript, the folks at Prototype have chosen to make it a library feature. The important thing to realize here is that it requires as much work to learn about the Class function as it would to learn about a new class keyword. And the same is true of Dojo:</p>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;">dojo.declare("Employee", Person,
{
    <span class="selector">constructor</span>: <span class="keyword">function</span>(name, age, currentResidence, position)
    {
          <span class="comments">// remember, Person constructor is called automatically</span>
          <span class="keyword">this</span>.password="";
          <span class="keyword">this</span>.position=position;
    },
    <span class="selector">login</span>: <span class="keyword">function</span>()
    {
          <span class="keyword">if</span> (<span class="keyword">this</span>.password)
               alert('you have successfully logged in');
          <span class="keyword">else</span>
               alert('please ask the administrator for your password');
    }
});</div>
</pre>
<p>Once again, dojo.declare might as well be a new keyword since its shoehorning classical inheritance right into JavaScript. The jQuery UI widgets do something very similar to this with the &#8220;widget&#8221; factory method:</p>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;">
$.widget("ui.sortable", $.extend({}, $.ui.mouse,
{
    <span class="selector">_init</span>: <span class="keyword">function</span>()
    {
           <span class="keyword">var</span> o = this.options;
           <span class="keyword">this</span>.containerCache = {};
           <span class="keyword">this</span>.element.addClass("ui-sortable");
<span class="comments">//...</span></div>
</pre>
<p>At the end of the day, it doesn&#8217;t really matter if you&#8217;re a JavaScript expert, all these libraries have quite the learning curve and if its your first time with any of them, you&#8217;re going to have be referring to documentation a lot.  And that&#8217;s <strong>not a bad thing</strong>. My point is simply that in JavaScript, more so than anywhere else, libraries need to first define what is essentially a new mini-language before they can begin to do anything useful.  This is a bit unfortunate, because it ends up tying language additions to library features, despite there being no necessary connection between the two. This makes it particularly hard to use the different libraries together in an efficient way. Sure, you can use all three in the same project, but there are some clear walls between them since each one decides to do classes a little bit differently.  Not to mention the fact that you now have pretty redundant code your site. This is what happens when a language is clearly missing features. Individual frameworks in the .NET, Objective-C, Java, and so forth world don&#8217;t suffer from core incompatibility in the way they choose to do something as fundamental as defining class hierarchies. This is part of the reason we built Objective-J as its own standalone entity.  If all you want is some of the cool language features that Objective-J adds (without any of the design patterns in Cappuccino) then you can <em>just</em> use Objective-J, there is no &#8220;all or nothing&#8221; contract.</p>
<h3>OK, so how is Objective-J different?</h3>
<p>As we&#8217;ve now seen, there is a general tendency to add language features to JavaScript. Now, in <em>my personal opinion</em>, I prefer language features as fundamental as these to be part of the syntax, not part of a library. Let&#8217;s look at what Objective-J inheritance looks like:</p>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;"><span class="keyword">@import</span> "Animal.j"

<span class="keyword">@implementation</span> Snake : Animal
{
    Number miceEaten;
}

- (String)<span class="selector">speak</span>
{
    <span class="keyword">return</span> "hisss!";
}

<span class="keyword">@end</span></div>
</pre>
<p>To me this code reads a little more naturally, and at the very least it&#8217;s no harder to pick up than the numerous library functions required in the other libraries. This approach also has the added benefit of making it absolutely trivial to write new syntax modules so that my text editor of choice can actually know what is going on in my code. You&#8217;ll also notice that creating a new syntax gives us great flexibility and allows us to add a few nifty features that wouldn&#8217;t otherwise be possible, such as optional static typing and code importing. But again, this isn&#8217;t as &#8220;out there&#8221; as some people would have you believe. It has even more benefits as we&#8217;ll soon find out, but before we do, let me answer the question that I&#8217;m sure many of you are asking right now:</p>
<h3>Why Objective-C style syntax?</h3>
<p>Again, we didn&#8217;t start off saying &#8220;lets port Objective-C&#8221;!  In fact, our first versions of Objective-J looked very similar to Java or C++:</p>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;"><span class="keyword">class</span> Snake <span class="keyword">extends</span> Animal
{
    Instance.<span class="selector">speak</span> = <span class="keyword">function</span>()
    {
          <span class="keyword">return</span> "hisss!";
    }
}</div>
</pre>
<p>However, this approach had some serious issues. To better understand them, we have to take a look at what the principal design goals of Objective-J actually were:</p>
<ol>
<li>First off, it <strong>had</strong> to be a strict superset of JavaScript &#8211; meaning all existing JavaScript had to work. Believe it or not, we <em>do like</em> JavaScript, and we didn&#8217;t want to break it or create a trade-off of features.  Not to mention we wanted the existing plethora of libraries to still work.</li>
<li>Secondly of course, we wanted to add a number of &#8220;key&#8221; features.  Some things as crucial as being able to <strong>import code</strong> and built in support for classical inheritance.  We also wanted some features inspired by languages like Ruby, such as method_missing.</li>
<li>And lastly, we wanted to keep the ease of use of JavaScript, which meant not adding an extra &#8220;compilation&#8221; step, but instead allowing developers to still pound out some code and hit refresh and see it run.</li>
</ol>
<p>The problem with adding language features in a way that would be more &#8220;familiar&#8221; to most developers, like the Java approach above, is that it would break backwards compatibility with existing JavaScript. This is because a lot of the keywords we wanted to use are actually reserved words in JavaScript, despite not being currently used. There is good reason for this though, because at some point they may actually <em>mean </em>something, and we wanted to make sure Objective-J kept working when JavaScript (finally) got a real update. Of course, we could have just chosen different keywords, like &#8220;Class&#8221; instead of &#8220;class&#8221; for instance. This however also breaks backwards compatibility because someone may have used the Class identifier in their code already.</p>
<p>The other problem with this approach is that it made it next to impossible to add harder features like optional static typing or dynamic dispatch of methods (thus allowing for method_missing) without having a full blown compiler. There&#8217;s no way to &#8220;intercept&#8221; a method call in JavaScript.  So once again, we were stuck.</p>
<p>Luckily for us however, a group of people ran into these same issues about 25 years ago when trying to improve the C language. These were the folks at Stepstone who developed Objective-C. Just like in our case, Objective-C had to be a strict superset of C (unlike C++ which does NOT always play nice with C). Also lucky for us is that JavaScript syntax is very close to C syntax, so their language additions translated very naturally to JavaScript. And perhaps now it is obvious why our keywords start with the &#8220;@&#8221; symbol, because in this manner there is no possibility of ever clashing with future JavaScript keywords or predefined user variables and functions. In other words, we can now add to the language as much as we want without the fear of breaking compatibility with existing JavaScript.</p>
<p>Similarly, the new method passing syntax allows us to have firmer control of how messages are sent:</p>
<pre>
<div class="codeblock" style="margin-bottom: 0px; border: 0px;">object.<span class="selector">jsMethod</span>();
[object <span class="selector">objjMessage</span>];</div>
</pre>
<p>This is the famous bracket syntax. What you may not realize is that this makes it perfectly clear when you are using Objective-J style message dispath, allowing us to &#8220;hook in&#8221; and add things like method_missing, etc. There are a whole bunch of other cool things that this enables which are outside the scope of this post, but I encourage you to check out the other <a href="http://cappuccino.org/learn/tutorials">tutorials</a> on our site to learn about them. All this, <strong>without</strong> breaking any existing libraries and without requiring the user to compile before every run. Pretty impressive for a langauge that is supposedly &#8220;completely different&#8221; from JavaScript.</p>
<h3>The Part About Leaky Abstractions</h3>
<p>On top of Objective-J, we&#8217;ve built the Cappuccino framework, which is designed to help developers write desktop class applications on the web. One of the many things Cappuccino does is &#8220;abstract away&#8221; the DOM, and I&#8217;ll get into why we do this in a moment.  But before I do, I think I should take a second to address this whole &#8220;leaky abstractions&#8221; business, because there seems to be a fair bit of confusion regarding this as well. If you haven&#8217;t already read it, or even if you have, I recommend you take a look at <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">Joel Spolsky&#8217;s original article on the subject</a>.</p>
<p>When people refer to this article, it is usually to criticize some &#8220;abstraction&#8221; because it will &#8220;inevitably&#8221; be leaky, and then oh boy you better watch out! But this was not Spolsky&#8217;s thesis. He didn&#8217;t end the article with &#8220;and thus avoid abstractions at all costs&#8221;. He was not somehow suggesting that we stop abstracting altogether. Quite the contrary, its clear that although these &#8220;leaks&#8221; are an unfortunate and unavoidable consequence, abstractions are still incredibly worthwhile: hence the conundrum. In every single example given in the article, it was never implied that the abstraction was somehow bad or invalid because of its leak: Just because certain SQL queries are slow doesn&#8217;t mean you should manually read in tab delimmeted files and scan them yourself. Just because TCP can fail if you unplug your computer doesn&#8217;t mean you should do IP transfers yourself.  It just turns out that, big surprise, nothing is perfect. Joel put it best himself:</p>
<blockquote><p>Indeed, the abstractions we&#8217;ve created over the years <strong>do</strong> allow us to deal with new orders of complexity in software development that we didn&#8217;t have to deal with ten or fifteen years ago, like GUI programming and network programming.</p></blockquote>
<p>And the truth of the matter is that the problem of leaks is overstated. I find it particularly hilarious when JavaScript programmers bring this up, since JavaScript, being a garbage collected <em>dynamic</em> <em><strong>interpreted</strong></em> language, is quite possibly one of the biggest abstractions of all. And yet, most of us can see that the benefits of JavaScript far outweigh the &#8220;leaks&#8221;. I think few JavaScript programmers have had to learn &#8220;the whole stack&#8221; once they&#8217;ve encountered one of JavaScript&#8217;s many leaks, whether it be performance issues (from being interpreted) or perhaps large memory consumption (from being improperly garbage collected in some browsers). No one has had to go and open up WebKit&#8217;s source code to figure out these problems. Most of the time you simply Google your concern and learn the workaround. Not that big a deal.</p>
<p>Leaks are just something we all have to learn to deal with in every facet of programming, because programming <strong>is</strong> abstraction. Abstraction is basically all programmers ever do, from pushing electrons across a wire, to turning C into assembly, to interpreting JavaScript in your browser, every step in computer science has been an exercise in abstraction. There is nothing inherently wrong with abstraction. I can understand if someone disagrees with a particular abstraction for well defined reasons, but simply waving your hands and referring me to the law of leaky abstractions is not a valid argument.</p>
<p>To suggest that we shouldn&#8217;t abstract because of Joel Spolsky&#8217;s Law that all abstractions are inherently leaky is to suggest that we shouldn&#8217;t debug because of Murphy&#8217;s Law that all code will remain inhrerently buggy.</p>
<h3>Back to Cappuccino and the DOM</h3>
<p>Cappuccino doesn&#8217;t just abstract away the DOM, it abstracts away HTML, CSS, VML, the JavaScript DOM API, etc. Why? Because as you can see there are just too many technologies with overlapping responsibilities. It&#8217;s never really clear whether you should create a button in HTML or in JavaScript code, and whether you should style it in a CSS files or apply styles manually in JavaScript, or whether you should just use Canvas to draw everything. Not to mention that on top of having to sort through all these technologies, you have the additional challenge of having to accomodate for every slightly different flavor that exists in every browser. Sometimes its relatively simple, but othertimes it can be quite complex, as is the case with Canvas and VML. The other problem is that these technologies were simply not originally designed to handle the tasks they are now performing.  HTML was designed for laying out documents, not creating applications.</p>
<p>We look at these technologies completely differently.  To us, HTML, CSS, Canvas, the DOM, etc all represent the &#8220;rendering engine&#8221; of the web, in the same way that graphics cards are the rendering engines for your computer. It&#8217;s too difficult to learn the intricacies of every graphics card on the market, so developers instead use technologies that hide these details like OpenGL. In the same way, Cappuccino defines a common way to &#8220;draw&#8221; for the web. Internally, it figures out whether its more efficient to make a div, or draw to a canvas, or use VML. This is a fantastic time saver today, because it allows Cappuccino to worry about things like performance and allows you to just get a button on the screen. But its going to be even better as time goes on. This is not only because the underlying code will get better, but because web applications today look like this:</p>
<blockquote><p><center><img class="alignnone size-full wp-image-204" title="webapps1" src="http://cappuccino.org/discuss/wp-content/uploads/2008/12/webapps1.png" alt="" width="400" height="137" /></center></p></blockquote>
<p>But tomorrow, when enough browsers finally support SVG, they may look like this:</p>
<blockquote><p><center><img class="alignnone size-full wp-image-206" title="webapps2" src="http://cappuccino.org/discuss/wp-content/uploads/2008/12/webapps2.png" alt="" width="400" height="137" /></center></p></blockquote>
<p>Or, for all we know, like this:</p>
<blockquote><p><center><img class="alignnone size-full wp-image-207" title="webapps4" src="http://cappuccino.org/discuss/wp-content/uploads/2008/12/webapps4.png" alt="" width="400" height="137" /></center></p></blockquote>
<p>The important thing is that when this happens, and it <em>will</em> eventually happen, people who invested heavily in todays technologies will have to spend a great deal of effort updating, but we will just swap out the guts of Cappuccino and your code will inherit the benefits of SVG or whatever new technology is out there.</p>
<h3>Conclusion</h3>
<p>To me what we&#8217;ve done with Objective-J and Cappuccino seems inevitable.  Even the most conservative major JavaScript libraries can&#8217;t get away without adding features to the language, and we&#8217;ve simply taken the next logical step with Objective-J. Similarly, Cappuccino is a tool that allows you to stop thinking about web pages and browser incompatibilities, and instead focus on the complex problems presented by the challenge of creating full-on applications on the web. As we&#8217;ve said <a href="http://cappuccino.org/discuss/2008/10/21/web-pages/">countless</a> <a href="http://cappuccino.org/learn/">times</a>, Cappuccino isn&#8217;t for everything. All of this abstraction isn&#8217;t necessary if you&#8217;re simply trying to add dynamic elements to a site like bankofamerica or digg. Cappuccino is for building rich applications in the browser, a small minority in the web world today. We believe that this is where things are headed, and we designed Cappuccino specifically to get <em>us</em> there.</p>
<p>- Francisco</p>
]]></content:encoded>
			<wfw:commentRss>http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/feed/</wfw:commentRss>
		<slash:comments>63</slash:comments>
		</item>
		<item>
		<title>Synthesizing Accessor Methods</title>
		<link>http://cappuccino.org/discuss/2008/10/26/synthesizing-accessor-methods/</link>
		<comments>http://cappuccino.org/discuss/2008/10/26/synthesizing-accessor-methods/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 07:58:45 +0000</pubDate>
		<dc:creator>ross</dc:creator>
				<category><![CDATA[Objective-J]]></category>
		<category><![CDATA[@accessors]]></category>
		<category><![CDATA[Accessor Synthesizing]]></category>

		<guid isPermaLink="false">http://cappuccino.org/discuss/?p=114</guid>
		<description><![CDATA[Yesterday, Francisco and Tom checked in a new feature to the Cappuccino git repository, called accessor synthesizing. This is a new language feature for Objective-J, and its designed to reduce the amount of boilerplate code developers have to write in a custom class. Normally, when you write a custom class in Objective-J, you also write [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, Francisco and Tom checked in a new feature to the Cappuccino git repository, called accessor synthesizing. This is a new language feature for Objective-J, and its designed to reduce the amount of boilerplate code developers have to write in a custom class. Normally, when you write a custom class in Objective-J, you also write a pair of getter and setter methods for each instance variable (ivar) that you want to expose. This usually means writing repetitive code that hardly ever changes each time you write it. With accessor synthesizing, developers will only have to write getter and setter methods when something truly interesting needs to happen in those methods.</p>
<p>Accessor synthesizing adds a new keyword to the Objective-J language, <strong>@accessors</strong>. When declaring your list of ivars in the <strong>@implementation</strong> block of your class, adding this one keyword will tell the Objective-J preprocessor to implement accessors for you. Let&#8217;s see an example of a simple class written using the old method, and then the same class using <strong>@accessors</strong>.</p>
<p>This Person class has two ivars, firstname and lastname. Before <strong>@accessors</strong>, the class looks like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
@<B><span style="color:#A020F0">implementation</span></B> <B><span style="color:#0000FF">Person</span></B> : <B><span style="color:#0000FF">CPObject</span></B>
{
    CPString firstName;
    CPString lastName;
}

- (<B><span style="color:#228B22">void</span></B>)<B><span style="color:#0000FF">setFirstName</span></B>:(<B><span style="color:#228B22">CPString</span></B>)<span style="color:#B8860B">aString</span>
{
    firstName = aString;
}

- (<B><span style="color:#228B22">CPString</span></B>)<B><span style="color:#0000FF">firstName</span></B>
{
    <B><span style="color:#A020F0">return</span></B> firstName;
}

- (<B><span style="color:#228B22">void</span></B>)<B><span style="color:#0000FF">setLastName</span></B>:(<B><span style="color:#228B22">CPString</span></B>)<span style="color:#B8860B">aString</span>
{
    lastName = aString;
}

- (<B><span style="color:#228B22">CPString</span></B>)<B><span style="color:#0000FF">lastName</span></B>
{
    <B><span style="color:#A020F0">return</span></B> lastName;
}

@<B><span style="color:#A020F0">end</span></B><!--END enscript--></pre>
<p>Using the new <strong>@accessors</strong> keyword, we can eliminate four methods:</p>
<pre class="enscript" ><!--BEGIN enscript-->
@<B><span style="color:#A020F0">implementation</span></B> <B><span style="color:#0000FF">Person</span></B> : <B><span style="color:#0000FF">CPObject</span></B>
{
    CPString firstName @<B><span style="color:#A020F0">accessors</span></B>;
    CPString lastName @<B><span style="color:#A020F0">accessors</span></B>;
}
@<B><span style="color:#A020F0">end</span></B><!--END enscript--></pre>
<p>This second version is much more succinct than the first, and less tedious to program. We think this will be helpful for developers, and will increase productivity and code readability.</p>
<p>It&#8217;s important to note that this doesn&#8217;t change the way you use a class. For our Person class, in both cases, the code for getting the first and last names of a person (in the myPerson variable) would look something like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
var fullName = [myPerson firstName] + <B><span style="color:#BC8F8F">&quot; &quot;</span></B> + [myPerson lastName];<!--END enscript--></pre>
<p>Similarly, setting an ivar looks like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
[myPerson setFirstName:<B><span style="color:#BC8F8F">&quot;Winston&quot;</span></B>];
[myPerson setLastName:<B><span style="color:#BC8F8F">&quot;Smith&quot;</span></B>];<!--END enscript--></pre>
<p>Like any key-value-coding compliant class, the name of the instance variable becomes the &#8220;property&#8221; or &#8220;key&#8221;, and that key name is used in the setter and getter methods. If your key is <em>firstName</em>, the getter for that key will be <em>firstName</em>, and the setter will be <em>setFirstName:</em>. The same pattern applies to any other key.</p>
<h3>Configuration</h3>
<p>There are a few configuration options when using the new <strong>@accessors</strong> technique. Perhaps most importantly, is the ability to change the name of the property. For example, if you declared an instance variable like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
CPString _location @<B><span style="color:#A020F0">accessors</span></B>;<!--END enscript--></pre>
<p>Objective-J would generate two accessor methods called <em>_location</em> and <em>_setLocation:</em>. These are the expected accessor methods for a key called &#8220;_location&#8221;. In order to generate more friendly versions without the leading underscore, you can modify your declaration like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
CPString _location @<B><span style="color:#A020F0">accessors</span></B>(<B><span style="color:#0000FF">property</span></B>=location);<!--END enscript--></pre>
<p>The addition of the property=location tells Objective-J to use &#8220;location&#8221; as the new property name, instead of &#8220;_location&#8221;, which will generate <em>location</em> and <em>setLocation:</em> methods without the underscore. In addition, you can also specify the specific name of both the getter and the setter method, like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
<B><span style="color:#A020F0">BOOL</span></B> _hidden @<B><span style="color:#A020F0">accessors</span></B>(<B><span style="color:#0000FF">getter</span></B>=isHidden, setter=setIsHidden);<!--END enscript--></pre>
<p>This code specified a boolean instance variable called _hidden; the getter method will be called <em>isHidden</em> and the setter method will be called <em>setIsHidden:</em>. One thing to keep in mind when specifying your own getter and setter method names is key-value-coding. Although we don&#8217;t yet have much documentation on the subject (you can read Apple&#8217;s documentation <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html">here</a>), key-value-coding enables several important features in Cappuccino, so it&#8217;s important to maintain key-value-coding compliance. Normally, the rules are that for a given key, say &#8220;foo&#8221;, the getter is called <em>foo</em>, and the setter is called <em>setFoo:</em>. There&#8217;s an additional rule for boolean values, which says that you can use <em>isFoo</em>, and <em>setIsFoo:</em>, in addition to the usual method names. In our case, thats why our custom getter and setter names are still key-value-coding compliant.</p>
<p>There are two additional configuration options, <strong>readonly</strong>, and <strong>copy</strong>. By default, <strong>@accessors</strong> will generate both a getter and a setter. If you only want to expose a getter method to the world, you can add the <strong>readonly</strong> value in the arguments. The other option, <strong>copy</strong>, has to do with how the setter works. Normally, when you pass an object to a setter, it&#8217;s assigned directly. The generated setter looks like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
- (<B><span style="color:#228B22">void</span></B>)<B><span style="color:#0000FF">setFoo</span></B>:(<B><span style="color:#228B22">id</span></B>)<span style="color:#B8860B">aFoo</span>
{
    foo = aFoo;
}<!--END enscript--></pre>
<p>When you specify <strong>copy</strong>, the argument is first sent the message copy, and the return value of that message is assigned to the instance variable:</p>
<pre class="enscript" ><!--BEGIN enscript-->
- (<B><span style="color:#228B22">void</span></B>)<B><span style="color:#0000FF">setFoo</span></B>:(<B><span style="color:#228B22">id</span></B>)<span style="color:#B8860B">aFoo</span>
{
    <B><span style="color:#A020F0">if</span></B> (foo !== aFoo)
        foo = [aFoo copy];
}<!--END enscript--></pre>
<p>This is useful if you want to ensure that an object you&#8217;re passing isn&#8217;t modified out from under you. Of course, it only applies to ivars that are both read and write, and cannot be combined with readonly. To summarize, you can use both of these options like this:</p>
<pre class="enscript" ><!--BEGIN enscript-->
@<B><span style="color:#A020F0">implementation</span></B> <B><span style="color:#0000FF">FooBar</span></B> : <B><span style="color:#0000FF">CPObject</span></B>
{
    <B><span style="color:#A020F0">id</span></B> foo @<B><span style="color:#A020F0">accessors</span></B>(<B><span style="color:#0000FF">readonly</span></B>);
    <B><span style="color:#A020F0">id</span></B> bar @<B><span style="color:#A020F0">accessors</span></B>(<B><span style="color:#0000FF">copy</span></B>);
}<!--END enscript--></pre>
<h3>History</h3>
<p>Apple introduced properties in Objective-C 2.0, which added a new way to declare instance variables for your classes. The two key features were the ability to use what has become known as &#8220;the dot syntax&#8221; for accessing instance variables, and the second was the ability to have the compiler automatically generate accessor methods. Reducing code, especially code that doesn&#8217;t change every time you write it, is certainly a worthwhile goal, so we thought it would be a good idea to bring this new idea to Objective-J. There are a lot of things about the Objective-C 2.0 implementation of properties that don&#8217;t make sense for Objective-J, so we decided not to port them directly and instead developed a syntax that we believe makes the most sense.</p>
<p>The main goal we want to achieve is eliminating the need to write your own accessor methods. We don&#8217;t, however, support the &#8220;dot syntax&#8221; aspect of Objective-C 2.0. This makes the dual @property and @synthesize syntax of Objective-C 2.0 redundant. Furthermore, rather than just choose one or the other, we decided to use a new keyword, <strong>@accessors</strong>. This will lesson the confusion for existing Cocoa developers trying to learn Objective-J. It will also make sure we don&#8217;t have an incompatible syntax going forward, as we explore new ways to work with existing Objective-C code.</p>
<h3>Conclusion</h3>
<p>Accessor synthesizing will reduce the amount of boilerplate code that needs to be written, and make the language just a little bit easier to develop in. Overall, we think it&#8217;s a worthwhile addition to the language. If you have an opinion, we&#8217;d love to hear it, so sound off in the comments. Although this new syntax isn&#8217;t yet written in stone, we won&#8217;t expect to change it after version 0.6 of Cappuccino ships. Checkout the latest version of the git repository to start using this new feature.</p>
<p>We&#8217;re also considering other language features that might make it easier to program in Objective-J. If you&#8217;ve got an idea, or you&#8217;re interested in being part of the discussion, you should sign up for the <a href="/discuss/list.php">mailing list</a>, where we&#8217;ll be discussing some of the new proposals.</p>
]]></content:encoded>
			<wfw:commentRss>http://cappuccino.org/discuss/2008/10/26/synthesizing-accessor-methods/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
