<?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>Jürg Billeter's blog &#187; Vala</title>
	<atom:link href="http://blogs.gnome.org/juergbi/tag/vala/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.gnome.org/juergbi</link>
	<description>Just another GNOME Blogs weblog</description>
	<lastBuildDate>Sun, 20 Jun 2010 20:35:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Closures and asynchronous methods in Vala</title>
		<link>http://blogs.gnome.org/juergbi/2009/09/18/closures-and-asynchronous-methods-in-vala/</link>
		<comments>http://blogs.gnome.org/juergbi/2009/09/18/closures-and-asynchronous-methods-in-vala/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 22:22:36 +0000</pubDate>
		<dc:creator>juergbi</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[GNOME]]></category>
		<category><![CDATA[Vala]]></category>

		<guid isPermaLink="false">http://blogs.gnome.org/juergbi/?p=28</guid>
		<description><![CDATA[It&#8217;s time for the release of Vala 0.7.6. Tarballs are available from the GNOME FTP servers. This release includes a couple of new features worth explaining in a bit more detail. Closures While Vala has lambda expressions for a long time, they haven&#8217;t supported accessing local variables from the outer method. With the release of [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s time for the release of <a href="http://mail.gnome.org/archives/vala-list/2009-September/msg00168.html">Vala 0.7.6</a>. Tarballs are available from the <a href="http://download.gnome.org/sources/vala/0.7/">GNOME FTP servers</a>. This release includes a couple of new features worth explaining in a bit more detail.</p>
<h2>Closures</h2>
<p>While Vala has lambda expressions for a long time, they haven&#8217;t supported accessing local variables from the outer method. With the release of Vala 0.7.6 this has changed, and you can access and set any local variable from lambda expressions. One place where I&#8217;d expect this to be convenient is in signal handlers:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">void</span> main <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">init</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> args<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var window <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">Window</span> <span style="color: #008000;">&#40;</span>Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">WindowType</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TOPLEVEL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    window<span style="color: #008000;">.</span><span style="color: #0000FF;">set_default_size</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">300</span>, <span style="color: #FF0000;">50</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    window<span style="color: #008000;">.</span><span style="color: #0000FF;">destroy</span><span style="color: #008000;">.</span><span style="color: #0000FF;">connect</span> <span style="color: #008000;">&#40;</span>Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">main_quit</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var button <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">Button</span><span style="color: #008000;">.</span><span style="color: #0000FF;">with_label</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Click me!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    button<span style="color: #008000;">.</span><span style="color: #0000FF;">clicked</span><span style="color: #008000;">.</span><span style="color: #0000FF;">connect</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
        window<span style="color: #008000;">.</span><span style="color: #0000FF;">title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Closures in Vala&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    window<span style="color: #008000;">.</span><span style="color: #0000FF;">add</span> <span style="color: #008000;">&#40;</span>button<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    window<span style="color: #008000;">.</span><span style="color: #0000FF;">show_all</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Gtk<span style="color: #008000;">.</span><span style="color: #0000FF;">main</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You can even write recursive lambda expressions:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">int</span> Func <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">void</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	Func fib <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
	fib <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> i <span style="color: #008000;">:</span> <span style="color: #008000;">&#40;</span>fib <span style="color: #008000;">&#40;</span>i <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> fib <span style="color: #008000;">&#40;</span>i <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		message <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;%d&quot;</span>, fib <span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h2>Asynchronous methods</h2>
<p>The idea of integrating async methods into the language is to make it easier to write code that calls or implements async methods. Async methods are frequently used in I/O API such as GIO and in D-Bus clients and servers. Traditionally, writing async code has beeen quite painful as you often end up writing callback chains and completely lose track over the control flow in the process.</p>
<p>Experimental support for async methods has been implemented in Vala almost a year ago. However, the implementation was rather a proof of concept than a finished solution. While I still expect a few bugs in that area with 0.7.6, it&#8217;s a lot more robust and complete than it used to be a short time ago.</p>
<p>So how does it look like? It&#8217;s not unlike <a href="http://blogs.gnome.org/alexl/2008/09/16/async-io-made-easy-using-javascript/">Alex&#8217;s AsyncRunner in JavaScript</a> except that it&#8217;s even more concise as it&#8217;s fully integrated into the language. The following code defines an async method list_dir that asynchronously enumerates all files in the home directory:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">async <span style="color: #6666cc; font-weight: bold;">void</span> list_dir <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    var dir <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">new_for_path</span> <span style="color: #008000;">&#40;</span>Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">get_home_dir</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">try</span> <span style="color: #008000;">&#123;</span>
        var e <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">yield</span> dir<span style="color: #008000;">.</span><span style="color: #0000FF;">enumerate_children_async</span> <span style="color: #008000;">&#40;</span>FILE_ATTRIBUTE_STANDARD_NAME, <span style="color: #FF0000;">0</span>, Priority<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">DEFAULT</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            var files <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">yield</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">next_files_async</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span>, Priority<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">DEFAULT</span>, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>files <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var info <span style="color: #0600FF; font-weight: bold;">in</span> files<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                print <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;%s<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, info<span style="color: #008000;">.</span><span style="color: #0000FF;">get_name</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>GLib<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span> err<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        warning <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error: %s<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, err<span style="color: #008000;">.</span><span style="color: #0000FF;">message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">void</span> main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    list_dir<span style="color: #008000;">.</span><span style="color: #0000FF;">begin</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var loop <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GLib<span style="color: #008000;">.</span><span style="color: #0000FF;">MainLoop</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    loop<span style="color: #008000;">.</span><span style="color: #0000FF;">run</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you can see, it&#8217;s very easy to call the async methods enumerate_children_async and next_files_async in try-catch blocks and loops. There is no need for manually creating structs for user_data or similar inconveniences.</p>
<p>Async methods in Vala can also be used to implement D-Bus servers that can process multiple requests at the same time. Using it couldn&#8217;t be easier, just write an async method such as the above example, add it to a class annotated with <strong>[DBus (name = "org.example.Test")]</strong>, and register an instance of that class with the D-Bus connection. Vala also supports async methods on client-side D-Bus interfaces.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.gnome.org/juergbi/2009/09/18/closures-and-asynchronous-methods-in-vala/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blogs.gnome.org/juergbi/tag/vala/feed/ ) in 1.23075 seconds, on Feb 11th, 2012 at 9:26 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 11th, 2012 at 10:26 pm UTC -->
