<?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:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Paolo Capriotti</title>
	<atom:link href="http://pcapriotti.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pcapriotti.wordpress.com</link>
	<description>KDE hacking and more</description>
	<pubDate>Sun, 27 Apr 2008 14:03:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>Rewriting from scratch</title>
		<link>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/</link>
		<comments>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 14:02:49 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=40</guid>
		<description><![CDATA[I have recently come across this article (and those linked by it), while I was considering a rewrite for my own pet project.
While I tend to think that rewriting from scratch is usually a bad idea, if we are talking about open source projects, there&#8217;s another element you should take into account: how pleasurable it [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I have recently come across <a href="http://my.opera.com/Vorlath/blog/2007/09/25/code-rewrite-yes">this article</a> (and those linked by it), while I was considering a rewrite for my own pet project.</p>
<p>While I tend to think that rewriting from scratch is usually a bad idea, if we are talking about open source projects, there&#8217;s another element you should take into account: how pleasurable it is to work on it. If you end up with a big and hairy code base you cannot touch without suddenly feeling all sad and pessimistic about the fate of the world, you should realize that something has gone deeply wrong: you are supposed to work on your project for <em>fun</em> (yeah, to scratch an itch, maybe, too&#8230;), and when the fun stops, most of the reasons to continue disappear. You have no (paying) customers, you have no deadlines, you will be able (or unable, depending on what you do in the rest of your time) to pay your bills anyway.</p>
<p>At this point, either you throw everything into the garbage bin - which may be sad, disappointing and frustrating, both for you and your users - or you take whatever lessons you may have learned in the process, and use your extra wisdom to start from scratch a hopefully better and more mature design and implementation that won&#8217;t have those shortcomings that brought you to this miserable situation.</p>
<p>It will probably have other defects and issues, though, more subtle and harder to spot in an upfront design. You have to accept it. And you have to accept the fact that you may need to start from scratch once again in the future.</p>
<p>Here is a simple form you can use to announce your decision to start from scratch. Just copy-paste it into your blog, filling in the required fields. It may save you precious time that you will be able to spend on your n-th rewrite <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre>I'll rewrite my pet project from scratch because:

[ ] It&#8217;s not modular enough
[ ] The original design didn&#8217;t take __________________ into consideration
[ ] It&#8217;s too slow
[ ] There is too much legacy code which I can&#8217;t seem to get rid of
[ ] I want to write it in ____________________

&#8212;-

[ ] And this time I&#8217;ll use TDD/BDD

[ ] And this time it will be done right!

Signed (the developer)

_____________________________</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=40&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Fighting zombies with a double fork</title>
		<link>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/</link>
		<comments>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 01:39:01 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[posix]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[zombies]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=38</guid>
		<description><![CDATA[No, this post is not about a special weapon against Romero&#8217;s undeads (though that would be cool :), but about a trick I&#8217;ve just discovered after a lot of struggling with child processes that aren&#8217;t properly reaped by their parent.
Let me try to be a bit more specific: suppose you have a parent that needs [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>No, this post is not about a special weapon against Romero&#8217;s undeads (though that would be cool :), but about a trick I&#8217;ve just discovered after a lot of struggling with child processes that aren&#8217;t properly reaped by their parent.<br />
Let me try to be a bit more specific: suppose you have a parent that needs to spawn two kinds of child processes. Processes of the first kind perform a computation and return to the parent immediately (so they are handled via a fork+exec+waitpid combination), while those of the second kind should be started, their pid recorded so that they may be killed later, and then forgotten.<br />
The question is: how to handle the second kind of subprocesses? If you just fork+exec, when you later kill them, you get zombie processes. If you ignore SIGCHLD (so auto-reaping is enabled) you can&#8217;t perform a blocking wait on the children of the first kind anymore. Handling SIGCHLD is perhaps possible, but I think you need to keep track of the PIDs of the spawned processes and do some magic in the handler. I didn&#8217;t manage to make this working.</p>
<p>What I did manage to get working is the following: just spawn the process in a temporary child process. This way the grandchild will be orphaned and won&#8217;t require a wait from its grandfather.</p>
<p>There are however some problems with this approach. Namely, the child needs to send the PID of its own child to the parent. I&#8217;ve accomplished that through a pipe. Here is some example code to better illustrate this technique:</p>
<pre name="code" class="python">

from subprocess import Popen, PIPE, STDOUT
import sys

def spawn(cmd):
    cmdline = (&#039;python&#039;, __file__) + cmd
    return int(Popen(cmdline, stdout = PIPE).stdout.read())

if __name__ == &#039;__main__&#039;:
    print Popen(sys.argv[1:], stdout = open(&#039;/dev/null&#039;, &#039;w&#039;), stderr = STDOUT).pid
    sys.exit(0)
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=38&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/feed/</wfw:commentRss>
		</item>
		<item>
		<title>skema 0.1</title>
		<link>http://pcapriotti.wordpress.com/2007/11/19/skema-01/</link>
		<comments>http://pcapriotti.wordpress.com/2007/11/19/skema-01/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 19:49:51 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[kde]]></category>

		<category><![CDATA[ruby]]></category>

		<category><![CDATA[skema]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/11/19/skema-01/</guid>
		<description><![CDATA[I have just released skema, a very simple tool I wrote to automate some repetitive coding tasks. Nothing fancy, just a ruby script that expands ERB templates in a directory, reading variable values from the command line, configuration files, or interactively.
Its purpose is similar to kapptemplate (it already includes a minimal KDE application template), but [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I have just released <strong>skema</strong>, a very simple tool I wrote to automate some repetitive coding tasks. Nothing fancy, just a ruby script that expands <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB</a> templates in a directory, reading variable values from the command line, configuration files, or interactively.<br />
Its purpose is similar to <a href="http://kapptemplate.granroth.org/">kapptemplate</a> (it already includes a minimal KDE application template), but it may be faster to use for little things, and it is probably easier to create templates for it.<br />
Here is a <a href="http://capriotti.selfip.net/skema-0.1.tar.gz">tarball</a> for those of you who want to try it out. You can find a short tutorial in the README file that should be enough to get you started.<br />
Here is a screenshot of skema in interactive mode while expanding the kapp template:<br />
<a href="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png" title="screenshot of skema 0.1"><img src="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png" alt="screenshot of skema 0.1" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=36&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/11/19/skema-01/feed/</wfw:commentRss>
	
		<media:content url="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png" medium="image">
			<media:title type="html">screenshot of skema 0.1</media:title>
		</media:content>
	</item>
		<item>
		<title>Some thoughts on git</title>
		<link>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/</link>
		<comments>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 13:58:29 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[git]]></category>

		<category><![CDATA[kde]]></category>

		<category><![CDATA[tagua]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[distributed vcs]]></category>

		<category><![CDATA[modularity]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/</guid>
		<description><![CDATA[As I mentioned in a previous post, we started using git for managing Tagua source code. There&#8217;s currently a lot of controversy in the topic of distributed versus centralized VCS&#8217;s, and I feel like expressing my own ideas, too. 
I&#8217;m no git guru (yet), so please don&#8217;t get offended if I missed something in my [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>As I mentioned in a previous post, we started using git for managing Tagua source code. There&#8217;s currently a lot of controversy in the topic of distributed versus centralized VCS&#8217;s, and I feel like expressing my own ideas, too. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
I&#8217;m no git guru (yet), so please don&#8217;t get offended if I missed something in my criticism. I hope to generate a fruitful discussion, because I really think that the VCS is an important element in the development of an open source project.</p>
<h3>Why git rules</h3>
<ul>
<li>It&#8217;s decentralized. I fully agree with Linus on this point: the decentralized model is superior, for various reasons I won&#8217;t talk about in detail here.</li>
<li>It&#8217;s simple. I don&#8217;t understand why people keep complaining about git being overly complicated. The object model is fairly straightforward, as soon as you stop thinking about revisions the cvs/svn way, and branching/merging is trivial.</li>
<li>The object model is very well designed, flexible and useful.</li>
</ul>
<h3>Why git sucks</h3>
<p>Well, git has a number of minor (?) defects, like portability issues, impossibility of doing partial checkouts (actually I think this is a problem with all the decentralized VCS&#8217;s) and stuff like that, but let me concentrate on those issues which are fundamental to git as an open source project, and are unlikely to be addressed in the future:</p>
<ul>
<li>It is written in (unreadable) C. I don&#8217;t want to start a flame war (really!), but browsing through git&#8217;s code is a painful experience. Huge .c files with tons of big functions with no apparent structure and almost no comment. Ubiquitous micro-management stuff for handling manually allocated buffers. In other words&#8230; a mess.</li>
<li>It (ab)uses the UNIX philosophy. <em>Do one thing, and do it well</em>. Yeah, of course it&#8217;s a great idea, but maybe they shouldn&#8217;t have taken it too literally. I mean, if we talk about modularity and reusable components, I&#8217;m all for it, but it doesn&#8217;t mean you have to make a different executable for each task (even low level ones that should be invisible to the end user) and chain them together via text pipes and bash scripts!</li>
<li>It&#8217;s not reusable by third party applications, at least not in any efficient/convenient way. If you want to build a higher-level abstraction over git (a so-called porcelain), you need to fork out every now and then to get input from git, and then parse its textual output. Yes, I know about libgit.a, but that&#8217;s not really usable as a library (half of git functions call <code>exit</code> on failure) and has no API documentation at all.</li>
</ul>
<p>That said, I have to admit that most of its defects aren&#8217;t that important if you are just going to use it, and not develop it. However, using something you have problems inspecting and modifying feels a bit like using a closed source product&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=34&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/feed/</wfw:commentRss>
		</item>
		<item>
		<title>KBoard is dead. Long live Tagua!</title>
		<link>http://pcapriotti.wordpress.com/2007/07/19/kboard-is-dead-long-live-tagua/</link>
		<comments>http://pcapriotti.wordpress.com/2007/07/19/kboard-is-dead-long-live-tagua/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 11:44:43 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[kboard]]></category>

		<category><![CDATA[kde]]></category>

		<category><![CDATA[tagua]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/07/19/kboard-is-dead-long-live-tagua/</guid>
		<description><![CDATA[We finally made a decision, and named the project Tagua. Thanks to all the people who suggested possible names, and expecially to Riccardo Iaconelli who came up with the definitive one.
I&#8217;m now doing a global s/kboard/tagua/g on webpages, code, tracker, cron jobs, git repository&#8230;
Ah, yes, I forgot to mention that we moved the project to [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>We finally made a decision, and named the project <strong><a href="http://en.wikipedia.org/wiki/Tagua">Tagua</a></strong>. Thanks to all the people who suggested possible names, and expecially to <a href="http://blog.ruphy.org">Riccardo Iaconelli</a> who came up with the definitive one.<br />
I&#8217;m now doing a global <code>s/kboard/tagua/g</code> on webpages, code, tracker, cron jobs, git repository&#8230;</p>
<p>Ah, yes, I forgot to mention that we moved the project to <a href="http://git.or.cz/">git</a> when we resumed the development at Akademy. There has been a <a href="http://lists.kde.org/?t=118415921300005&amp;r=1&amp;w=2">long thread</a> on kde-core-devel discussing the adoption of git by subprojects, and there emerged that moving away from the KDE svn (though apparently discouraged) does not mean parting from the KDE project. I&#8217;ll probably talk about our experience with git in a dedicated post.</p>
<p>So, if you want to try the development version of Tagua, either grab a nightly snapshot (temporarely located <a href="http://tagua.ath.cx/tarballs/latest.tar.gz">here</a>), or clone our main repository:</p>
<pre>
git clone http://repo.or.cz/r/tagua.git</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=33&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/07/19/kboard-is-dead-long-live-tagua/feed/</wfw:commentRss>
		</item>
		<item>
		<title>KBoard: an update</title>
		<link>http://pcapriotti.wordpress.com/2007/07/18/kboard-an-update/</link>
		<comments>http://pcapriotti.wordpress.com/2007/07/18/kboard-an-update/#comments</comments>
		<pubDate>Wed, 18 Jul 2007 01:13:03 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[kboard]]></category>

		<category><![CDATA[kde]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/07/18/kboard-an-update/</guid>
		<description><![CDATA[KBoard is a project by Maurizio and myself for a generic board game application. It started back in October 2005, and progressed slowly but steady until the end of 2006. After then it was left starving on the playground, basically untouched until a few days ago.
After a prolific discussion at Akademy, we are now back [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>KBoard is a project by <a href="http://myrizio.wordpress.com">Maurizio</a> and myself for a generic board game application. It started back in October 2005, and progressed slowly but steady until the end of 2006. After then it was left starving on the playground, basically untouched until a few days ago.<br />
After a prolific discussion at Akademy, we are now back developing KBoard at full speed. Here are a couple of screenshots showing our progresses so far:</p>
<p><a href="http://pcapriotti.files.wordpress.com/2007/07/kboard.png" title="KBoard"><img src="http://pcapriotti.files.wordpress.com/2007/07/kboard_thumb.png" alt="KBoard" /></a></p>
<p><a href="http://pcapriotti.files.wordpress.com/2007/07/kboard1.png" title="KBoard"><img src="http://pcapriotti.files.wordpress.com/2007/07/kboard1_thumb.png" alt="KBoard" /></a></p>
<p>Compare them with the old <a href="http://kboard.sourceforge.net/wiki/index.php/Screenshots">screenshots</a> and you&#8217;ll see how much has been done in just a few weeks: the main layout and the clock are now themable via <a href="http://www.lua.org">Lua</a> scripts, just like the chessboard and pieces.<br />
I shall thank Maurizio for this astonishing work, and generally for his invaluable Lua theme loader, which is one of the finest pieces of code in KBoard.</p>
<p>By the way, most of the recent changes are actually invisible to the user, and concern the API used by the graphical interface to communicate with game plugins (called <em>variants</em>). I recently worked (not that much) on the animation stuff, and ported three games to the new API. Maurizio made the whole refactoring of the code using the old API at akademy, and revised the concepts around the pool (i.e. the place where captured pieces end up in games like Shogi and Crazyhouse).</p>
<p>We plan to release KBoard 1.0 by the end of the year. It might be an optimistic estimate, but if we continue to work with this speed, it should be actually possible. There is a lot of things to fix, but the planned features are almost all there. Needless to say, any help (developers, artists) would be really appreciated. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>At the moment, our biggest dilemma is the name. We decided that the name should be changed (KBoard is ugly and doesn&#8217;t even make clear what the application is about), but after a whole day of awfully poor proposals, we still have to find a decent alternative. So you if have a nice name in mind (not necessarily starting with k, think about Phonon, Plasma, Solid&#8230; those are good names!) please tell us!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=24&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/07/18/kboard-an-update/feed/</wfw:commentRss>
	
		<media:content url="http://pcapriotti.files.wordpress.com/2007/07/kboard_thumb.png" medium="image">
			<media:title type="html">KBoard</media:title>
		</media:content>

		<media:content url="http://pcapriotti.files.wordpress.com/2007/07/kboard1_thumb.png" medium="image">
			<media:title type="html">KBoard</media:title>
		</media:content>
	</item>
		<item>
		<title>Canvas adapter and plasma</title>
		<link>http://pcapriotti.wordpress.com/2007/06/05/canvas-adapter-and-plasma/</link>
		<comments>http://pcapriotti.wordpress.com/2007/06/05/canvas-adapter-and-plasma/#comments</comments>
		<pubDate>Tue, 05 Jun 2007 16:08:54 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[kde]]></category>

		<category><![CDATA[kgamecanvas]]></category>

		<category><![CDATA[plasma]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/06/05/canvas-adapter-and-plasma/</guid>
		<description><![CDATA[I&#8217;ve finally finished polishing the code of KGameCanvasAdapter, a hundred lines of straightforward code that stand as a bridge between KGameCanvas code and any QPainter based drawing system, including of course QGV.
The only test case for the adapter is a port of my kollision game to plasma. Porting was very easy: it was just a [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve finally finished polishing the code of KGameCanvasAdapter, a hundred lines of straightforward code that stand as a bridge between KGameCanvas code and any QPainter based drawing system, including of course QGV.</p>
<p>The only test case for the adapter is a port of my <a href="http://websvn.kde.org/trunk/playground/games/kollision/">kollision</a> game to plasma. Porting was very easy: it was just a matter of having the MainArea class inherit from Plasma::Applet and KGameCanvasAdapter instead of QWidget. The resulting applet is not really a game, at the moment: it is just a box with bouncing red balls.<br />
However, I think it&#8217;s a good test for both my adapter and plasma, since it can show how well plasma performs with CPU intensive plasmoids. Here is a short screencast of the kollision plasmoid in action.</p>
<p><span style="text-align:center; display: block;"><a href="http://pcapriotti.wordpress.com/2007/06/05/canvas-adapter-and-plasma/"><img src="http://img.youtube.com/vi/rK5rhHnDE0M/2.jpg" alt="" /></a></span></p>
<p>The code is available in a git repository:</p>
<pre>
git clone http://kollision.ath.cx/plasma.git plasma</pre>
<p>will download the whole patched plasma directory. To compile it, you should move it inside a kdebase/workspace working copy, so that it uses the existing cmake build system of kdebase.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=23&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/06/05/canvas-adapter-and-plasma/feed/</wfw:commentRss>
	
		<media:content url="http://img.youtube.com/vi/rK5rhHnDE0M/2.jpg" medium="image" />
	</item>
		<item>
		<title>KBattleship: almost ready</title>
		<link>http://pcapriotti.wordpress.com/2007/04/13/kbattleship-almost-ready/</link>
		<comments>http://pcapriotti.wordpress.com/2007/04/13/kbattleship-almost-ready/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 21:39:47 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[kbattleship]]></category>

		<category><![CDATA[kde]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/04/13/kbattleship-almost-ready/</guid>
		<description><![CDATA[This week I&#8217;ve been hacking quite a lot on KBattleship, adding almost all required features: a decent AI, network play and sounds. While Riccardo is working on KWelcomeScreen, a library class inspired by this blog post by Johann, I&#8217;m going to make some minor adjustments, add small missing features like scoring, and if everything goes [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This week I&#8217;ve been hacking quite a lot on KBattleship, adding almost all required features: a decent AI, network play and sounds. While Riccardo is working on KWelcomeScreen, a library class inspired by <a href="http://johann.pwsp.net/2007/04/09/a-new-game-starter/">this blog post</a> by Johann, I&#8217;m going to make some minor adjustments, add small missing features like scoring, and if everything goes well I guess we can move the application to kdereview within next week.<br />
This is a screenshot showing a game between the old KBattleship and my rewrite:<br />
<a href="http://linuz.sns.it/~paolo/kbs3.png" title="KBS4 screenshot"><img src="http://linuz.sns.it/~paolo/kbs3-thumb.png" alt="KBS4 screenshot" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=22&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/04/13/kbattleship-almost-ready/feed/</wfw:commentRss>
	
		<media:content url="http://linuz.sns.it/~paolo/kbs3-thumb.png" medium="image">
			<media:title type="html">KBS4 screenshot</media:title>
		</media:content>
	</item>
		<item>
		<title>Dynamic types and virtual inner types</title>
		<link>http://pcapriotti.wordpress.com/2007/04/05/dynamic-types-and-virtual-inner-types/</link>
		<comments>http://pcapriotti.wordpress.com/2007/04/05/dynamic-types-and-virtual-inner-types/#comments</comments>
		<pubDate>Thu, 05 Apr 2007 11:18:23 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[c++]]></category>

		<category><![CDATA[concepts]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/04/05/dynamic-types-and-virtual-inner-types/</guid>
		<description><![CDATA[The concept based polymorphism I tried to explain in my previous post can be more conveniently expressed with another syntax which avoids concepts at all.
The key observation is that runtime polymorphism and F-bounded polymorphism are very similar in nature, and, to some extent, the latter can be implemented using the former. If I have understood [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <em>concept based polymorphism</em> I tried to explain in my previous post can be more conveniently expressed with another syntax which avoids concepts at all.</p>
<p>The key observation is that runtime polymorphism and F-bounded polymorphism are very similar in nature, and, to some extent, the latter can be implemented using the former. If I have understood correctly, that is exactly how the Java virtual machine implements generics.</p>
<p>For example, suppose you have an abstract class <code>Shape</code> with a virtual function <code>draw</code>, and several concrete classes like <code>Square</code>, <code>Circle</code>, <code>Triangle</code>, which override <code>draw</code>.<br />
Client code using runtime polymorphism would look like:</p>
<pre>
void render(Shape* shape)
{
  shape-&gt;draw();
}</pre>
<p>while using generic programming one could write:</p>
<pre>
template &lt;typename S&gt;
where Inherits&lt;S, Shape&gt;
void render(S* s)
{
  s-&gt;draw();
}</pre>
<p>assuming that the concept Inherits&lt;T, U&gt; is verified when T inherits from U (possibly indirectly). An approximation for Inherits is</p>
<pre>
auto concept Inherits&lt;typename T, typename U&gt;
{
  // T can be implicitly converted to U
  T::operator U();
}</pre>
<p>Using the generic code in such situations should probably be considered a mistake, because if one wants to use template based polymorphism, defining the abstract class Shape is pointless. It would be better to directly define a ShapeConcept having a member function draw.</p>
<p>However, the example suggests that a syntax resembling templates could be used to express runtime polymorphism:</p>
<pre>
template &lt;Shape! S&gt;
void render(S* s)
{
  s-&gt;draw();
}</pre>
<p>the idea being that S is the runtime type of <code>s</code>. The compiler could instantiate the template immediately and implement the body just like a <code>Shape</code> pointer were used instead of <code>S</code>. Here <code>S</code> is not really a type, since for example expressions like</p>
<pre>
new S</pre>
<p>should be rejected; let&#8217;s call things like <code>S</code> <strong>dynamic types</strong>. They behave much like incomplete types (cannot be instantiated directy, sizeof cannot be called on them), but are a different beast: for the purpose of typechecking they are just subtypes of their <strong>parent concept</strong> (<code>Shape</code>, in this example), but for code generation they are considered equivalent to it.</p>
<p>Why could all of this syntax be possibly useful? Well, when using ordinary abstract classes, it does not really add anything to the language, but it shows its power when another (more than syntactical) extension is introduced: <strong>virtual inner types</strong>.</p>
<p>Just like the name suggests, virtual inner types are inner types which behave polymorphically. The typical (and motivating) example here is still that of the abstract factory. So suppose you have the following abstract factory:</p>
<pre>
class AbstractAnimal;
class AbstractFood;

class AbstractFactory
{
public:
  virtual AbstractAnimal* createAnimal() = 0;
  virtual AbstractFood* createFood() = 0;
};</pre>
<p>and the following abstract products:</p>
<pre>
class AbstractAnimal
{
public:
  virtual void eat(Food*) = 0;
};

class AbstractFood
{
public:
  virtual int quality() const = 0;
};</pre>
<p>As explained in my previous posts [1] [2], there&#8217;s no way to explain to the compiler that the <code>Food</code> that an animal is able to eat is only that created by the same factory which created that very animal.<br />
Virtual inner types could be used to solve the problem:</p>
<pre>
class AbstractAnimal;
class AbstractFood;

class AbstractFactory
{
public:
  virtual typedef AbstractAnimal Animal;
  virtual typedef AbstractFood Food;
  virtual Animal* createAnimal() = 0;
  virtual Food* createFood() = 0;
};

class AbstractAnimal
{
public:
  virtual typedef AbstractFactory Factory;
  virtual void eat(Factory::Food* food) = 0;
};

class AbstractFood
{
public:
  virtual typedef AbstractFactory Factory; // unused, only here for completeness
  virtual int quality() const = 0;
};</pre>
<p>The idea is that <code>virtual typedef Base X</code> forces inherited class to define an inner type <code>X</code> inheriting from <code>Base</code>. Furthermore, this type behaves polymorphically (the type itself, not only instances!). That means that if <code>A</code> is a dynamic type with parent <code>AbstractAnimal</code>, then <code>A::Factory</code> is the correct dynamic type with parent <code>AbstractFactory</code>.</p>
<p>To explain this fact in more detail, let&#8217;s continue the example:</p>
<pre>
class Cow;
class Grass;

class CowFactory : public AbstractFactory
{
public:
  typedef Cow Animal;
  typedef Grass Food;
  Cow* createAnimal();
  Grass* createFood();
};

class Cow : public Animal
{
public:
  typedef CowFactory Factory;
  void eat(Grass* grass);
};

class Grass : public Food
{
public:
  typedef CowFactory Factory;
  int quality() const;
};</pre>
<p>notice that we have defined inner types corresponding to the virtual typedefs of parent classes. Besides, the argument to eat is <code>Grass</code> and not <code>AbstractFood</code>.<br />
Client code, in fact, cannot simply pass an <code>AbstractFood</code> object to the <code>eat</code> member function. The following function should not compile:</p>
<pre>
void feed(AbstractAnimal* animal, AbstractFood* food)
{
  animal-&gt;eat(food);
}</pre>
<p>but can be fixed with the use of dynamic types</p>
<pre>
template &lt;AbstractFactory! Factory&gt;
void feed(Factory::Animal* animal, Factory::Food* food)
{
  animal-&gt;eat(food);
}</pre>
<p>expressing the fact that both <code>animal</code> and <code>food</code> come from the same factory of dynamic type Factory. The food parameter (actually stored in an <code>AbstractFood</code> variable), is statically downcasted to <code>Grass</code> before being passed to the <code>eat</code> member function. The static typechecking via dynamic types ensures that such a downcast is always safe, so there is no need to perform a dynamic cast.</p>
<p>These ideas for a language extension are essentially equivalent to the ones expressed in the previous post, but seem to require less drastic changes in compiler implementations. Furthermore, they doesn&#8217;t use concepts, so it could be even implemented on top of the existing gcc.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/21/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/21/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=21&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/04/05/dynamic-types-and-virtual-inner-types/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Covariance versus substitutivity</title>
		<link>http://pcapriotti.wordpress.com/2007/04/03/covariance-versus-substitutivity/</link>
		<comments>http://pcapriotti.wordpress.com/2007/04/03/covariance-versus-substitutivity/#comments</comments>
		<pubDate>Tue, 03 Apr 2007 14:21:03 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
		
		<category><![CDATA[c++]]></category>

		<category><![CDATA[concepts]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/04/03/covariance-versus-substitutivity/</guid>
		<description><![CDATA[The covariant argument problem which I tried to explain in a previous post has its roots in the incompatibility between covariance and substitutivity in a static typed world.
Substitutivity is considered the most important feature in the design of an object oriented language, though it is often underrated or misunderstood by many users of the language. [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The covariant argument problem which I tried to explain in a <a href="http://pcapriotti.wordpress.com/2007/02/25/the-covariant-argument-problem/">previous post</a> has its roots in the incompatibility between <strong>covariance</strong> and <strong>substitutivity</strong> in a static typed world.</p>
<p>Substitutivity is considered the most important feature in the design of an object oriented language, though it is often underrated or misunderstood by many users of the language. Basically, it states that subtyping is a &#8220;is-a&#8221; relation, i.e: any object of a derived class can be used everywhere an object of the base class in required, expressed with C++ terminology. In languages that follow this rule, derived classes may only widen the interface of their parent, since narrowing it could cause conflicts which are unpredictable at compile time. Narrowing the interface could be done in many ways, so they are all prohibited in languages like C++, C# or Java:</p>
<ul>
<li>removing a virtual function of the base class;</li>
<li>contravariantly changing the return type of a virtual function (i.e. moving it up in the inheritance hierarchy);</li>
<li>covariantly changing the type of any argument of a virtual function (i.e. moving it down in the inhritance hierarchy).</li>
</ul>
<p>The problem is that any of these possibilities where allowed, a piece of code that is able to operate on an object of the base class could &#8220;use a feature&#8221; that the derived class has removed (or incompatibly modified) and cause runtime failures.</p>
<p>For example, suppose that class <code>Cow</code> inherits <code>Animal</code>, where the latter has a virtual function <code>eat</code> which takes a <code>Food</code> object as an argument. It seems natural to override <code>eat</code> in <code>Cow</code> so that it takes a <code>Grass</code> (cows don&#8217;t eat any food), but if we do so, the following function</p>
<pre>
void f(Animal* a)
{
  Food* f = new Food;
  a-&gt;eat(f);
}</pre>
<p>which is perfectly valid when a is of class Animal, fails when called with an instance of Cow, and this violates substitutivity.</p>
<p>The wikipedia <a href="http://en.wikipedia.org/w/index.php?title=Method_overriding_%28programming%29&amp;oldid=117316117">article about overriding</a> is a perfect example of how much confusion the substitutivity rule can cause. The example in the article uses runtime exceptions to narrow the interface of the base class (note that the language used there is dynamically typed, so there would be no reason to do so). Using exception works (even in static typed languages, of course) but has several drawbacks:</p>
<ul>
<li>moves a part of typechecking to runtime, and that goes against the idea of having static typing in the first place;</li>
<li>clutters the code with typechecking statements;</li>
<li>feels a bit like a hack: it is essentially working around a limitation of the programming language.</li>
</ul>
<p>Using exceptions for narrowing the interface of derived classes, the previous example could be worked out as follows:</p>
<pre>
class Cow : public Animal
{
  virtual void eat(Food* food)
  {
    if (Grass* grass = dynamic_cast&lt;Grass*&gt;(food)) {
      // method body
    }
    else {
      throw typechecking_exception(/* ... */);
    }
  }
};</pre>
<p>I guess at this point some of you may still feel a bit unsure about <em>why</em> would anyone need to narrow the interface of derived classes.<br />
Indeed, many times, the urge of doing so turns out to be a direct consequence of a flawed object oriented design. Look at the Person / Baby example from the wikipedia article, for instance: the error there is that Baby should not be modelled as a subclass of Person. It is probably better to have a (likely abstract) class HumanBeing, with Person and Baby being both derived classes. If Baby needs to reuse part of Person&#8217;s implementation, it&#8217;s probably better to consider using incapsulation (after all <a href="http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4">inheritance is not for code reuse</a>).</p>
<p>However, there are situations (like the one I tried to describe <a href="http://pcapriotti.wordpress.com/2007/02/25/the-covariant-argument-problem/">here</a>) where the need to narrow interfaces while inheriting is justifiable. These situations can be distinguished by the fact that one is willing to sacrifice substitutivity, because sometimes it even gets in the way, and it is not a valuable feature anymore, but an obstacle for a clean design.<br />
In fact, it was clear from the previous examples that interface narrowing (called <strong>CAT</strong> in Eiffel terminology) is not an evil thing <em>per se</em>; it&#8217;s just that it can cause runtime failures when the substitutivity principle is applied.<br />
For an example of a situation where a CAT is necessary, take the <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Abstract Factory pattern</a>. For those who don&#8217;t have familiarity with this technique, it basically consists of an <strong>abstract factory</strong> class, which is able to create <strong>abstract products</strong>. Each abstract product is represented by a different abstract class that is the return type of a specialized (abstract) factory method of the abstract factory class. <strong>Concrete products</strong> must then be defined by inheritance; they are returned by the overridden factory methods of a <strong>concrete factory</strong> class. Several factory classes associated with different concrete products can be defined. The <strong>client</strong> only uses the abstract products and the abstract factory, and this allows for great generality in that the client is able to deal with a number of different product families, just by changing the particular concrete factory it uses (and this can be even done at runtime).</p>
<p>The fact that the client only accesses products through the particular concrete factory it happens to be using implies that it won&#8217;t be mixing concrete products belonging to different families (i.e. created with different concrete factories). There are some facts worth noticing here:</p>
<ul>
<li>Substitutivity does not apply. Or better, it <em>should not</em> be applied. Applying it would mean that incompatible products could be mixed, probably resulting in undefined behaviour.</li>
<li>It is client code&#8217;s responsibility to use the correct factory. If more than one factory is used, respective products should not be intermixed.</li>
<li>If the abstract products (hence the concrete ones) are interdependent, an interface narrowing could be needed. To stick with the previous example, suppose that both <code>Animal</code> and <code>Food</code> are abstract products, <code>Cow</code> and <code>Grass</code> being corresponding concrete implementations. If an <code>Animal</code> takes a <code>Food</code> object as an argument to one of its methods, how can this method be overridden in the concrete class? The dilemma here is that (assuming a well behaved client) we know that the method is going to be called with a <code>Grass</code> argument, but we are forced to declare it as a general <code>Food</code> because we need to preserve the parent signature.</li>
</ul>
<p>Mainstream static typed languages do not offer a clean solution to the problems outlined here. The <em>template wrapper</em> workaround used in KBoard is nice, but has several drawbacks as well (I&#8217;ll probably talk about it in more detail in another post). Obviously, such a solution could be simplified by direct language support, but the fact that typechecking has been partially moved to runtime makes it suboptimal anyway, so that it is not probably worth including it in the language, if it claims to be statically typed.<br />
The main question here is: &#8220;it is possible to give up substitutivity in a hierarchy to achieve greater power on the customization of derived classes, while having <em>all</em> type checking done at compile time?&#8221;.</p>
<p>To see why the answer <em>has to be</em> yes, let us find a solution to the problem using generic polymorphism instead of virtual functions.<br />
It turns out that, using generic programming facilities, the Abstract Factory pattern can be implemented without using an abstract factory class at all! Here is a sample realization of the previous scenario using C++ templates:</p>
<pre>
class Cow;
class Grass;

class CowFactory
{
public:
  typedef Cow Animal;
  typedef Grass Food;

  // factory methods are not needed! one can simply use "new CowFactory::Food"
}

class Cow
{
public:
  virtual void eat(Grass* g) { /* ... */ }
};

class Food
{
  // ...
};</pre>
<p>As you can see, there are no abstract classes at all. We directly implement concrete products following an implicit interface. Client code could be structured like this:</p>
<pre>
template &lt;typename Factory&gt;
class Client
{
  void f()
  {
    typename Factory::Animal animal;
    typename Factory::Food food;

    animal.eat(food);
  };
};</pre>
<p>This may look like a perfect solution: elegant, clean, efficient (notice that there are no virtual functions, and even dynamic allocations are avoided) and just as general and powerful as that based on runtime polymorphism, but unfortunately it has a fundamental drawback which makes it unfeasible in all practical circumstances: it requires the client code to have a Factory template argument.<br />
This has a series of unfortunate consequences:</p>
<ul>
<li>Client code has to be completely recompiled whenever you change the factory or the products.</li>
<li>There is a copy of the client code for each product family. If there are a lot of families, this leads to code bloat.</li>
<li>There&#8217;s no way to dynamically load a product family at runtime. A family cannot reside in a dynamic library. This is because genericity here depends on template instantiation, which can only happen at compile time.</li>
</ul>
<p>Nevertheless, it is possible to elaborate on that idea to define a language extension which almost perfectly solves the problem. My idea is based on <strong>concepts</strong>, a new C++ feature which will be introduced with the new language standard C++0x.<br />
To begin with, I&#8217;ll show you how the ConceptC++ language can be extended so that abstract classes can be modelled by concepts. Let&#8217;s stick to the Java terminology of calling <strong>interfaces</strong> those abstract classes with no data members and all pure virtual functions.<br />
There is an obvious natural map from interfaces to concept, mapping for example a class like</p>
<pre>
class A
{
  virtual void f(int x) = 0;
  virtual Foo* g() const = 0;
};</pre>
<p>to the concept</p>
<pre>
concept A&lt;typename T&gt;
{
  void T::f(int x);
  Foo* T::g() const;
};</pre>
<p>Code using the interface A could be rewritten using a template parameter T constrained by the concept A, and the semantics would be preserved, as well as the typechecking, with all pros and cons of the generic abstract factory implementation outlined above. Indeed, that was a particular case of this transformation, where we have simply omitted the concepts involved.</p>
<p>Let&#8217;s now consider an implementation of the A interface</p>
<pre>
class X : public A
{
public:
  void f(int x) { cout &lt;&lt; x &lt;&lt; endl; }
  Foo* g() const { return new Foo; }
};</pre>
<p>Is it possible to extend the language so that the above class compiles even when A is a concept? If the concept A has a single argument, no constructor or free function constraints, and no associated types, the answer is yes. The compiler needs to create an abstract class corresponding to the concept and have X inherit from it.</p>
<p>In this case we did not gain anything, but let&#8217;s broaden a little bit the set of admissible concepts. Define a concept <strong>dynamic</strong> if it has a single argument, no constructor or free function constraints, and all its associated types are constrained by dynamic concepts and never used by value (probably other restrictions are needed, but you get an idea). For example:</p>
<pre>
concept AbstractFoo&lt;typename T&gt;
{
  int T::foo() const;
};

concept A&lt;typename T&gt;
{
  AbstractFoo Foo = T::Foo;
  void T::test(Foo* x) const;
  Foo* T::create() const;
};

class MyFoo : public AbstractFoo
{
public:
  int value;
  MyFoo(int value) : value(value) { }
  int foo() const { return value; }
};

class X : public A
{
public:
  typedef MyFoo Foo;
  void test(MyFoo* f) const { f-&gt;value = 37; }
  MyFoo* create() const { return new MyFoo(42); }
};</pre>
<p>Since AbstractFoo and A are dynamic concepts by the above definition, the compiler lets you treat them as abstract classes, and inherit from them.<br />
Under the hood, the compiler creates the corresponding abstract classes and virtual tables, and behaves just like the concepts where abstract classes at all. If you are wondering what all this complication is for, just look at the argument of the test member function: it is an associated type, and as such, covariantly modified in the derived class. This could not be possible with normal inheritance.</p>
<p>To see how all of this can possibly work (and be statically checked), let&#8217;s write down some usage examples:</p>
<pre>
void evil()
{
  A* a = new X;
  AbstractFoo* foo = new MyFoo(15);
  a-&gt;test(foo);
}

void safe()
{
  A* a = new X;
  a-&gt;test(a-&gt;createFoo());
}</pre>
<p>The compiler has no way to understand that the evil function doesn&#8217;t actually do anything evil, so it has to give a type checking error on the test call there.<br />
However, the safe function does something interesting: it calls test with a parameter which is of the Foo inner type of a&#8217;s actual type. Therefore, the compiler can safely assume that no type mismatch is taking place, and accept the call.</p>
<p>But what if one wants to store the result of createFoo in some member of temporary variable? What is the type of this variable? Here we need to invent a bit of new syntax. I like the idea of being able to write something like the following:</p>
<pre>
A* a = new X;
conceptof(a)::Foo* foo = a-&gt;createFoo();
a-&gt;test(foo);</pre>
<p>Everything can be checked at compile time, since we know from the concept specification that createFoo is going to return the Foo inner type of the actual type of a, and we can typecheck the second and third line without knowing this type exactly. The conceptof keywords extracts the dynamic concept associated to an object and returns a dynamic type.<br />
Dynamic types can be used just like ordinary types: they can be typedef&#8217;d like this:</p>
<pre>
A* a = new X;
typedef A! conceptof(a) T;
T::Foo* foo = a-&gt;createFoo();
a-&gt;test(foo);</pre>
<p>(note the exclamation mark), and used as template parameters:</p>
<pre>
template &lt;A! T&gt;
void f(T* a)
{
  T::Foo* foo = a-&gt;createFoo();

}</pre>
<p>in which case the template is immediately instantiated.</p>
<p>For completeness&#8217; sake, I&#8217;ll translate the previous example about cows and grass to dynamic concepts:</p>
<pre>
concept GenericFactory&lt;typename Factory&gt;
{
  GenericFood Animal = Factory::Animal;
  GenericAnimal Food = Factory::Food;

  Animal* Factory::createAnimal();
  Food* Factory::createFood();
};

concept GenericFood&lt;typename Food&gt;
{
  GenericFactory Factory = Food::Factory;
  int Food::quality();
};

concept GenericAnimal&lt;typename Animal&gt;
{
  GenericFactory Factory = Animal::Factory;
  void Animal::eat(GenericFactory::Food* f);
};

class CowFactory;

class Grass : public GenericFood!
{
public:
  typedef CowFactory Factory;
  int quality() { return 0; }
};

class Cow : public GenericAnimal!
{
public:
  typedef CowFactory Factory;
  void eat(Grass* grass) { }
};

class CowFactory : public GenericFactory!
{
public:
  typedef Cow Animal;
  typedef Grass Food;

  Cow* createAnimal() { return new Cow; }
  Grass* createFood() { return new Grass; }
};

class Client
{
public:
  void f(GenericFactory* factory) {
    typedef GenericFactory! conceptof(factory) Factory;
    Factory::Animal* animal = factory-&gt;createAnimal();
    Factory::Food* food = factory-&gt;createFood();
    animal-&gt;eat(food);
  }

  template &lt;GenericFactory! Factory&gt;
  void feed(Factory::Animal* animal,
            Factory::Food* food)
  {
    animal-&gt;eat(food);
  }
};</pre>
<p>Of course, these are only a bunch of ideas, and I cannot be sure that this approach is feasible. I would love to hear the opinion of some C++ expert about this stuff.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&blog=806312&post=19&subd=pcapriotti&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/04/03/covariance-versus-substitutivity/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>