<?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>Tore Vestues blogs &#187; Boo</title>
	<atom:link href="http://tore.vestues.no/category/boo/feed/" rel="self" type="application/rss+xml" />
	<link>http://tore.vestues.no</link>
	<description>On a quest for the silver bullet..</description>
	<lastBuildDate>Mon, 26 Jul 2010 12:22:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a dynamic xml reader with C# 4.0</title>
		<link>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/</link>
		<comments>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 21:28:49 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/</guid>
		<description><![CDATA[&#8220;The static type dynamic&#8221; was the catchphrase at PDC&#8217;08 when talking about what&#8217;s new in C# 4.0. The dynamic type seems to be introduced mainly to simplify the code you write when doing Com interop. But many also see this as a step towards the dynamic languages for C#. I like C#. I also like [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;The static type dynamic&#8221; was the catchphrase at PDC&#8217;08 when talking about what&#8217;s new in C# 4.0. The dynamic type seems to be introduced mainly to simplify the code you write when doing Com interop. But many also see this as a step towards the dynamic languages for C#. I like C#. I also like dynamic languages like python. In addition I am a big fan of the static language Boo that actually feels quite dynamic. This has given me a special interest in the dynamic type in C#.</p>
<p>One of the nice things (there are several others!) about dynamic languages like Python is that the code feels much cleaner. It is not bloated with type declarations all over. This clean feel was also the goal of the language Boo, and even though it is a static language, Boo really gives you that dynamic feel.</p>
<p>The question is the of course: Is the C# getting more dynamic?</p>
<p>Well to be honest I do not think so. Having to declare a type at all (even if the declaration is &#8220;dynamic&#8221;) feels quite static, and the code bloats like static languages. Most of the time. I think it is wrong to think that C# is a more dynamic language due to the dynamic keyword, and I feel you are not using the right tools if you try to use C# as a dynamic language. If you want to write dynamic code, use a dynamic language.</p>
<p>But when we get the dynamic keyword, lets find things to use it for. When I am introduced to new features I am not that interested in all the fancy things you can do with it, I want real value. And I think I have found an example of real value in the dynamic keyword. I&#8217;ll give you an example, other than in com-interop, where the dynamic keyword will help you keep your code nice and clean, where otherwise you&#8217;d have to write a whole lot of ugly code.</p>
<p><strong>The Quick Xml Reader</strong></p>
<p>The funny thing is that I have already implemented a nice xmlreader in Boo (using the IQuackFoo-interface). When the dynamic keyword was introduced in C# 4 and you&#8217;ve got the IDynamicObject from the DLR, I figured I can do the exact same thing in C#. That&#8217;s just cool!</p>
<p>So what did I do? Here&#8217;s the comment from the Boo code file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> XmlReader<span style="color: black;">&#40;</span>IQuackFu<span style="color: black;">&#41;</span>:
<span style="color: #483d8b;">&quot;&quot;&quot;
Represents an xmldocument using &quot;dynamic typing&quot;. 
Can be used to retrieve single values from the xml-document.
There is no support for retrieving lists.
&nbsp;
Language: 
 Element: Both nodes and leaves
 Node: an element containing sub-elements
 Leaf: An element that represents a value (in xml this is an attribute or a node with nodetype text)
&nbsp;
use method names to retrieve nodes, and properties to retrieve leaves
example:
&lt;myXml stat=&quot;ok&quot;&gt;
	&lt;user id=&quot;123&quot; nsid=&quot;123&quot;&gt;
	    &lt;username&gt;Tore&lt;/username&gt;
	    &lt;password&gt;foo&lt;/password&gt;
	&lt;/user&gt;
&lt;/myXml&gt;
&nbsp;
myXml as duck = XmlReader(xmlString)
myXml //will fail since &quot;myXml&quot; is no leaf
myXml() // will return a new XmlReaderrepresenting the rsp-node with children
myXml().stat // will return &quot;ok&quot;
myXml().user().username // will return &quot;Tore&quot;
&quot;&quot;&quot;</span></pre></td></tr></table></div>

<p>How cool is that? This is dynamic, resolved at run time, and it can now be done in C# as well. The point with parsing xml is that you won&#8217;t catch errors in the xml structure at compile time anyway. So why not just do the code dynamic?</p>
<p>So, our Xml reader let&#8217;s you easily get values from an xml structure without having to declare all the types and without having to worry about all the details of the xml-parsing that normally is a bit complex and frankly, boring. In addition the code is much more readable than normal xml-parsing code.</p>
<p><strong>Xml reading in C#</strong></p>
<p>So, how do we do this in C#? Our XmlReader is a class where you can use any method name or property name on it, and it will still compile. For example, This will compile:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">dynamic ourReader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span>xmlString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ourReader.<span style="color: #0000FF;">WhateverMethodNameYouLike</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>To do this, the XmlReader needs to inherit the IDynamicObject, and it has to implement the method GetMetaObject that returns a MetaObject. </p>
<p><strong>Simplifying the MetaObject</strong></p>
<p>To implement a MetaObject and do some useful stuff you actually have to understand how to build a Linq expression tree that describes the action to be taken. If people are going to use this, it should be as simple as possible. Building Linq expression trees is not as simple as possible IMHO. So, I&#8217;ll make a generic base class that hides the whole MetaObject with its Linq expression trees, and let you override some methods in your XmlReader class instead.</p>
<p>I will not go into details on how I have done this, but if you&#8217;re interested, you can look at the source (there&#8217;s a link at the end). The &#8216;magic&#8217; is within the MetaBaseDynamicObject. Together with the BaseDynamicObject these can be reused for different tasks that needs on the fly interpreting of property and method calls.</p>
<p>All <i>you</i> have to do is to inherit the BaseDynamicObject, and override any of these methods:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> BaseDynamicObject<span style="color: #008000;">:</span>IDynamicObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a method on this object is called run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the method that has been called&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;args&quot;&gt;parameters on the method call&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The return value for the originally called method&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">Object</span> Call<span style="color: #000000;">&#40;</span>CallAction action, <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a property is set on this object run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the property which value is attemted set&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;value&quot;&gt;the value that the property is attemted set with&lt;/param&gt;    </span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> SetMember<span style="color: #000000;">&#40;</span>SetMemberAction action, <span style="color: #FF0000;">object</span> value<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// If a property is called on this object run time, this method will be called.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;action&quot;&gt;Information on the property which is being called&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;The return value for the property that has been called&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #FF0000;">Object</span> GetMember<span style="color: #000000;">&#40;</span>GetMemberAction action<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//, MetaObject[] args)</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Implementing the XmlReader</strong></p>
<p>So what we have to do here is to create a class (XmlReader), inherit the BaseDynamicObject, and override Call and GetMember. We will not bother with SetMember now, as we do not want to support updating the xml. This is what it looks like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> XmlReader<span style="color: #008000;">:</span>BaseDynamicObject
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> XmlElement element<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> XmlReader<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> xmlText<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        var doc <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlDocument<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        doc.<span style="color: #0000FF;">LoadXml</span><span style="color: #000000;">&#40;</span>xmlText<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        element <span style="color: #008000;">=</span> doc.<span style="color: #0000FF;">DocumentElement</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> XmlReader<span style="color: #000000;">&#40;</span>XmlElement xmlelement<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        element <span style="color: #008000;">=</span> xmlelement<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> Call<span style="color: #000000;">&#40;</span>CallAction action, <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var elements <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>elements.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> 
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span> <span style="color: #008000;">+</span> action.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' doesn't exist in xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsLeaf<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span> <span style="color: #008000;">+</span> action.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;' is a Leaf and cannot be fetched as a Node&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> GetMember<span style="color: #000000;">&#40;</span>GetMemberAction action<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var elements <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">SelectNodes</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>elements.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsLeaf<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>XmlElement<span style="color: #000000;">&#41;</span>elements<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> elements<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">InnerText</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">else</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span><span style="color: #008000;">+</span>action.<span style="color: #0000FF;">Name</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;' is not a Leaf and cannot be fetched one&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        var attribute <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">GetAttributeNode</span><span style="color: #000000;">&#40;</span>action.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>attribute <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Element '&quot;</span><span style="color: #008000;">+</span>action.<span style="color: #0000FF;">Name</span><span style="color: #008000;">+</span><span style="color: #666666;">&quot;' doesn't exist in xml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> attribute.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsLeaf<span style="color: #000000;">&#40;</span>XmlElement leafElement<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> leafElement.<span style="color: #0000FF;">ChildNodes</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&amp;&amp;</span>
            leafElement.<span style="color: #0000FF;">ChildNodes</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">NodeType</span> <span style="color: #008000;">==</span> XmlNodeType.<span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>This class now does all the xml-handling. So that means you can do stuff like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> xmlString <span style="color: #008000;">=</span> 
	<span style="color: #666666;">&quot;&lt;?xml version=<span style="color: #008080; font-weight: bold;">\&quot;</span>1.0<span style="color: #008080; font-weight: bold;">\&quot;</span> encoding=<span style="color: #008080; font-weight: bold;">\&quot;</span>utf-8<span style="color: #008080; font-weight: bold;">\&quot;</span> ?&gt;&quot;</span> <span style="color: #008000;">+</span>
	<span style="color: #666666;">&quot;&lt;myXml stat=<span style="color: #008080; font-weight: bold;">\&quot;</span>ok<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #008000;">+</span>
		<span style="color: #666666;">&quot;&lt;user id=<span style="color: #008080; font-weight: bold;">\&quot;</span>321<span style="color: #008080; font-weight: bold;">\&quot;</span> nsid=<span style="color: #008080; font-weight: bold;">\&quot;</span>123<span style="color: #008080; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #008000;">+</span>
		    <span style="color: #666666;">&quot;&lt;username&gt;Tore&lt;/username&gt;&quot;</span> <span style="color: #008000;">+</span>
		    <span style="color: #666666;">&quot;&lt;password&gt;foo&lt;/password&gt;&quot;</span> <span style="color: #008000;">+</span>
		<span style="color: #666666;">&quot;&lt;/user&gt;&quot;</span> <span style="color: #008000;">+</span>
	<span style="color: #666666;">&quot;&lt;/myXml&gt;&quot;</span><span style="color: #008000;">;</span>
dynamic myXml <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlReader<span style="color: #000000;">&#40;</span>xmlString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">stat</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;ok&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">user</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">username</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;Tore&quot;</span>
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>myXml.<span style="color: #0000FF;">user</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">nsid</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// &quot;123&quot;</span></pre></td></tr></table></div>

<p>This is a real declarative and easy way to handle xml, and since xml will only fail run time anyway, there is no argument that you lose compile time checking either.</p>
<p>I hope that has triggered you a bit on some exciting possibilities on using the dynamic type.</p>
<p><a href='http://vestues.no/tore/wp-content/dynamic.zip' title='Dynamic C#4.0 Xml example'>Here&#8217;s</a> the source. Please remember you will need C# 4.0 to make this work.</p>
<p>Thanks to <a href="http://saftsack.fs.uni-bayreuth.de/~dun3/archives/first-look-ducktyping-c-4-0-idynamicobject-metaobject/202.html">this</a> and <a href="http://blogs.msdn.com/cburrows/archive/2008/10/28/c-dynamic-part-ii.aspx">this</a> blog for giving me information and ideas.</p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2009/01/05/creating-a-dynamic-xml-reader-with-c-40/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Boo extensibility tutorials</title>
		<link>http://tore.vestues.no/2008/12/27/the-boo-extensibility-tutorials/</link>
		<comments>http://tore.vestues.no/2008/12/27/the-boo-extensibility-tutorials/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 07:32:35 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/27/the-boo-extensibility-tutorials/</guid>
		<description><![CDATA[Boo&#8217;s compiler extensibility is an extremely nice feature which sometimes make me talk about the next generation of programming languages. For me it&#8217;s about being able to create frameworks in a more powerful way, not being restricted by language syntax.
One problem with Boo is that lack of tutorials and help to everyone that wants to [...]]]></description>
			<content:encoded><![CDATA[<p>Boo&#8217;s compiler extensibility is an extremely nice feature which sometimes make me talk about the next generation of programming languages. For me it&#8217;s about being able to create frameworks in a more powerful way, not being restricted by language syntax.</p>
<p>One problem with Boo is that lack of tutorials and help to everyone that wants to start out with Boo. I have spent quite some time scanning the Boo source code, trying and failing, and forever searching the web for information, never finding anything. It&#8217;s been a long road for me to learn the language, and I do not think it should be this hard for everyone else.</p>
<p>So, I&#8217;ve spent some time writing some tutorials on Boo&#8217;s compiler extensibility. Hope they help!</p>
<ul>
<li><a href="http://tore.vestues.no/2008/09/28/what-makes-boo-great/">What makes Boo great</a></li>
<li><a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Boo Getting Started</a></li>
<li><a href="http://tore.vestues.no/2008/12/06/boo-extensions-explained/">Boo extensions explained</a></li>
<li><a href="http://tore.vestues.no/2008/12/18/boo-astattributes-explained/">Boo AstAttributes explained</a></li>
<li><a href="http://tore.vestues.no/2008/12/22/boo-astmacros-explained/">Boo AstMacros explained</a></li>
</ul>
<p>Feel free to contact me with any questions, and also if there are other topics you want covered.</p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/27/the-boo-extensibility-tutorials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Boo AstMacros explained</title>
		<link>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/</link>
		<comments>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 21:15:39 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/22/boo-astmacros-explained/</guid>
		<description><![CDATA[In this post I am going to explain how you write your own macros in Boo. Writing macros is a powerful way to use the compiler extensibility built into Boo. Macros in Boo actually let you create your own keywords which are resolved at compile time.
Before you read on, these posts might be useful to [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am going to explain how you write your own macros in Boo. Writing macros is a powerful way to use the compiler extensibility built into Boo. Macros in Boo actually let you create your own keywords which are resolved at compile time.</p>
<p>Before you read on, these posts might be useful to read:</p>
<ul>
<li><a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Boo Getting Started</a></li>
<li><a href="http://tore.vestues.no/2008/12/06/boo-extensions-explained/">Boo extensions explained</a></li>
<li><a href="http://tore.vestues.no/2008/12/18/boo-astattributes-explained/">Boo AstAttributes explained</a></li>
</ul>
<p>To create a Macro, you simply have to create a class that inherits from AbstractAstMacro and overrides the Expand-method. In addition your class name must end with &#8220;Macro&#8221;.</p>
<p>Let&#8217;s demonstrate with my DevNull-macro:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DevNullMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>How do you use it? In your code (in a different assembly than where DevNullMacro is situated) you can write something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>:
	devNull:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>Here you send in a &#8220;Block&#8221; to the macro (&#8221;X>0&#8243;). You can actually also send arguments:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>:
	devNull x,<span style="color: #ff4500;">10</span>,<span style="color: #483d8b;">&quot;Hello&quot;</span>:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>So, a Macro can take any number of arguments, and then it takes a block (which can be as big as you like).</p>
<p>Now why did I call this macro DevNull? I did it to demonstrate how the macros work. The point is if you choose to do nothing in the Expand method, nothing will happen. The compiler will actually remove the macro with its arguments and its Block. Meaning that since it is only our macro that handles the code inside the block sent into the macro we can send in whatever we&#8217;d like. </p>
<p>This code actually compiles:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> DoTheMath<span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>:
	devNull x,<span style="color: #ff4500;">10</span>,<span style="color: #483d8b;">&quot;Hello&quot;</span>:
		x<span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>
		I_can_write_whatever_here_actually</pre></td></tr></table></div>

<p>Ok, so when the compiler hits a macro statement in the compiling code, it finds the macro implementation (in this case the DevNullMacro), and sends it both the arguments and the code block. The compiler then removes all that code, and continues. </p>
<p>(I know this might seem a little confusing. When I started learning Boo it helped quite a lot to look at the compiled code using a tool like Reflector. You should try to write this code and look at it. It will clear up a few things, believe me.)</p>
<p>So what is the point then? The point is that within the macro, you can add code to the code structure. You can of course add whatever you like, but normally you add something based on the input to the macro.</p>
<p>To demonstrate something useful, I will show you how to implement <a href="http://en.wikipedia.org/wiki/Design_by_contract">Design By Contract</a>. I have previously demonstrated it in several talks I&#8217;ve held, but then I have implemented it as AstAttributes like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Account:	
	public Balance <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>
&nbsp;
	<span style="color: black;">&#91;</span>Require<span style="color: black;">&#40;</span>amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: black;">&#91;</span>Ensure<span style="color: black;">&#40;</span>Balance == originalBalance + amount<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> Deposit<span style="color: black;">&#40;</span>amount <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span>:
		originalBalance = Balance
		Balance += amount</pre></td></tr></table></div>

<p>I will not go into details about this, but I&#8217;m pretty sure you will manage to do it yourself after reading <a href="http://tore.vestues.no/2008/12/18/boo-astattributes-explained/">my post about AstAttributes</a>.</p>
<p>Now I want to implement Design By Contract as macros, in the same way it is implemented in Eiffel, the language that introduced it. I want to write my assertions like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Account:
&nbsp;
	public Balance <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> Deposit<span style="color: black;">&#40;</span>amount <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span>:
		requires:
			amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span>
		body:
			originalBalance = Balance
			Balance += amount
		ensures:
			Balance == originalBalance + amount</pre></td></tr></table></div>

<p>We have three macros here: requires, body, and ensures. I&#8217;m going to show you in detail how to implement the requires-part of the code, then I&#8217;ll add the source for both body and ensures as well. When we hit the requires macro in the code, we&#8217;re going to replace it with some actual code that forces those requirements. So, where we find this code in our Deposit-method,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">requires:
	amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span></pre></td></tr></table></div>

<p>we want, at compile time, to put something like this to the output assembly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: black;">&#40;</span>amount<span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Some suitable error message&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>To add code where the macro statement is situated, we simply return a Statement from our Expand-method. What we want to make is a Block (which is a derived type of Statement):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
	blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn</pre></td></tr></table></div>

<p>Still nothing happens, since we&#8217;re just returning an empty Block. We have to fill the return block with something. So for each assertion (which turns out as Statements) in our code, we want to assert it. Let&#8217;s loop all our statements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
	blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> statement <span style="color: #ff7700;font-weight:bold;">in</span> macro.<span style="color: black;">Block</span>.<span style="color: black;">Statements</span>:
		expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression = TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement<span style="color: black;">&#41;</span>
		blockToReturn.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>
			CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn</pre></td></tr></table></div>

<p>In addition to the looping, which is pretty straight forward, we&#8217;re doing two new things here. We&#8217;re trying to turn every statement into a BinaryExpression, and we&#8217;re creating blocks for each BinaryExpression.</p>
<p>Let&#8217;s look at our TryMakeBinaryExpression first. The point is that if we want to assert something, we have to make sure they resolve to either true or false. First we need all the Statements to be ExpressionStatements. If you for instance had written an if-statement in you macro like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">requires:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">1</span>==<span style="color: #ff4500;">1</span>:
		do_something</pre></td></tr></table></div>

<p>This will not be an ExpressionStatement, it is an IfStatement (you should look into the Boo class-hierarchy, use Visual studio or Reflector). </p>
<p>So, when we have assured us it is all ExpressionStatements we have to deal with, we also want to ensure they are all BinaryExpressions. They all have to resolve into either true or false. Both of these checks are run here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement <span style="color: #ff7700;font-weight:bold;">as</span> Statement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression:
	expression = statement <span style="color: #ff7700;font-weight:bold;">as</span> ExpressionStatement 
	<span style="color: #ff7700;font-weight:bold;">if</span> expression <span style="color: #ff7700;font-weight:bold;">is</span> null:
		RaiseNotBinaryException<span style="color: black;">&#40;</span>statement.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	binaryexpression = expression.<span style="color: black;">Expression</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression 
	<span style="color: #ff7700;font-weight:bold;">if</span> binaryexpression <span style="color: #ff7700;font-weight:bold;">is</span> null:
		RaiseNotBinaryException<span style="color: black;">&#40;</span>expression.<span style="color: black;">Expression</span>.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">return</span> binaryexpression</pre></td></tr></table></div>

<p>Ok, now we either have a BinaryExpression or it has already failed. Let&#8217;s make it into a block:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Block:
	exceptionText = <span style="color: #483d8b;">&quot;Voilated precondition: &quot;</span> + expression.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>|
			block:
				<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> $<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span>:
					<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span>$exceptionText<span style="color: black;">&#41;</span>
		|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span></pre></td></tr></table></div>

<p>Here we&#8217;re using quasiquotations again. It&#8217;s understandable and easy to both read and write.</p>
<p>And that&#8217;s the complete macro. So now for every BinaryExpression written, the macro will assert its correctness. Here&#8217;s the complete code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RequiresMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
		blockToReturn = Block<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> statement <span style="color: #ff7700;font-weight:bold;">in</span> macro.<span style="color: black;">Block</span>.<span style="color: black;">Statements</span>:
			expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression = TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement<span style="color: black;">&#41;</span>
			blockToReturn.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>
				CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> blockToReturn
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> CreateAssertblockFromBinaryExpression<span style="color: black;">&#40;</span>expression <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Block:
		exceptionText = <span style="color: #483d8b;">&quot;Voilated precondition: &quot;</span> + expression.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>|
				block:
					<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> $<span style="color: black;">&#40;</span>expression<span style="color: black;">&#41;</span>:
						<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span>$exceptionText<span style="color: black;">&#41;</span>
			|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> TryMakeBinaryExpression<span style="color: black;">&#40;</span>statement <span style="color: #ff7700;font-weight:bold;">as</span> Statement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression:
		expression = statement <span style="color: #ff7700;font-weight:bold;">as</span> ExpressionStatement 
		<span style="color: #ff7700;font-weight:bold;">if</span> expression <span style="color: #ff7700;font-weight:bold;">is</span> null:
			RaiseNotBinaryException<span style="color: black;">&#40;</span>statement.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
		binaryexpression = expression.<span style="color: black;">Expression</span> <span style="color: #ff7700;font-weight:bold;">as</span> BinaryExpression 
		<span style="color: #ff7700;font-weight:bold;">if</span> binaryexpression <span style="color: #ff7700;font-weight:bold;">is</span> null:
			RaiseNotBinaryException<span style="color: black;">&#40;</span>expression.<span style="color: black;">Expression</span>.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">return</span> binaryexpression
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> RaiseNotBinaryException<span style="color: black;">&#40;</span>typeInfo <span style="color: #ff7700;font-weight:bold;">as</span> String<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Not a binary expression: &quot;</span> + typeInfo<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The other two macros, BodyMacro and EnsuresMacro are listed here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BodyMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> Statement:
		<span style="color: #ff7700;font-weight:bold;">return</span> macro.<span style="color: black;">Block</span></pre></td></tr></table></div>

<p>Here we only need to return the actual code written within the macro.</p>
<p>And here&#8217;s the EnsuresMacro:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> EnsuresMacro<span style="color: black;">&#40;</span>AbstractAstMacro<span style="color: black;">&#41;</span>:
&nbsp;
	override <span style="color: #ff7700;font-weight:bold;">def</span> Expand<span style="color: black;">&#40;</span>macro <span style="color: #ff7700;font-weight:bold;">as</span> MacroStatement<span style="color: black;">&#41;</span>:
		ancestor <span style="color: #ff7700;font-weight:bold;">as</span> Method = macro.<span style="color: black;">GetAncestor</span><span style="color: black;">&#40;</span>NodeType.<span style="color: black;">Method</span><span style="color: black;">&#41;</span>
&nbsp;
		oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = ancestor.<span style="color: black;">Body</span>
		ensuresBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = RequiresMacro<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Expand</span><span style="color: black;">&#40;</span>macro<span style="color: black;">&#41;</span>
		newBody = <span style="color: black;">&#91;</span>|
			block:
				<span style="color: #ff7700;font-weight:bold;">try</span>:
					$oldBody
				ensure:
					$ensuresBody
		|<span style="color: black;">&#93;</span>.<span style="color: black;">Block</span>
&nbsp;
		ancestor.<span style="color: black;">Body</span> = newBody</pre></td></tr></table></div>

<p>Here we take all the code in the method containing the macro, and surrounds it with a try-clause to ensure that we always run the ensures-part of the code. Notice also that we&#8217;re using the RequireMacro, since it creates the assert-block exactly the way we want it.</p>
<p>A helping hand:<br />
When you explore this, I actually advice you to write some of your macro code in C#, as it gives you a lot more intellisense-help, then when you have found out what you want, write them back in Boo, as it is often much more readable, especially with the nice quasiquotation which you do not have in C#.</p>
<p>Hope it helps!</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/22/boo-astmacros-explained/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Boo AstAttributes explained</title>
		<link>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/</link>
		<comments>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 22:31:50 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/18/boo-astattributes-explained/</guid>
		<description><![CDATA[Writing extensions for Boo is a very powerful thing. In this post I&#8217;m going to explain how to write AstAttributes in Boo. These attributes are much more than normal .net attributes. They are one of the ways you can extend the Boo language.
Before you read on, these posts might be useful to read:

Boo Getting Started
Boo [...]]]></description>
			<content:encoded><![CDATA[<p>Writing extensions for Boo is a very powerful thing. In this post I&#8217;m going to explain how to write AstAttributes in Boo. These attributes are much more than normal .net attributes. They are one of the ways you can extend the Boo language.</p>
<p>Before you read on, these posts might be useful to read:</p>
<ul>
<li><a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Boo Getting Started</a></li>
<li><a href="http://tore.vestues.no/2008/12/06/boo-extensions-explained/">Boo extensions explained</a></li>
</ul>
<p>To implement an AstAttribute you have to create a class that inherits the AbstractAstAttribute-class and implements the Apply-method. The class name must be postfixed with &#8220;Attribute&#8221;, the syntax is then &#8220;&#8216;YourAttributeName&#8217;Attribute&#8221;. Like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DemoAttribute<span style="color: black;">&#40;</span>AbstractAstAttribute<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span><span style="color: #008000;">type</span> <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>You have now created an AstAttribute named Demo, and you can use it (from another assembly) like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>Demo<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> MyTestClass:
	<span style="color: #ff7700;font-weight:bold;">def</span> constructor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>For the moment our attribute does absolutely nothing. To make it do something we have to do some work in the Apply-method we implemented. The method takes a Node as an argument. A Node can be just about any type, since alot of classes inherit the Node-class. Here the Node is the actual &#8220;thing&#8221; that you tagged the Attribute with. In our example the Node will be a definition of the MyTestClass. Say you tag a Method you will get the method definition (including all its content) passed into the Apply method. We will examine the possibilities this gives us, but let us first take a look at what sub types the Node-class has, that are relevant for us.</p>
<p><a href='http://vestues.no/tore/wp-content/classdefinitioninheritance.png' title='Boo ClassDefinition inheritance picture'><img src='http://vestues.no/tore/wp-content/classdefinitioninheritance.thumbnail.png' alt='Boo ClassDefinition inheritance picture' /></a></p>
<p>The picture shows the relevant subtypes. In other words these subtypes show where you can use your attribute, meaning you can tag classes, method parameters, methods, constructors, fields and properties with an AstAttribute that you create.</p>
<p>To experiment with this, I&#8217;ve made a little solution for you that you, it will print just about any information that is available at compile-time. (I&#8217;ve added it at the bottom)</p>
<p><strong>NotifyPropertyChanged</strong></p>
<p>I want to demonstrate an AstAttribute that I&#8217;ve been using in quite a few of the presentations I&#8217;ve held on Boo, and that I have also mentioned in this blog earlier (<a href="http://tore.vestues.no/2008/09/28/what-makes-boo-great/">What makes boo great</a>): the NotifyPropertyChanged-attribute. I wrote this attribute about half a year ago, and it really blew my mind on all the possibilities Boo offers.</p>
<p>First let&#8217;s look at how we implement the INotifyPropertyChanged in C#, just to remind us of what parts we want to auto generate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer<span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> firstName<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> lastName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> firstName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            firstName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> lastName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            lastName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> info<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>PropertyChanged <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            PropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #000000;">&#40;</span>info<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>That code is as ugly as always. We basically want to write it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>NotifyPropertyChanged<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> BooCustomer:
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>LastName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	lastName <span style="color: #ff7700;font-weight:bold;">as</span> String
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>FirstName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	firstName <span style="color: #ff7700;font-weight:bold;">as</span> String</pre></td></tr></table></div>

<p>Ok, the first thing we have to do is make sure the attribute is used on a class and not somewhere else. Let&#8217;s do it like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> GetClassDefinition<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition:
	classDef = target <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition
	<span style="color: #ff7700;font-weight:bold;">if</span> classDef <span style="color: #ff7700;font-weight:bold;">is</span> null:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;The NotifyPropertyChangedAttribute can only be used on classes&quot;</span><span style="color: black;">&#41;</span>		
	<span style="color: #ff7700;font-weight:bold;">return</span> classDef</pre></td></tr></table></div>

<p>We&#8217;re casting the Node to a ClassDefinition, which is the type it must be if the attribute is used on a class. Then we must add the INotifyPropertyChanged to the class definition. Notice that our BooCustomer hasn&#8217;t implemented it, since it&#8217;s expecting us (the attribute) to do it.</p>
<p>Ok, lets add the code for that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
	AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AddInterface<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	classDef.<span style="color: black;">BaseTypes</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.INotifyPropertyChanged&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>That was simple! Next we need to add the actual Event:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
	classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
	AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
	AddEvent<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AddEvent<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	notifyEvent = Event<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	notifyEvent.<span style="color: black;">Type</span> = SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.PropertyChangedEventHandler&quot;</span><span style="color: black;">&#41;</span>
	notifyEvent.<span style="color: black;">Name</span> = <span style="color: #483d8b;">&quot;PropertyChanged&quot;</span>
	classDef.<span style="color: black;">Members</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>notifyEvent<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Here we create an event, set the right type, set the name, and simply add it to our classdefinition. </p>
<p>The last thing to do is to find all the properties, and add some code to the setter of those properties. This is how we find all the properties:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> AddToAllProperties<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> member <span style="color: #ff7700;font-weight:bold;">in</span> classDef.<span style="color: black;">Members</span>:
		<span style="color: #008000;">property</span> = member <span style="color: #ff7700;font-weight:bold;">as</span> Property
		<span style="color: #ff7700;font-weight:bold;">continue</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">is</span> null
		AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>And to conclude it, we must implement the AddNewBody-method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">as</span> Property<span style="color: black;">&#41;</span>:
	oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = <span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span>
	<span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span> = <span style="color: black;">&#91;</span>|
		$oldBody
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>PropertyChanged <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> null<span style="color: black;">&#41;</span>:
			PropertyChanged<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,System.<span style="color: black;">ComponentModel</span>.<span style="color: black;">PropertyChangedEventArgs</span><span style="color: black;">&#40;</span>$<span style="color: black;">&#40;</span><span style="color: #008000;">property</span>.<span style="color: black;">Name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	|<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Ok, here&#8217;s something new. The [|...|]-tags. You probably can understand what is happening here, but not really why. The syntax is easy to both read and write. It&#8217;s called quasi quotation. I will describe it further in another post.</p>
<p>That concludes our attribute! Here&#8217;s the complete code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> System
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Boo.<span style="color: black;">Lang</span>.<span style="color: black;">Compiler</span>.<span style="color: black;">Ast</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> NotifyPropertyChangedAttribute<span style="color: black;">&#40;</span>AbstractAstAttribute<span style="color: black;">&#41;</span>:
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> Apply<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span>:
		classDef = GetClassDefinition<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
		AddInterface<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
		AddEvent<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
		AddToAllProperties<span style="color: black;">&#40;</span>classDef<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> GetClassDefinition<span style="color: black;">&#40;</span>target <span style="color: #ff7700;font-weight:bold;">as</span> Node<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition:
		classDef = target <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition
		<span style="color: #ff7700;font-weight:bold;">if</span> classDef <span style="color: #ff7700;font-weight:bold;">is</span> null:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;The NotifyPropertyChangedAttribute can only be used on classes&quot;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> classDef
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddInterface<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		classDef.<span style="color: black;">BaseTypes</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.INotifyPropertyChanged&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddEvent<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		notifyEvent = Event<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		notifyEvent.<span style="color: black;">Type</span> = SimpleTypeReference<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;System.ComponentModel.PropertyChangedEventHandler&quot;</span><span style="color: black;">&#41;</span>
		notifyEvent.<span style="color: black;">Name</span> = <span style="color: #483d8b;">&quot;PropertyChanged&quot;</span>
		classDef.<span style="color: black;">Members</span>.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>notifyEvent<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddToAllProperties<span style="color: black;">&#40;</span>classDef <span style="color: #ff7700;font-weight:bold;">as</span> ClassDefinition<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> member <span style="color: #ff7700;font-weight:bold;">in</span> classDef.<span style="color: black;">Members</span>:
			<span style="color: #008000;">property</span> = member <span style="color: #ff7700;font-weight:bold;">as</span> Property
			<span style="color: #ff7700;font-weight:bold;">continue</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">is</span> null
			AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> AddNewBody<span style="color: black;">&#40;</span><span style="color: #008000;">property</span> <span style="color: #ff7700;font-weight:bold;">as</span> Property<span style="color: black;">&#41;</span>:
		oldBody <span style="color: #ff7700;font-weight:bold;">as</span> Block = <span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span>
		<span style="color: #008000;">property</span>.<span style="color: black;">Setter</span>.<span style="color: black;">Body</span> = <span style="color: black;">&#91;</span>|
			$oldBody
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>PropertyChanged <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> null<span style="color: black;">&#41;</span>:
				PropertyChanged<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,System.<span style="color: black;">ComponentModel</span>.<span style="color: black;">PropertyChangedEventArgs</span><span style="color: black;">&#40;</span>$<span style="color: black;">&#40;</span><span style="color: #008000;">property</span>.<span style="color: black;">Name</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		|<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Using that code, you can now write classes like this that actually implement the complete INotifyPropertyChanged-behaviour:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">&nbsp;
&nbsp;
<span style="color: black;">&#91;</span>NotifyPropertyChanged<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> BooCustomer:
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>LastName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	lastName <span style="color: #ff7700;font-weight:bold;">as</span> String
	<span style="color: black;">&#91;</span>Property<span style="color: black;">&#40;</span>FirstName<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
	firstName <span style="color: #ff7700;font-weight:bold;">as</span> String</pre></td></tr></table></div>

<p>Try it yourself and look at the code through reflector to see what is really going on here. Remember to put the attribute and the classes using it in different assemblies.</p>
<p>Any comments on how to improve or clarify any of this will be appreciated.</p>
<p>The solution that prints alot of info on your attribute, and shows where you can place it: <a href='http://vestues.no/tore/wp-content/attributeexplorer.zip' title='Boo AttributeExplorer-solution'>Boo AttributeExplorer-solution</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/18/boo-astattributes-explained/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Boo extensions explained</title>
		<link>http://tore.vestues.no/2008/12/06/boo-extensions-explained/</link>
		<comments>http://tore.vestues.no/2008/12/06/boo-extensions-explained/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 17:41:58 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/12/06/boo-extensions-explained/</guid>
		<description><![CDATA[Boo&#8217;s compiler extensibility means that you can write extensions that run in the compiler when your Boo code compiles. This powerful feature enables you to actually extend the Boo language itself. In this post I will try to explain what this basically means.
Say you want to make a new keyword in Boo, called &#8220;WriteHelloWorld&#8221; (yes, [...]]]></description>
			<content:encoded><![CDATA[<p>Boo&#8217;s compiler extensibility means that you can write extensions that run in the compiler when your Boo code compiles. This powerful feature enables you to actually extend the Boo language itself. In this post I will try to explain what this basically means.</p>
<p>Say you want to make a new keyword in Boo, called &#8220;WriteHelloWorld&#8221; (yes, it&#8217;s an extremely bad example, but I want to keep it as simple as possible). Boo does not understand the word &#8220;WriteHelloWorld&#8221; by default, but you can make it understand. What you have to do is write a little &#8220;plug-in&#8221; to the compiler.</p>
<p>The &#8220;WriteHelloWorld&#8221; plug-in have to be implemented in a separate assembly from the assembly that is using the keyword. This is because the plug-in assembly with the &#8220;WriteHelloWorld&#8221;-functionality has to be run in the compiler and thus already be compiled during the compilation of your main assembly. I&#8217;ve added an illustration that I use in some of my Boo-presentations.</p>
<p><a href='http://vestues.no/tore/wp-content/boocompilerextensions.png' title='Boo extensions explained'><img src='http://vestues.no/tore/wp-content/boocompilerextensions.thumbnail.png' alt='Boo extensions explained' /></a></p>
<p>When your main assembly is under compilation, the &#8220;WriteHelloWorld&#8221;-plug-in will be asked for help every time the compiler hits the &#8220;WriteHelloWorld&#8221;-keyword.</p>
<p>I&#8217;ve implemented the WriteHelloWorld as a meta method, since it is the easiest way to create new keywords if you do not need to work with the ast-tree.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>Meta<span style="color: black;">&#93;</span>
static <span style="color: #ff7700;font-weight:bold;">def</span> WriteHelloWorld<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>|
		Console.<span style="color: black;">WriteLine</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello World!&quot;</span><span style="color: black;">&#41;</span>
	|<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>This method tells the compiler that when it finds the &#8220;WriteHelloWorld&#8221; keyword, it should replace it with the code &#8220;Console.Writeline(&#8221;Hello World!&#8221;). And it does, if you look at the output assembly (using a tool like Reflector), you will see that this is exactly what the Boo-compiler has written. Remember that this is compile-time. Within the compiler process itself. The WriteHelloWorld-metamethod will never be executed run-time, it won&#8217;t exist after compilation. </p>
<p>Notice also that the main assembly has to reference the plug-in-assembly. If you do not do that, the compiler does not know where to find the plug-in.</p>
<p>This is how all extensions/plug-ins work in Boo. The compiler asks a plug-in to interpret what a keyword/attribute means, and gives the plug-in control of what to do. If you use Macros or Attributes you will also be able to access the compiler-tree, enabling you to manipulate the whole compiler process. For even more power you can add custom steps to the compiler pipeline, where you can do literally anything you want to with the code. More about that another time.</p>
<p>I&#8217;ve attached the solution that implements this. Read <a href="http://tore.vestues.no/2008/09/09/boo-getting-started/">Getting Started</a> to see what you need to run the solution.</p>
<p><a href='http://vestues.no/tore/wp-content/booextensionexample.zip' title='Boo extensions explained Solution'>Boo extensions explained Solution</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/12/06/boo-extensions-explained/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Microsoft should introduce compiler extensibility</title>
		<link>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/</link>
		<comments>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 21:54:22 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>
		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/</guid>
		<description><![CDATA[At PDC08 I attended a presentation about “Contracts” (and Pex). “Contracts” is the result of the Spec# work. It is an attempt to enable Design by Contract (DbC) in .Net. Design by Contract is a concept popularized by Bertrand Meyer, and it is fully implemented and integrated in the language Eiffel. 
Microsoft decided that DbC [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://microsoftpdc.com/">PDC08</a> I attended a presentation about <a href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL51.wmv">“Contracts” (and Pex)</a>. “Contracts” is the result of the Spec# work. It is an attempt to enable <a href="http://en.wikipedia.org/wiki/Design_by_contract">Design by Contract (DbC)</a> in .Net. Design by Contract is a concept popularized by <a href="http://en.wikipedia.org/wiki/Bertrand_Meyer">Bertrand Meyer</a>, and it is fully implemented and integrated in the language <a href="http://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a>. </p>
<p>Microsoft decided that DbC should not be a part of the languages themselves, but rather it should be offered as a standard library. I think this is unfortunate. If it had been a part of the language, the syntax could have been more declarative, and it probably would have given DbC more power. </p>
<p>The problem with having DbC as just another library is that you do not get any keywords, it is not allowed to be as declarative as it could have been, and the syntax gets unnecessary verbose. And besides, if it is just another library then this is really old news. There are several DbC libraries available on the .Net platform already. </p>
<p>But Contracts is actually a bit more than just another library. I believe the Contracts-team, not being allowed (I suspect) to make it a part of the languages, really wanted the power anyway. And so they got creative, and did a couple of interesting things.</p>
<p>First, they made tools as a plug-in to Visual Studio, and these tools are there to support the library. Second they actually added post compilation. In Contracts, you define all the Contracts-library code in the beginning of the method for readability (making it more declarative). But for this to execute correctly some of the code (the ensure-part), has to be at the end of the method. What Contracts&#8217; tools do is actually move that part of the code to the end of the method, after the code is compiled.</p>
<p>This is very, very interesting, and it raises the question: What is a library/framework? Normally it is just some code you can execute following the normal constraints of a language. What the Contracts-team has done is to extend the concept of what a library/framework is. They implicitly say a library/framework is both code and tools, meaning the tools can actually do some tweaking with the code after it has been written. In this case it is done for the sake of being declarative.</p>
<p>This means that when you create a library you get the power to add declarativeness to it, beyond the language. Now that is a very compelling concept! There are of course a couple of problems here. Creating these kinds of tools for your library is not a trivial task! Microsoft can do this because a) they&#8217;ve got the resources and b) because if all else fails, they can add support directly in the compiler, which they have already done on several occasions.</p>
<p>The pain here is of course, that Microsoft can do it, but you really can&#8217;t. It&#8217;s too much work, and you can forget about accessing the compiler. We as developers are not enabled to have this power.</p>
<p>I&#8217;ve been playing around with <a href="http://boo.codehaus.org/">Boo</a> for some time now, and I love the way Boo allows me to add keywords and to change things during compilation. My point is that Boo actually enables you to make frameworks that also incorporates tools the way the Contracts library does, and in addition it actually allows you to create keywords! And the best thing about it is that this is doable for all. It&#8217;s not very complicated. It really empowers us as developers to redefine the concept of frameworks.</p>
<p>If a framework can be functionality, tools and keywords then we enable us to write powerful, declarative, and less verbose code. Boo enables us. C#, sadly do not. </p>
<p><em>The very, very sad thing here is that during <a href="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL57.wmv">another session at PDC08</a>, Anders Hejlsberg &#8220;hinted strongly&#8221; that such compiler extensibility will never, ever be available in C#.</em></p>
<p>To demonstrate &#8220;Contracts&#8221;, this is how DbC is implemented:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">string</span> GetDescription<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> x<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
 Contract.<span style="color: #0000FF;">Requires</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&lt;</span>x<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 Contract.<span style="color: #0000FF;">Ensures</span><span style="color: #000000;">&#40;</span>Contract.<span style="color: #0000FF;">Result</span><span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
 <span style="color: #008080; font-style: italic;">// Method body ...</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>You add a set of Requires and Ensures at the top of your method. The Requires-statements must be true at the beginning of the method, and the Ensures statements must be true at the end of the method. This means that the Ensures statements must be executed last. This is where Microsoft uses the post compilation to move that part of the code after compilation.</p>
<p>I have actually already made a little library for DbC in Boo, it&#8217;s very easy to do (probably only took me an hour or so to implement). This is how you say the same thing with my little library:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>Require<span style="color: black;">&#40;</span>i<span style="color: #66cc66;">&lt;</span>x<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span>Ensure<span style="color: black;">&#40;</span>result<span style="color: #66cc66;">!</span>=null<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> GetDescription<span style="color: black;">&#40;</span>x <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> String: 
	result <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #dc143c;">string</span>
	// Method body ...</pre></td></tr></table></div>

<p>It&#8217;s got all the functionality of Contracts, it&#8217;s just as declarative (or probably even more), and in my own opinion it&#8217;s less verbose. And do not forget that it took me about an hour to implement the whole library! This is enabling developers to be more productive!</p>
<p>Why Microsoft does not want to enable us with this is beyond me. But regardless of Microsoft, there is nothing stopping you from using Boo!</p>
<p>-Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/10/31/why-microsoft-should-introduce-compiler-extensibility/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL57.wmv" length="220868989" type="video/x-ms-wmv" />
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/pdc08/WMV-HQ/TL51.wmv" length="313739159" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>What makes Boo great?</title>
		<link>http://tore.vestues.no/2008/09/28/what-makes-boo-great/</link>
		<comments>http://tore.vestues.no/2008/09/28/what-makes-boo-great/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 08:03:51 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/09/28/what-makes-boo-great/</guid>
		<description><![CDATA[About five years ago Rodrigo Barreto de Oliveira created the programming language Boo. It is a statically typed .Net language that incorporates some very interesting features not seen in any of the mainstream .net languages today. 
What makes Boo cool?
The cool thing about Boo is its no nonsense approach. I really enjoy its focus on [...]]]></description>
			<content:encoded><![CDATA[<p>About five years ago Rodrigo Barreto de Oliveira created the programming language Boo. It is a statically typed .Net language that incorporates some very interesting features not seen in any of the mainstream .net languages today. </p>
<p><strong>What makes Boo cool?</strong></p>
<p>The cool thing about Boo is its no nonsense approach. I really enjoy its focus on a clean, wrist-friendly syntax combined with expressiveness and simplicity. Boo is actually inspired by Python, and combining the best of Python with the .net framework and statical typing is quite powerful. </p>
<p>Boo makes the compiler work for you. The philosophy is, if the compiler can figure it out, you shouldn&#8217;t have to tell it. For instance, Boo has an automatic Type Inference. If the compiler can figure out what type a method returns or a field is, you do not have to declare it yourself. You can write</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> GetCount<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
 <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
&nbsp;
count = GetCount<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>instead of</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> getCount<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span>
 <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
&nbsp;
count <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #008000;">int</span> = GetCount<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>It&#8217;s not hard for the compiler to deduce that the getCount() method is returning an integer, so why should you have to tell it? That&#8217;s wrist-friendliness for you. Mind you, as long as the compiler can figure it out, both alternatives are valid Boo code. Then it is up to you if you want to declare the types or not.</p>
<p>Let&#8217;s look at the classic &#8220;Hello world&#8221; to demonstrate Boos expressiveness. In C# it is implemented something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HelloWorld
<span style="color: #000000;">&#123;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
 <span style="color: #000000;">&#123;</span>
   Console.<span style="color: #0000FF;">Writeline</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello, world!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
 <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In Boo, writing this line will do the same thing (you do not need a class or a method):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world!&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><a href="http://boo.codehaus.org/BooManifesto.pdf">The Boo manifesto</a> simply concludes, &#8220;The guys who came up with &#8216;public static void main&#8217; were probably kidding&#8221;. </p>
<p>Although Boo is inspired by Python, it is not just another Iron Python. It&#8217;s much more than that.</p>
<p><strong>What makes Boo great?</strong></p>
<p>These are examples of Boo&#8217;s no-nonsense approach and there are many more examples of this. I really like that the language itself is built on this philosophy. But what really makes Boo great is its compiler extensibility. This means that you can write extensions that run in the compiler when your Boo code compiles. This powerful feature enables you to actually extend the Boo language itself.</p>
<p>There are several ways to do this. You can write syntactic attributes. They look like regular .net custom attributes, but they are not. You can also make new keywords either with syntatic macros or meta methods. If these techniques are not fitting your needs you can hook up to the compiler pipeline and do virtually anything you want to with the output of the compiler process.</p>
<p>What all these techniques have in common is that custom code is run during compilation of your Boo code, and this custom code changes the output. The compiler takes the code as input, and asks your extensions to do their magic, and then creates the output which normally is an assembly. The magic your extensions do is to basically change whatever you&#8217;d like, giving you control of the output.</p>
<p>The great thing about this is that compiler extensibility is an excellent tool for removing boring, repetitive boiler plate code. This enables us to increase our productivity. Let me demonstrate how:</p>
<p><strong>INotifyPropertyChanged</strong></p>
<p>I hate implementing the INotifyPropertyChanged interface. To implement it I have to write a lot of boiler plate code. And as we all know, boiler plate code is boring to write, it&#8217;s often harder to maintain, and it bloats your otherwise elegant code. This is a problem since you need to implement the INotifyPropertyChanged interface to be able to use .Net&#8217;s data binding, and I have come to realize that data binding is an extremely nice tool when used correctly. </p>
<p>For those of you not familiar with Data binding and INotifyPropertyChanged: Data binding is about one object wanting to know when something changes in another object. It is really an effective implementation of the observer-pattern. Often this is used when a gui-control wants to know if the object it represents changes. To do this the gui-control is databound to the object it represents. In order for the data binding to work, the observed object needs to implement the INotifyPropertyChanged interface. This is all very nice and effective, except for all the boiler plate code in the observed object.</p>
<p>This is basically how the observed object is supposed to implement the INotifyPropertyChanged interface in C#:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer<span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> firstName<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> lastName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> FirstName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> firstName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            firstName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> LastName
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> lastName<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        set
        <span style="color: #000000;">&#123;</span>
            lastName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LastName&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> NotifyPropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> info<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>PropertyChanged <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            PropertyChanged<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #000000;">&#40;</span>info<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Man, this is simply too much code for just notifying when some properties change! I honestly do not believe that Microsoft is proud of this. The problem is that the language does not allow us to simplify it. Sadly this is probably the best solution within the mainstream .Net languages. Believe me, I&#8217;ve been looking for better ways to do this for more than a year, and I haven&#8217;t found a substantially better way. But that was before I found Boo. Because Boo is different. Boo gives you possibilities where other languages and frameworks must give in.</p>
<p>Let&#8217;s analyze the INotifyPropertyChanged-implementation. What is it that we really want to do? We want to notify when a property has changed. If we think a bit declarative, this should only be one line of code: &#8220;Notify observers when properties change&#8221;. And with Boo, you can actually do that. I&#8217;ve implemented a syntactic attribute called &#8220;NotifyPropertyChanged&#8221;, and when you tag your class with it, all the boiler plate code is added at compile time, so you no longer have to bother with the details. Look at this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>CreateProperties,NotifyPropertyChanged<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> BooCustomer:	
	public LastName <span style="color: #ff7700;font-weight:bold;">as</span> String
	public FirstName <span style="color: #ff7700;font-weight:bold;">as</span> String</pre></td></tr></table></div>

<p>Can you believe that this code does the same as the C# code above? Well it does. I have extended the compiler with two attributes. When the code compiles, the &#8220;CreateProperties&#8221;-attribute takes all public fields and turns them into properties, and the &#8220;NotifyPropertyChanged&#8221;-attribute adds all the code for notifying about the changes. When this code hits the compiler, all the boiler plate code is added to the output, leaving your code simple, clean and elegant.</p>
<p>I won&#8217;t go into the details here on how to create these compiler extensions, but I will say that it is a lot easier than you might fear. And as an additional goodie, you can write these extensions in any .net language you&#8217;d like.</p>
<p>The INotifyPropertyChanged example is just one example. I feel that repetitive boiler plate code is introduced more and more these days. The problem with this kind of code is that it&#8217;s hard to remove it with our mainstream .Net languages. Boo is the perfect tool for removing this kind of code. I believe that these possibilities can substantially increase our productivity in enterprise systems. That being said, I do not see Boo as a mainstream language in the near future. This is partly due to the lack of good tool support, and that the community is still too small. Instead I see Boo as a language that will be used in some suitable layers or components in a larger application written in a mainstream .Net language. </p>
<p>If you&#8217;d like to look into Boo, I recommend you start at the <a href="http://boo.codehaus.org">Boo homepage</a>.</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/09/28/what-makes-boo-great/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Boo: Getting started</title>
		<link>http://tore.vestues.no/2008/09/09/boo-getting-started/</link>
		<comments>http://tore.vestues.no/2008/09/09/boo-getting-started/#comments</comments>
		<pubDate>Tue, 09 Sep 2008 06:52:07 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Bits and bytes]]></category>
		<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/09/09/boo-getting-started/</guid>
		<description><![CDATA[Want to look into Boo? Let me give you a quick guide to getting the tools you need.
Development environment
For the time being I recommend you do your coding in Sharpdevelop. Download and install the latest  here. You should also download the latest Boo-distro (the binaries), and move (and overwrite) the Boo-files into The sharpdevelop [...]]]></description>
			<content:encoded><![CDATA[<p>Want to look into Boo? Let me give you a quick guide to getting the tools you need.</p>
<p><strong>Development environment</strong></p>
<p>For the time being I recommend you do your coding in Sharpdevelop. Download and install the latest  <a href="http://www.icsharpcode.net/OpenSource/SD/Download/">here</a>. You should also download the latest <a href="http://dist.codehaus.org/boo/distributions/?C=M;O=D">Boo-distro</a> (the binaries), and move (and overwrite) the Boo-files into The sharpdevelop install-folder: SharpDevelop\3.0\AddIns\AddIns\BackendBindings\BooBinding. Now your development environment is in place.</p>
<p>For Visual Studio integration, <a href="http://www.codeplex.com/BooLangStudio/Release/ProjectReleases.aspx?ReleaseId=16551">Boo Lang Studio</a> is the thing. But sadly I don&#8217;t find it stable enough yet.</p>
<p><strong>The source</strong></p>
<p>Yes, you should download the <a href="http://dist.codehaus.org/boo/distributions/?C=M;O=D">Boo source code</a>. It&#8217;s helpful to use it to browse class hierarchies, to see how things actually work, and most importantly, the source contains a lot of example code. I actually learned macros by looking at the source code for the built in macros (print, assert etc).</p>
<p><strong>The compiler extensibility</strong></p>
<p>To fully understand how Boo works with the compiler, it&#8217;s very helpful to be able to inspect the result. That is, the assemblies that are produced. There is an excellent tool for doing that, <a href="http://reflector.red-gate.com/download.aspx">Reflector</a>. Download it, and start looking at the compiled assemblies you produce.</p>
<p><strong>Starting points</strong></p>
<p>You should start at <a href="http://boo.codehaus.org">the Boo homepage</a>. Also take a look at <a href="http://www.ayende.com/Blog/category/498.aspx">Ayende&#8217;s blog</a>. He blogs quite a bit about Boo.</p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/09/09/boo-getting-started/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Boo!</title>
		<link>http://tore.vestues.no/2008/09/04/boo/</link>
		<comments>http://tore.vestues.no/2008/09/04/boo/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 19:54:44 +0000</pubDate>
		<dc:creator>Tore Vestues</dc:creator>
				<category><![CDATA[Boo]]></category>

		<guid isPermaLink="false">http://tore.vestues.no/2008/09/04/boo/</guid>
		<description><![CDATA[Is there really a language on the .net-plattform that can increase our productivity beyond what we&#8217;re experiencing today with C#?
I know that Iron Python and Iron Ruby are sailing up as really exciting alternatives to what we are used to with their dynamic nature and neat syntax (among other things!). But there is yet another [...]]]></description>
			<content:encoded><![CDATA[<p>Is there really a language on the .net-plattform that can increase our productivity beyond what we&#8217;re experiencing today with C#?</p>
<p>I know that Iron Python and Iron Ruby are sailing up as really exciting alternatives to what we are used to with their dynamic nature and neat syntax (among other things!). But there is yet another language with some unique and mind blowing features that I actually think can rise above all these languages. It&#8217;s simply called <b>Boo</b>. And with Boo, I really think we can make a difference when it comes to productivity.</p>
<p>Boo isn&#8217;t actually a new language, it&#8217;s been around for some five years now, but it has not become a mainstream language, yet. Well, now it&#8217;s positioning itself as &#8220;the&#8221; language for building DSL&#8217;s (domain specific languages). And it almost certainly is. Given also that DSL by many is thought of as &#8220;the future&#8221;, this by itself actually makes the future of Boo quite bright. That&#8217;s great! But as DSL is sailing up as the buzz of the year, there is something many actually overlook in Boo: It&#8217;s an extremely powerful language by itself, dsl or not!</p>
<p>What makes it so powerful is its compiler extensibility. Combined with the no-nonsense python style and syntax, this language rocks! The essence of the compiler extensibility is that Boo makes it very easy to write code that is executed in the actual compiling process. This is actually a form of code generation, except you do not generate the code, you change the generated output from the compiler. So how does this make us more productive?</p>
<p>To give you a little taste:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="boo" style="font-family:monospace;"><span style="color: #006400;">&#91;</span>Singleton<span style="color: #006400;">&#93;</span>
<span style="color:blue;font-weight:bold;">class</span> TheSingleton:
   <span style="color:gray;">pass</span></pre></td></tr></table></div>

<p>This is how you create a Singleton-class in Boo. It can&#8217;t be simpler, and it&#8217;s way less code than in any other Singleton implementation I&#8217;ve ever seen. That&#8217;s because Boo gives us a chance to easily extend our language, which few other languages can. The Singleton attribute you see is not a normal attribute, it&#8217;s an AST attribute, which means when the compiler find it, it executes some code, which in turn adds all the functionality to the class when it is written into the assembly. Did you read my post about <a href="http://tore.vestues.no/2008/07/31/the-declarative-mindset/">the declarative mindset</a>? Boo is spot on declarative: we state what we want to do (&#8221;make a singleton&#8221;), and we simply do not have to write the actual implementation. And why should we? Every singleton is implemented just the same way. It&#8217;s no need to do it more than once. Boo makes this not only possible to handle elegantly, but also easy. I love it!</p>
<p>To be honest, I haven&#8217;t been this excited about technology in a long time, and I&#8217;ve decided to focus on Boo in the time to come, simply because I believe this might be the star of tomorrow. </p>
<p>This means I will be posting code and tutorials about it, doing research, joining the community, and hopefully making a tiny bit of difference, doing my share of pushing the world in the right direction when it comes to developing better software.</p>
<p>So, don&#8217;t hesitate, join the fun. It&#8217;s right here: <a href="http://boo.codehaus.org">Boo!</a></p>
<p>- Tore Vestues</p>
]]></content:encoded>
			<wfw:commentRss>http://tore.vestues.no/2008/09/04/boo/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
