<?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>I am Zef &#187; scala</title>
	<atom:link href="http://zef.me/tag/scala/feed" rel="self" type="application/rss+xml" />
	<link>http://zef.me</link>
	<description>Technology, Me, You.</description>
	<lastBuildDate>Mon, 30 Aug 2010 13:30:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel='hub' href='http://zef.me/?pushpress=hub'/>
		<item>
		<title>When Scala DSLs Fail</title>
		<link>http://zef.me/2371/when-scala-dsls-fail</link>
		<comments>http://zef.me/2371/when-scala-dsls-fail#comments</comments>
		<pubDate>Fri, 13 Nov 2009 12:23:31 +0000</pubDate>
		<dc:creator>Zef</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://zef.me/?p=2371</guid>
		<description><![CDATA[The hot new contender in the space of interal DSLs (dom [...]]]></description>
			<content:encoded><![CDATA[<p><img alt="logo" title="logo" width="200" height="35" class="alignnone size-full wp-image-2399" align="right" src="http://zef.me/wp-content/uploads/2009/11/logo.png" />The hot new contender in the space of interal DSLs (domain-specific languages as libraries) is <a href="http://www.scala-lang.org/">Scala</a>. Scala is, as is implied by its name, a language that is designed to <em>scale</em> from small one-off scripts to large enterprise applications. It is a statically typed language, and in many ways can be seen as a successor to Java. It also compiles to JVM bytecode. Similar to Ruby, Scala has a flexible syntax, which makes it an interesting choice to develop internal DSLs for (like Ruby on Rails).</p>

<p>The most popular web application framework for Scala is <a href="http://liftweb.net/">Lift</a>. On its website it is described as follows:</p>

<blockquote>
<p>Lift is an expressive and elegant framework for writing web applications. Lift stresses the importance of security, maintainability, scalability and performance, while allowing for high levels of developer productivity.</p>
</blockquote>

<p>In Lift, more configuration is done in Scala code rather than in <a href="http://zef.me/2333/when-jboss-seam-fails">XML files like in a framework like Seam</a>. Consequently, simple typing mistakes are typically caught by the compiler. One area where mistakes are still easily made are in its XML-based language for constructing user interfaces. I won&#8217;t milk this one too much, <a href="http://zef.me/2308/when-rails-fails">we know the trick</a> <a href="http://zef.me/2333/when-jboss-seam-fails">by now</a>, but let me demonstrate what happens when a programmer makes mistakes in templates.</p>

<p><strong>Mistake #1: mistyping a class name</strong></p>

<p>What happens if my satanistic fingers force me to type hellWorld, instead of helloWorld:</p>

<p style="margin-left: 40px; "><code>&lt;lift:surround with=&quot;default&quot; at=&quot;content&quot;&gt;<br />
&nbsp;&nbsp; &lt;h2&gt;Welcome to your project!&lt;/h2&gt;<br />
&nbsp;&nbsp; &lt;p&gt;&lt;lift:<span style="color: rgb(255, 0, 0); ">hellWorld</span>.howdy /&gt;&lt;/p&gt;<br />
&lt;/lift:surround&gt;</code></p>

<p>We get no error at compile time. At runtime, however something odd happens.</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/lift-fail-1.png"><img alt="lift-fail-1" title="lift-fail-1" width="281" height="93" class="alignnone size-full wp-image-2372" src="http://zef.me/wp-content/uploads/2009/11/lift-fail-1.png" /></a></p>

<p>It does display the &quot;Welcome to your project!&quot; message, and the list item with &quot;Home&quot; on it is part of our global template. But where did our howdy message go?</p>

<p>In the console we see the following;</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/lift-fail-1b.png"><img alt="lift-fail-1b" title="lift-fail-1b" width="480" height="92" class="alignnone size-full wp-image-2373" src="http://zef.me/wp-content/uploads/2009/11/lift-fail-1b.png" /></a></p>

<p>In a way this is an acceptable message, but shouldn&#8217;t this be an error?</p>

<p><strong>Mistake #2: mistyping an opening tag</strong></p>

<p>This is a nice one. Instead of typing &quot;surround&quot; in our template, we misspell it as &quot;surrond&quot;. A honest mistake. How does Lift deal with this?</p>

<p style="margin-left: 40px; "><code>&lt;lift:<span style="color: rgb(255, 0, 0); ">surrond</span> with=&quot;default&quot; at=&quot;content&quot;&gt;<br />
&nbsp;&nbsp;&lt;h2&gt;Welcome to your project!&lt;/h2&gt;<br />
&nbsp;&nbsp;&lt;p&gt;&lt;lift:hellWorld.howdy /&gt;&lt;/p&gt;<br />
&lt;/lift:surround&gt;</code></p>

<p>This is what we see when we load the page:</p>

<div><a href="http://zef.me/wp-content/uploads/2009/11/lift-fail-2.png"><img alt="lift-fail-2" title="lift-fail-2" width="600" height="158" class="alignnone size-full wp-image-2374" src="http://zef.me/wp-content/uploads/2009/11/lift-fail-2.png" /></a></div>

<p>line 4 does not exist? I&#8217;m pretty sure that it does&#8230; Looking at the stacktrace, I see it has to do something with XML parsing, but other than that, the message is kind of unexpected.</p>

<p>Alright, one more, then it&#8217;s been enough.</p>

<p><strong>Mistake #3: mistyping a template block name</strong></p>

<p>Let&#8217;s now misspell some more, we&#8217;re getting good at it.</p>

<p style="margin-left: 40px; "><code>&lt;lift:surround with=&quot;default&quot; at=&quot;<span style="color: rgb(255, 0, 0); ">conent</span>&quot;&gt;<br />
&nbsp;&nbsp; &lt;h2&gt;Welcome to your project!&lt;/h2&gt;<br />
&nbsp;&nbsp; &lt;p&gt;&lt;lift:hellWorld.howdy /&gt;&lt;/p&gt;<br />
&lt;/lift:surround&gt;</code></p>

<p>Again, no compilation errors. When we load the page we see the following:</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/lift-fail-3.png"><img alt="lift-fail-3" title="lift-fail-3" width="273" height="118" class="alignnone size-full wp-image-2375" src="http://zef.me/wp-content/uploads/2009/11/lift-fail-3.png" /></a></p>

<p>No error message on screen, or in our console. What happened here? We only see the global template and nothing from the content we just defined. This, of course, has to do with the fact that we&#8217;re defining a template that&#8217;s never actually called. It&#8217;s subtle, but can be tricky to find.</p>

<p><strong>Enough already, we get it!</strong></p>

<p>Ok, ok. I know. You get my point. Statically typed framework fail to detect errors that are encoded in strings and XML files. So what can we do to fix this (other than IDE support)? Well, the obvious thing is stop abusing strings and XML files in this way. If we find an acceptable way to encode these things in Scala we can check a lot more. Lift takes its first steps towards this by doing configuration in its <code>Boot</code> class.&nbsp;There are a few Scala DSLs that make <a href="http://szeiger.de/blog/category/scala/scala-query/">database queries statically verifiable</a>.&nbsp;Still, the user interface part breaks.&nbsp;</p>

<p>
<meta charset="utf-8" /></p>

<p>To fix this I attempted to build a simple version of WebDSL as an internal DSL in Scala. What follows is a piece of code that defines a user interface using this DSL:</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-1.png"><img alt="scala-internal-fail-1" title="scala-internal-fail-1" width="306" height="248" class="alignnone size-full wp-image-2389" src="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-1.png" /></a></p>

<p>This piece of code defines an <code>entries</code> template. The template renders a header saying &quot;All entries&quot;. Underneath we see a list of all Entry objects (<code>Entry.all</code>). For each entry there is a list item with a form. The entry&#8217;s name and text are displayed and there&#8217;s a button to delete the entry from the database. Simple and elegant (in my opinion).</p>

<p>The nice thing is that when I misspell one of the UI constructs, I get an error:</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-2.png"><img alt="scala-internal-fail-2" title="scala-internal-fail-2" width="294" height="83" class="alignnone size-full wp-image-2390" src="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-2.png" /></a></p>

<p>So the error is found at compile time, which is nice. However, are these errors very helpful?</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-2b.png"><img alt="scala-internal-fail-2b" title="scala-internal-fail-2b" width="274" height="130" class="alignnone size-full wp-image-2391" src="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-2b.png" /></a></p>

<p>value buton? Is a buton supposed to be a value? In our domain-speak we&#8217;re attempting to define a page element here, not a value. The good thing about error messages as you compile is that you see that something is wrong early, and typically can see clearly where the error is. The error messages, however, are not always very helpful, and hardly ever domain specific (&quot;No such page element: buton&quot; would have been a great error message).</p>

<p>But there&#8217;s another error in the code I just showed and the compiler didn&#8217;t catch it at all. The thing is that&nbsp;<code>listitem</code>s cannot appear just anywhere, they have to appear within <code>list</code>s! The <code>list</code> construct was left out. That&#8217;s easily fixed:</p>

<p><a href="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-1-fixed.png"><img alt="scala-internal-fail-1-fixed" title="scala-internal-fail-1-fixed" width="318" height="277" class="alignnone size-full wp-image-2393" src="http://zef.me/wp-content/uploads/2009/11/scala-internal-fail-1-fixed.png" /></a></p>

<p>What this example shows is that although we can catch many errors by creating an embedded DSL in Scala, there are definitely limits. Additionally, there is no way to give domain specific error messages at compile time (unless we extend the compiler), error messages typically expose the underlying implementation. Yes I admit it, the code to construct a <code>button</code> and define its logic is actually a method call.</p>

<p>So although internal DSLs in a statically typed language help you to find certain types of error faster, the error reporting is far from perfect. This is one of the strong points of <em>external DSLs</em> as we will see in a future post. <strong>Update:</strong> <a href="http://zef.me/2409/static-verification-an-external-dsl-advantage">This post</a>, to be exact.</p>
]]></content:encoded>
			<wfw:commentRss>http://zef.me/2371/when-scala-dsls-fail/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->