<?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>My Diversions &#187; GWT</title>
	<atom:link href="http://www.kablambda.org/blog/tag/gwt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kablambda.org/blog</link>
	<description>Notes on things I'm thinking and doing</description>
	<lastBuildDate>Tue, 20 Oct 2009 11:31:55 +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>GWT as a CAL client</title>
		<link>http://www.kablambda.org/blog/2008/03/15/gwt-as-a-cal-client/</link>
		<comments>http://www.kablambda.org/blog/2008/03/15/gwt-as-a-cal-client/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 09:12:04 +0000</pubDate>
		<dc:creator>Tom Davies</dc:creator>
				<category><![CDATA[CAL and Open Quark]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://diversions.nfshost.com/blog/2008/03/15/gwt-as-a-cal-client/</guid>
		<description><![CDATA[I&#8217;ve been interested in GWT as a way of building rich Internet applications since it appeared, and I&#8217;m very pleased to see it getting better and better.

So it&#8217;s natural that I&#8217;d want to try using it with CAL, a functional language quite similar to Haskell which runs on the JVM.

I used a similar approach to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been interested in <a href="http://code.google.com/webtoolkit/">GWT</a> as a way of building rich Internet applications since it appeared, and I&#8217;m very pleased to see it getting <a href="http://timepedia.blogspot.com/2008/03/gwtthe-road-to-15-part-1_07.html">better</a> and <a href="http://timepedia.blogspot.com/2008/03/gwt-road-to-15-linkers.html">better</a>.</p>

<p>So it&#8217;s natural that I&#8217;d want to try using it with <a href="http://openquark.org">CAL</a>, a functional language quite similar to <a href="http://haskell.org">Haskell</a> which runs on the JVM.</p>

<p>I used a similar approach to marshalling Javabeans to CAL algebraic types as I <a href="http://diversions.nfshost.com/blog/2007/10/03/cal-and-tapestry-5-part-2-algebraic-types-and-forms/">used before</a>, but this time I haven&#8217;t used any bytecode manipulation &#8212; as the Java classes are needed at compile time for the GWT client there isn&#8217;t any point in generating them at runtime (although generating them as a separate build step might be useful). I&#8217;ve also extended the previous work to include mapping a Java 5 enum to a CAL algebraic type which has constructors with zero parameters.</p>

<p>So in our GWT client we can write:</p>

<pre><code>CaltestServiceAsync service = GWT.create(CaltestService.class);
((ServiceDefTarget) service).setServiceEntryPoint(
    GWT.getModuleBaseURL() + "CaltestService");
service.processPerson(
    new Person(Salutation.MR, "Jim", "Earl", "Jones"), new MyAsyncCallback(...));
</code></pre>

<p>and call the CAL function:</p>

<pre><code>public processPerson p =
    let Person s f m l = p; 
    in Person s (toUpperCase f) (lift toUpperCase m) (toUpperCase l);
</code></pre>

<p>where the types are:</p>

<pre><code>data Salutation = MR | MRS deriving Inputable, Outputable;
data Person =
    Person salutation :: Salutation 
              firstName :: String 
              middleName :: (Maybe String) 
              lastName :: String 
    deriving Inputable, Outputable;
</code></pre>

<p>All this is done via three different annotations, and a special subclass of the GWT <code>RemoteServiceServlet</code>.</p>

<p>The first annotation, <code>@Cal</code> is applied to the GWT service interface, and indicates the CAL module to map the functions on the interface to:</p>

<pre><code>@Cal(workspace = "myworkspace.cws", module = "TDavies.GwtTest")
public interface CaltestService extends RemoteService {
    @Cal
    Person processPerson(Person p);
...
</code></pre>

<p>The CAL types <code>Person</code> and <code>Salutation</code> need to be mapped to Java classes:</p>

<p>Person is a simple Javabean with getters and setters for each attribute:</p>

<pre><code>@CalBean(workspace = "myworkspace.cws", module = "TDavies.GwtTest",
    constructorName = "Person")
public class Person implements IsSerializable {
    private Salutation salutation;
    private String firstName;
    private String lastName;
    private String middleName;
...
</code></pre>

<p>Note that <code>middleName</code> has the type <code>Maybe String</code> in the CAL type. A value of <code>null</code> maps to <code>Nothing</code> while a value of <code>"x"</code> maps to <code>Just "x"</code>.</p>

<p>Salutation is an <code>enum</code>:</p>

<pre><code>@CalEnum(workspace = "myworkspace.cws", module = "TDavies.GwtTest",
    type = "Salutation")
public enum Salutation {
    MR, MRS
}
</code></pre>

<p>The names of the enum&#8217;s values must be identical to the names of the CAL constructors.</p>

<p>A subclass of <code>RemoteServiceServlet</code> checks for the annotations and transforms the values in both directions.</p>

<p>The source code for this experiment is available via anonymous svn from <code>http://tgdavies.beanstalkapp.com/eddy/browse/trunk/cal</code>. Please note that this repository contains various other half-baked and half-finished experiments! Look at the <code>build.xml</code> file to see how to set up an environment &#8212; you&#8217;ll need to supply OpenQuark, GWT and <a href="http://www.mortbay.org/">Jetty</a>.</p>

<p>In my next post I&#8217;ll describe how to persist information on the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kablambda.org/blog/2008/03/15/gwt-as-a-cal-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
