<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7045524330253482541</id><updated>2011-12-08T21:38:20.761-08:00</updated><category term='i18n'/><category term='math'/><category term='scala lift'/><category term='scala'/><category term='architecture'/><category term='java'/><category term='programming'/><category term='life'/><title type='text'>Jim McBeath</title><subtitle type='html'>Coding and Life</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>62</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-59788347034578309</id><published>2011-12-08T21:36:00.001-08:00</published><updated>2011-12-08T21:38:20.774-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='life'/><title type='text'>Levels of Expertise</title><content type='html'>An attempt to improve the objectivity of skill self-ratings.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#discussion"&gt;Discussion&lt;/a&gt;
&lt;li&gt;&lt;a href="#scale"&gt;Scale&lt;/a&gt;
&lt;li&gt;&lt;a href="#references"&gt;References&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="discussion"&gt;&lt;/a&gt;
&lt;h3&gt;Discussion&lt;/h3&gt;

We are often asked to rate things on a scale, typically 1 to 5 or 1 to 10.
Rarely is there an attempt to define what those different numbers mean.
From a statistician's point of view, this makes the values useful for the
sole purpose of comparing a single individual's ratings against other ratings
of that individual.
In particular, without a good definition of what the various levels mean,
I don't see how there can be any effective communication from one person to
another of the meaning of such a rating.

&lt;br/&gt;&lt;br/&gt;
When my doctor asks me to tell him how much something hurts on a scale
of 1 to 10, I have no idea what information he expects to get when I say
"3" or "7".

&lt;br/&gt;&lt;br/&gt;
I once asked an acquaintance to rate, on a scale of 1 (bad) to 10 (good),
a movie he had just seen.  He said it was a 9.  I was suspicious of this
answer, so I asked him how he would rate Star Wars, which I knew to be
his all-time favorite movie, on the same 1-to-10 scale.  He said 12.

&lt;br/&gt;&lt;br/&gt;
I personally consider it an aspect of innumeracy, but people often try to
emphasize something by using numbers that are outside of the valid range.
We may chuckle when Nigel says he likes his amp better because it
&lt;a href="http://www.spinaltapfan.com/atozed/TAP00160.HTM"&gt;
goes to 11&lt;/a&gt;, but how often have you heard someone talking in all
seriousness about putting in a "110% effort"?
What does that actually
&lt;a href="http://www.bepress.com/jqas/vol7/iss2/2/"&gt;mean&lt;/a&gt;?
How would you know if someone were
&lt;a href="http://www.fakingnews.com/2010/05/sc-strikes-down-demands-of-110-percent-from-employees-as-unconstitutional/"&gt;putting in&lt;/a&gt;
110% versus 100%?
If 110% is a valid number, then presumably
&lt;a href="http://blog.moneysavingexpert.com/2011/07/15/you-cannot-give-110-effort-%E2%80%93-an-explosion-of-pent-up-nerd-rage/"&gt;so is 120%&lt;/a&gt;,
so anyone
suggesting a &lt;a href="http://edge.ebaumsworld.com/mediaFiles/picture/460723/982951.png"&gt;mere 110%&lt;/a&gt;
is clearly not asking for enough effort.

&lt;br/&gt;&lt;br/&gt;
People tend to
&lt;a href="http://www.apa.org/monitor/feb03/overestimate.aspx"&gt;overestimate&lt;/a&gt;
&lt;a href="http://en.wikipedia.org/wiki/Illusory_superiority"&gt;how good&lt;/a&gt;
they are at all sorts of things,
including cognitive, social and physical skills.
If we all overrate ourselves by the same amount, I suppose that could all cancel
out and you could still compare people's ratings -
but without knowing &lt;i&gt;a priori&lt;/i&gt;
what their ratings should be, we don't know how much they might be
overrating themselves.

&lt;br/&gt;&lt;br/&gt;
When people consider their own expertise, it is common for those with less
expertise to overvalue themselves more than people with more expertise.
With more expertise comes more awareness of what one could do better.
Einstein
&lt;a href="http://www.notable-quotes.com/e/einstein_albert.html"&gt;said&lt;/a&gt;,
"As our circle of knowledge expands, so does the circumference of darkness surrounding it."
Relative beginners easily fall into the
&lt;a href="http://www2.merriam-webster.com/cgi-bin/mwdictsn?va=sophomoric"&gt;Sophomore Illusion&lt;/a&gt;
of thinking they
know a lot because the circumference of their knowledge is not yet large
enough for them to recognize the size of the surrounding darkness.

&lt;br/&gt;&lt;br/&gt;
In 1989, psychologist
&lt;a href="http://www.psy.cmu.edu/faculty/hayes/index.html"&gt;John Hayes&lt;/a&gt;
at &lt;a href="http://www.cmu.edu/"&gt;Carnegie Mellon University&lt;/a&gt;
identified what is now called the "ten-year rule"
(although there are
&lt;a href="http://blog.enkerli.com/2008/12/23/expertise-quest/"&gt;earlier commenters&lt;/a&gt;,
including &lt;a href="http://www.cs.cmu.edu/simon/"&gt;Herbert Simon&lt;/a&gt;,
who was also at CMU).
As
&lt;a href="http://www.its.caltech.edu/~len/"&gt;Leonard Mlodinow&lt;/a&gt;
says in
"&lt;a href="http://books.google.com/books?id=UJxRLCq9l3IC"&gt;The Drunkard's Walk&lt;/a&gt;",
"Experts often speak of the
'&lt;a href="http://www.selfgrowth.com/articles/10-Year_Rule_to_Become_an_Expert.html"&gt;ten&lt;/a&gt;-&lt;a href="http://rogercostello.wordpress.com/category/10-year-rule-for-exceptional-performance/"&gt;year&lt;/a&gt;
&lt;a href="http://creativity.netslova.ru/Ten-year_rule.html"&gt;rule&lt;/a&gt;,'
meaning that it takes at least a decade of hard work, patience and striving
to become highly successful in most endeavors." (links mine)
The ten-year rule is related to the idea that it takes about 10,000 hours
of practice at something to become an expert; with 5 hours of practice
per business day and 200 business days per year,
it would take ten years to rack up that many hours.
If you find yourself thinking how wonderfully expert you are in something
that you have practiced for only a few years, perhaps you should
consider the ten-year rule and temper your evaluation.

&lt;br/&gt;&lt;br/&gt;
Given that people are so bad at these ratings, it seems to me that the only
way to get any useful information from someone when asking this kind of
self-rating question is to have an objective definition
of what each level means.

&lt;br/&gt;&lt;br/&gt;
One way to think about a scale is by how many people fall into each level.
There are currently
&lt;a href="http://www.nytimes.com/2011/11/01/world/united-nations-reports-7-billion-humans-but-others-dont-count-on-it.html"&gt;7 billion people&lt;/a&gt;
in the world,
or almost 10 to the 10th power.
This conveniently maps to a logarithmic scale from 0 to 10,
allowing us to define eleven levels starting with level 0
containing all approximately 10 billion people in the world
and with each higher level having one tenth
the number of people as the level just below it.
If the descriptions of a level are hard to interpret,
perhaps the size of that level will help give an indication
of whether a person should be rated there.

&lt;br/&gt;&lt;br/&gt;
Years ago, during a job interview, I was asked to rate my level
of expertise in various subjects, such as programming languages
and development tools.
This was not an unusual question, I had been asked this question
before and have been asked it since.
What was different that time was that the interviewer included
a scale with some relatively objective descriptions for determining
level of expertise.
I rather liked the scale, so
although I don't recall the exact definition of his levels,
I have tried to reproduce that concept here,
using descriptions somewhat similar to those given by that interviewer.
Unfortunately, I don't remember who introduced that scale
to me, so I am unable to give credit.

&lt;br/&gt;&lt;br/&gt;
There are many reasons one might want a scale of expertise,
including rating potential employees or creating a summary
of the amount of expertise within a company.
The scale I present here is intended to be very general;
given its logarithmic nature that can include the
entire world population, it is capable of allowing comparison of
expertise across everyone in the world.
You might think that would make it suboptimal for
rating (potential) employee expertise,
but I think there are enough levels to make it useful for that purpose.

&lt;a name="scale"&gt;&lt;/a&gt;
&lt;h3&gt;Scale&lt;/h3&gt;

The scale below includes the following columns:
&lt;ul&gt;
&lt;li&gt;Level: a number for the level, from 0 to 10,
    with 10 being the highest level of expertise.
&lt;li&gt;Name: a name for the level.
    These are taken from a set of expertise level names proposed by the
    &lt;a href="https://we.riseup.net/tsolife+tsolife-goes-ruby/levels-of-expertise"&gt;
    Traveling School of Life&lt;/a&gt;.
    My use of them probably doesn't quite match their intent,
    but I liked the names and thought the ten words matched my
    levels pretty well, so I applied them to my levels
    and added "ignorant" for level 0.
&lt;li&gt;Description: a brief description of the level.
    The descriptions are worded as if for a technical tool;
    for application to other areas or concepts, modify accordingly.
    Comments referring to companies assume a large company (10,000+ people)
    with large divisions (1000+ people);
    being a company-wide guru in a company with 100 people
    might not get you past level 6.
&lt;li&gt;Size: the approximate number of people expected to be at that level
    worldwide.
    As mentioned above, this is a simple logarithmic scale.
    The number of people in a level is 10&lt;sup&gt;10-L&lt;/sup&gt; where
    L is the level number.
&lt;li&gt;Practice: the approximate amount of practice that could be required
    to reach that level of expertise.
    Putting in that many hours does not guarantee reaching that level,
    and reaching that level does not necessarily require
    putting in that many hours.
    The conversion factors are 1,000 hours per year or 5 hours per day.
&lt;/ul&gt;

All of these different factors are rough estimates,
not intended as absolutes but merely as guidelines to help
people rank themselves in a way that allows for more meaningful results.
I don't have any research to show how well my guesses about
Description, Size and Practice correlate;
if anyone knows of something along those lines,
that would be interesting.

&lt;br/&gt;&lt;br/&gt;
&lt;a name="scale-table"&gt;&lt;/a&gt;
&lt;table class="scale-table" border=1&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th width="55%"&gt;Description&lt;/th&gt;
&lt;th&gt;Size&lt;/th&gt;
&lt;th&gt;Practice&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;ignorant&lt;/td&gt;
    &lt;td&gt;I have never heard of it.&lt;/td&gt;
    &lt;td&gt;10,000,000,000&lt;/td&gt;
    &lt;td&gt;none&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;interested&lt;/td&gt;
    &lt;td&gt;I have heard a little about it, but don't know much.&lt;/td&gt;
    &lt;td&gt;1,000,000,000&lt;/td&gt;
    &lt;td&gt;1 hour&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;pursuing&lt;/td&gt;
    &lt;td&gt;I have read an article or two about it and understand the basics
        of what it is, but nothing in depth.&lt;/td&gt;
    &lt;td&gt;100,000,000&lt;/td&gt;
    &lt;td&gt;1 day (5 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;
    &lt;td&gt;beginner&lt;/td&gt;
    &lt;td&gt;I have read an in-depth article, primer, or how-to book,
        and/or have played with it a bit.&lt;/td&gt;
    &lt;td&gt;10,000,000&lt;/td&gt;
    &lt;td&gt;1 week (25 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;
    &lt;td&gt;apprentice&lt;/td&gt;
    &lt;td&gt;I have used it for at least a few months and have successfully
        completed a small project using it.&lt;/td&gt;
    &lt;td&gt;1,000,000&lt;/td&gt;
    &lt;td&gt;3 months (250 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;5&lt;/td&gt;
    &lt;td&gt;intermediate&lt;/td&gt;
    &lt;td&gt;I have used it for a year or more on a daily or regular basis,
        and am comfortable using it in moderately complex projects.&lt;/td&gt;
    &lt;td&gt;100,000&lt;/td&gt;
    &lt;td&gt;1 year (1,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;
    &lt;td&gt;advanced&lt;/td&gt;
    &lt;td&gt;I have been using it for many years, know all of the basic aspects,
        and am comfortable using it as a key element in complex projects.
        People in my group come to me with their questions.&lt;/td&gt;
    &lt;td&gt;10,000&lt;/td&gt;
    &lt;td&gt;5 years (5,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;
    &lt;td&gt;accomplished&lt;/td&gt;
    &lt;td&gt;I am a local expert, with ten or more years of solid experience.
        People in my division come to me with their questions.&lt;/td&gt;
    &lt;td&gt;1,000&lt;/td&gt;
    &lt;td&gt;10 years (10,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;8&lt;/td&gt;
    &lt;td&gt;master&lt;/td&gt;
    &lt;td&gt;I am a company-wide guru with twenty or more years of experience;
        people from other divisions come to me with their questions.&lt;/td&gt;
    &lt;td&gt;100&lt;/td&gt;
    &lt;td&gt;20 years (20,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;
    &lt;td&gt;grandmaster&lt;/td&gt;
    &lt;td&gt;I am a recognized international authority on it.&lt;/td&gt;
    &lt;td&gt;10&lt;/td&gt;
    &lt;td&gt;30 years (30,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;
    &lt;td&gt;great-grandmaster&lt;/td&gt;
    &lt;td&gt;I created it, and am the number 1 expert in the world.&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;50 years (50,000 hours)&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;a name="references"&gt;&lt;/a&gt;
&lt;h3&gt;References&lt;/h3&gt;

Other scales of expertise:
&lt;ul&gt;
&lt;li&gt;Ted Neward describes four levels in his
&lt;a href="http://blogs.tedneward.com/2008/08/14/The+NeverEnding+Debate+Of+Special
ist+V+Generalist.aspx"&gt;
post of August 16&lt;/a&gt;:
Apprentice, Journeyman, Master, Adept.
&lt;li&gt;&lt;a href="http://www.sld.demon.co.uk/dreyfus.pdf"&gt;The Dreyfus model of skill acquisition&lt;/a&gt;:
Novice, (Advanced) Beginner, Competent, Proficient, Expert.
&lt;li&gt;&lt;a href="http://www.performancemattersinc.com/posts/stages-of-expertise/"&gt;
Paul Schempp's take&lt;/a&gt; on Dreyfus's five levels,
with "Capable" rather than "Advanced Beginner".
&lt;li&gt;The &lt;a href="http://en.wikipedia.org/wiki/Four_stages_of_competence"&gt;
Four Stages of Competence&lt;/a&gt; of Thomas Gordon:
Unconscious Incompetence, Conscious Incompetence,
Conscious Competence, Unconscious Competence.
And how they might apply to
&lt;a href="http://devthought.com/2009/02/24/the-four-stages-of-programming-competence/"&gt;programming&lt;/a&gt;.
&lt;/ul&gt;

Other articles:
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://norvig.com/21-days.html"&gt;
    Teach Yourself Programming in Ten Years&lt;/a&gt;,
    by Peter Norvig, 2001.
&lt;li&gt;&lt;a href="http://www.ascue.org/files/proceedings/2009/p52.pdf"&gt;
    From Novice to Expert: Harnessing the Stages of
    Expertise Development in the Online World&lt;/a&gt;,
    by Douglas A Kranach, in the 2009 ASCUE Proceedings.
&lt;li&gt;&lt;a href="http://www.scribd.com/doc/52366153/16/Expertise"&gt;
    A College Student's Guide to Computers in Education&lt;/a&gt;,
    by Dave Moursund;
    Chapter 3,
    "Expertise and Problem Solving", page 25,
    with a discussion of expertise as related to hours of study and practice.
&lt;li&gt;An 2006 excerpt from
    &lt;a href="http://seedmagazine.com/content/article/how_to_get_to_carnegie_hall/"&gt;
    Jonah Lehrer&lt;/a&gt;
    on the importance of practice for Mozart and Tiger Woods.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-59788347034578309?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/59788347034578309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=59788347034578309' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/59788347034578309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/59788347034578309'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/12/levels-of-expertise.html' title='Levels of Expertise'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-7901178083384944858</id><published>2011-07-28T15:36:00.000-07:00</published><updated>2011-07-28T15:36:51.272-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Debugging Scala Parser Combinators</title><content type='html'>Two simple mechanisms for debugging parsers written using Scala's parser combinators.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Contents&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="#intro"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href="#example"&gt;Example Parser&lt;/a&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href="#calling-parsers"&gt;Calling Individual Parsers&lt;/a&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href="#tracing"&gt;Tracing&lt;/a&gt;&lt;br /&gt;
&lt;li&gt;&lt;a href="#updated-example"&gt;Updated Example&lt;/a&gt;&lt;br /&gt;
&lt;/ul&gt;
&lt;a name="intro"&gt;&lt;/a&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;In a recent comment on my 2008
&lt;a href="http://jim-mcbeath.blogspot.com/2008/09/scala-parser-combinators.html"&gt;
blog post&lt;/a&gt;
about Scala's parser combinators,
a reader asked how one might go about debugging such a parser.
As
&lt;a href="http://www.quanttec.com/fparsec/users-guide/debugging-a-parser.html"&gt;
one post&lt;/a&gt;
says,
"Debugging a parser implemented with the help of a combinator library
has its special challenges."
You may have trouble
&lt;a href="http://lorgonblog.wordpress.com/2007/12/12/monadic-parser-combinators-part-seven/"&gt;
setting breakpoints&lt;/a&gt;,
and stack traces can be
difficult to interpret.

&lt;br/&gt;&lt;br/&gt;
The two techniques I show here may not provide you with the kind of
visibility you might be used to when single-stepping through problem code,
but I hope they provide at least a little more visibility than you might
otherwise have.

&lt;a name="example"&gt;&lt;/a&gt;
&lt;h3&gt;Example Parser&lt;/h3&gt;
As an example parser I will use an integer-only version of the
four-function arithmetic parser I
built for my 2008 parser combinator post.
The code consists of a set of case classes to represent the parsed results
and a parser class that contains the parsing rules and a few helper methods.
You can copy this code into a file and either compile it or load it into
the Scala &lt;a href="http://www.scala-lang.org/node/2097"&gt;REPL&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.parsing.combinator.syntactical.StandardTokenParsers

sealed abstract class Expr {
    def eval():Int
}

case class EConst(value:Int) extends Expr {
    def eval():Int = value
}

case class EAdd(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval + right.eval
}

case class ESub(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval - right.eval
}

case class EMul(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval * right.eval
}

case class EDiv(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval / right.eval
}

case class EUMinus(e:Expr) extends Expr {
    def eval():Int = -e.eval
}

object ExprParser extends StandardTokenParsers {
    lexical.delimiters ++= List("+","-","*","/","(",")")

    def value = numericLit ^^ { s =&amp;gt; EConst(s.toInt) }

    def parens:Parser[Expr] = "(" ~&amp;gt; expr &amp;lt;~ ")"

    def unaryMinus:Parser[EUMinus] = "-" ~&amp;gt; term ^^ { EUMinus(_) }

    def term = ( value |  parens | unaryMinus )

    def binaryOp(level:Int):Parser[((Expr,Expr)=&amp;gt;Expr)] = {
        level match {
            case 1 =&amp;gt;
                "+" ^^^ { (a:Expr, b:Expr) =&amp;gt; EAdd(a,b) } |
                "-" ^^^ { (a:Expr, b:Expr) =&amp;gt; ESub(a,b) }
            case 2 =&amp;gt;
                "*" ^^^ { (a:Expr, b:Expr) =&amp;gt; EMul(a,b) } |
                "/" ^^^ { (a:Expr, b:Expr) =&amp;gt; EDiv(a,b) }
            case _ =&amp;gt; throw new RuntimeException("bad precedence level "+level)
        }
    }
    val minPrec = 1
    val maxPrec = 2

    def binary(level:Int):Parser[Expr] =
        if (level&amp;gt;maxPrec) term
        else binary(level+1) * binaryOp(level)

    def expr = ( binary(minPrec) | term )

    def parse(s:String) = {
        val tokens = new lexical.Scanner(s)
        phrase(expr)(tokens)
    }

    def apply(s:String):Expr = {
        parse(s) match {
            case Success(tree, _) =&amp;gt; tree
            case e: NoSuccess =&amp;gt;
                   throw new IllegalArgumentException("Bad syntax: "+s)
        }
    }

    def test(exprstr: String) = {
        parse(exprstr) match {
            case Success(tree, _) =&amp;gt;
                println("Tree: "+tree)
                val v = tree.eval()
                println("Eval: "+v)
            case e: NoSuccess =&amp;gt; Console.err.println(e)
        }
    }
    
    //A main method for testing
    def main(args: Array[String]) = test(args(0))
}
&lt;/pre&gt;
In the &lt;code&gt;ExprParser&lt;/code&gt; class, the lines up to and including the
definition of the &lt;code&gt;expr&lt;/code&gt; method define the parsing rules,
whereas the methods from &lt;code&gt;parse&lt;/code&gt; onwards are helper methods.

&lt;a name="calling-parsers"&gt;&lt;/a&gt;
&lt;h3&gt;Calling Individual Parsers&lt;/h3&gt;
In our example parser we can easily ask it to parse a string by calling
our &lt;code&gt;ExprParser.test&lt;/code&gt; method, which parses the string using our
&lt;code&gt;parse&lt;/code&gt; method, prints the resulting parse, and
(if the parse was successful) evaluates the parse tree and prints that value.

&lt;br/&gt;&lt;br/&gt;
The last line of &lt;code&gt;parse&lt;/code&gt;
parses a string using our expression parser:

&lt;pre name="hlcode" class="scala"
&gt;phrase(expr)(tokens)
&lt;/pre&gt;
&lt;code&gt;phrase&lt;/code&gt; is a method in
&lt;a href="http://www.scala-lang.org/api/current/scala/util/parsing/combinator/syntactical/StandardTokenParsers.html"&gt;
&lt;code&gt;StandardTokenParsers&lt;/code&gt;&lt;/a&gt;
that parses an input stream using the specified parser.
The only thing special about our &lt;code&gt;expr&lt;/code&gt; method is that we
happen to have selected it as our top-level parser -
but we could just as easily have picked one of our other parsers
as our top-level parser.

&lt;br/&gt;&lt;br/&gt;
Let's add another version of the &lt;code&gt;test&lt;/code&gt; method that lets us
specify which parser to use as the top-level parser.
We want to print out the results in the same way as for the existing
&lt;code&gt;test&lt;/code&gt; method, so we first
refactor that existing method:

&lt;pre name="hlcode" class="scala"
&gt;def test(exprstr: String) =
        printParseResult(parse(exprstr))

    def printParseResult(pr:ParseResult[Expr]) = {
        pr match {
            case Success(tree, _) =&amp;gt;
                println("Tree: "+tree)
                val v = tree.eval()
                println("Eval: "+v)
            case e: NoSuccess =&amp;gt; Console.err.println(e)
        }
    }
&lt;/pre&gt;
Now we add a new &lt;code&gt;parse&lt;/code&gt; method that accepts a parser as
an argument, and we call that from our new &lt;code&gt;test&lt;/code&gt; method:

&lt;pre name="hlcode" class="scala"
&gt;def parse(p:Parser[Expr], s:String) = {
        val tokens = new lexical.Scanner(s)
        phrase(p)(tokens)
    }

    def test(p:Parser[Expr], exprstr: String) =
        printParseResult(parse(p,exprstr))
&lt;/pre&gt;
We can run the Scala REPL, load our modified file using the ":load" command,
then manually call the top-level parser by calling our &lt;code&gt;test&lt;/code&gt;
method.
To reduce typing, we import everything from &lt;code&gt;ExprParser&lt;/code&gt;.
In the examples below, text in &lt;b&gt;bold&lt;/b&gt; is what we type,
the rest is printed by the REPL.

&lt;pre name="hlcode" class="scala"
&gt;scala&amp;gt; &lt;b&gt;import ExprParser._&lt;/b&gt;
import ExprParser._

scala&amp;gt; &lt;b&gt;test("1+2")&lt;/b&gt;
Tree: EAdd(EConst(1),EConst(2))
Eval: 3

scala&amp;gt; &lt;b&gt;test("1+2*3")&lt;/b&gt;
Tree: EAdd(EConst(1),EMul(EConst(2),EConst(3)))
Eval: 7

scala&amp;gt; &lt;b&gt;test("(1+2)*3")&lt;/b&gt;
Tree: EMul(EAdd(EConst(1),EConst(2)),EConst(3))
Eval: 9
&lt;/pre&gt;
We can also call the &lt;code&gt;test&lt;/code&gt; method that takes a parser as an
argument, allowing us to specifically test one particular parsing rule
at a time.
If we pass in &lt;code&gt;expr&lt;/code&gt; as the parser, we will get the same
results as above;
but if we pass in a different parser, we may get different results.

&lt;pre name="hlcode" class="scala"
&gt;scala&amp;gt; &lt;b&gt;test(expr,"1+2*3")&lt;/b&gt;
Tree: EAdd(EConst(1),EMul(EConst(2),EConst(3)))
Eval: 7

scala&amp;gt; &lt;b&gt;test(binary(1),"1+2*3")&lt;/b&gt;
Tree: EAdd(EConst(1),EMul(EConst(2),EConst(3)))
Eval: 7

scala&amp;gt; &lt;b&gt;test(binary(2),"1+2*3")&lt;/b&gt;
[1.2] failure: ``/'' expected but `+' found

1+2*3
 ^

scala&amp;gt; &lt;b&gt;test(parens,"1+2")&lt;/b&gt;
[1.1] failure: ``('' expected but 1 found

1+2
^

scala&amp;gt; &lt;b&gt;test(parens,"(1+2)")&lt;/b&gt;
Tree: EAdd(EConst(1),EConst(2))
Eval: 3

scala&amp;gt; &lt;b&gt;test(parens,"(1+2)*3")&lt;/b&gt;
[1.6] failure: end of input expected

(1+2)*3
     ^
&lt;/pre&gt;
&lt;a name="tracing"&gt;&lt;/a&gt;
&lt;h3&gt;Tracing&lt;/h3&gt;
If you have a larger parser that is not behaving and you are not quite
sure where the problem lies, it can be tedious to directly call
individual parsers until you find which one is misbehaving.
Being able to trace the progress of the whole parser running on an
input known to cause the problem might be helpful, but sprinkling
&lt;code&gt;println&lt;/code&gt; statements throughout your parser can be tricky.
This section provides an approach that allows you to do some tracing
with minimal changes to your code.
The output can get pretty verbose, but
at least this will give you a starting point from which you may be
able to devise your own improved debugging.

&lt;br/&gt;&lt;br/&gt;
The idea behind this approach is to wrap some or all of the individual
parsers in a debugging parser that delegates its &lt;code&gt;apply&lt;/code&gt; action
to the wrapper parser, but that prints out some debugging information.
The &lt;code&gt;apply&lt;/code&gt; action is called during the act of parsing.

&lt;br/&gt;&lt;br/&gt;
&lt;b&gt;Note:&lt;/b&gt; this code relies on the fact that the code
for the various combinators in
the &lt;code&gt;Parser&lt;/code&gt; class in Scala's
&lt;code&gt;StandardTokenParsers&lt;/code&gt;
(which is implemented as an inner class in
&lt;code&gt;scala.util.parsing.combinator.Parsers&lt;/code&gt;)
does not override any &lt;code&gt;Parser&lt;/code&gt;
method other than &lt;code&gt;apply&lt;/code&gt;.

&lt;br/&gt;&lt;br&gt;
This code could be added directly to the &lt;code&gt;ExprParser&lt;/code&gt; class,
but it is presented here as a separate class to make it easier to reuse.
Add this &lt;code&gt;DebugStandardTokenParsers&lt;/code&gt; class
to the file containing &lt;code&gt;ExprParsers&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;trait DebugStandardTokenParsers extends StandardTokenParsers {
    class Wrap[+T](name:String,parser:Parser[T]) extends Parser[T] {
        def apply(in: Input): ParseResult[T] = {
            val first = in.first
            val pos = in.pos
            val offset = in.offset
            val t = parser.apply(in)
            println(name+".apply for token "+first+
                    " at position "+pos+" offset "+offset+" returns "+t)
            t
        }
    }
}
&lt;/pre&gt;
The &lt;code&gt;Wrap&lt;/code&gt; class provides the hook into the &lt;code&gt;apply&lt;/code&gt;
method that we need in order to print out our trace information as the
parser runs.
Once this class is in place, we modify &lt;code&gt;ExprParser&lt;/code&gt; to
inherit from it rather than from &lt;code&gt;StandardTokenParsers&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;object ExprParser extends DebugStandardTokenParsers { ... }
&lt;/pre&gt;
So far we have not changed the behavior of the parser, since we have not
yet wired in the &lt;code&gt;Wrap&lt;/code&gt; class.
To do so, we can take any of the existing parsers and wrap it in a
&lt;code&gt;new Wrap&lt;/code&gt;.
For example, with the top-level &lt;code&gt;expr&lt;/code&gt; parser
we could do this,
with the added code highlighted in &lt;b&gt;bold&lt;/b&gt;:

&lt;pre name="hlcode" class="scala"
&gt;def expr = &lt;b&gt;new Wrap("expr",&lt;/b&gt; ( binary(minPrec) | term ) &lt;b&gt;)&lt;/b&gt;
&lt;/pre&gt;
We can make this a bit easier to edit and read by using implicits.
In &lt;code&gt;DebugStandardTokenParsers&lt;/code&gt; we add this method:

&lt;pre name="hlcode" class="scala"
&gt;implicit def toWrapped(name:String) = new {
        def !!![T](p:Parser[T]) = new Wrap(name,p)
    }
&lt;/pre&gt;
Now we can wrap our &lt;code&gt;expr&lt;/code&gt; method like this:

&lt;pre name="hlcode" class="scala"
&gt;def expr = &lt;b&gt;"expr" !!!&lt;/b&gt; ( binary(minPrec) | term )
&lt;/pre&gt;
If you don't like using &lt;code&gt;!!!&lt;/code&gt; as an operator, you are free
to pick something more to your taste, or you can leave out the implicit
and just use the &lt;code&gt;new Wrap&lt;/code&gt; approach.

&lt;br/&gt;&lt;br/&gt;
At this point you must modify your source code by adding the above syntax
to each parsing rule that you want to trace.
You can go through and do them all, or you can just pick out the ones
you think are the most likely culprits and wrap those.
Note that you can wrap any parser this way, including those that appear
as pieces in the middle of other parsers.
The following example shows how some of the parsers in the &lt;code&gt;term&lt;/code&gt;
and &lt;code&gt;binaryOp&lt;/code&gt; methods can be wrapped:

&lt;pre name="hlcode" class="scala"
&gt;    def term = &lt;b&gt;"term" !!!&lt;/b&gt; ( value |  &lt;b&gt;"term-parens" !!!&lt;/b&gt; parens | unaryMinus )

    def binaryOp(level:Int):Parser[((Expr,Expr)=&amp;gt;Expr)] = {
        level match {
            case 1 =&amp;gt;
                &lt;b&gt;"add" !!!&lt;/b&gt; "+" ^^^ { (a:Expr, b:Expr) =&amp;gt; EAdd(a,b) } |
                &lt;b&gt;"sub" !!!&lt;/b&gt; "-" ^^^ { (a:Expr, b:Expr) =&amp;gt; ESub(a,b) }
            case 2 =&amp;gt;
                &lt;b&gt;"mul" !!!&lt;/b&gt; "*" ^^^ { (a:Expr, b:Expr) =&amp;gt; EMul(a,b) } |
                &lt;b&gt;"div" !!!&lt;/b&gt; "/" ^^^ { (a:Expr, b:Expr) =&amp;gt; EDiv(a,b) }
            case _ =&amp;gt; throw new RuntimeException("bad precedence level "+level)
        }
    }

&lt;/pre&gt;
Assuming we have wrapped the &lt;code&gt;expr&lt;/code&gt;, &lt;code&gt;term&lt;/code&gt; and
&lt;code&gt;binaryOp&lt;/code&gt; methods as in the above examples, here is what the
output looks like for a few tests.
As in the previous REPL example, user input is in &lt;b&gt;bold&lt;/b&gt;.
If you are using the REPL and reload the file, remember to
run &lt;code&gt;import ExprParser._&lt;/code&gt; again to pick up the
newer definitions.

&lt;pre name="hlcode" class="scala"
&gt;scala&amp;gt; &lt;b&gt;test("1")&lt;/b&gt;
term.apply for token 1 at position 1.1 offset 0 returns [1.2] parsed: EConst(1)
plus.apply for token EOF at position 1.2 offset 1 returns [1.2] failure: ``+'' expected but EOF found

1
 ^
minus.apply for token EOF at position 1.2 offset 1 returns [1.2] failure: ``-'' expected but EOF found

1
 ^
expr.apply for token 1 at position 1.1 offset 0 returns [1.2] parsed: EConst(1)
Tree: EConst(1)
Eval: 1

scala&amp;gt; &lt;b&gt;test("(1+2)*3")&lt;/b&gt;
term.apply for token 1 at position 1.2 offset 1 returns [1.3] parsed: EConst(1)
plus.apply for token `+' at position 1.3 offset 2 returns [1.4] parsed: +
term.apply for token 2 at position 1.4 offset 3 returns [1.5] parsed: EConst(2)
plus.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``+'' expected but `)' found

(1+2)*3
    ^
minus.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``-'' expected but `)' found

(1+2)*3
    ^
expr.apply for token 1 at position 1.2 offset 1 returns [1.5] parsed: EAdd(EConst(1),EConst(2))
term-parens.apply for token `(' at position 1.1 offset 0 returns [1.6] parsed: EAdd(EConst(1),EConst(2))
term.apply for token `(' at position 1.1 offset 0 returns [1.6] parsed: EAdd(EConst(1),EConst(2))
term.apply for token 3 at position 1.7 offset 6 returns [1.8] parsed: EConst(3)
plus.apply for token EOF at position 1.8 offset 7 returns [1.8] failure: ``+'' expected but EOF found

(1+2)*3
       ^
minus.apply for token EOF at position 1.8 offset 7 returns [1.8] failure: ``-'' expected but EOF found

(1+2)*3
       ^
expr.apply for token `(' at position 1.1 offset 0 returns [1.8] parsed: EMul(EAdd(EConst(1),EConst(2)),EConst(3))
Tree: EMul(EAdd(EConst(1),EConst(2)),EConst(3))
Eval: 9

scala&amp;gt; &lt;b&gt;test(parens,"(1+2)")&lt;/b&gt;
term.apply for token 1 at position 1.2 offset 1 returns [1.3] parsed: EConst(1)
mul.apply for token `+' at position 1.3 offset 2 returns [1.3] failure: ``*'' expected but `+' found

(1+2)
  ^
div.apply for token `+' at position 1.3 offset 2 returns [1.3] failure: ``/'' expected but `+' found

(1+2)
  ^
add.apply for token `+' at position 1.3 offset 2 returns [1.4] parsed: +
term.apply for token 2 at position 1.4 offset 3 returns [1.5] parsed: EConst(2)
mul.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``*'' expected but `)' found

(1+2)
    ^
div.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``/'' expected but `)' found

(1+2)
    ^
add.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``+'' expected but `)' found

(1+2)
    ^
sub.apply for token `)' at position 1.5 offset 4 returns [1.5] failure: ``-'' expected but `)' found

(1+2)
    ^
expr.apply for token 1 at position 1.2 offset 1 returns [1.5] parsed: EAdd(EConst(1),EConst(2))
Tree: EAdd(EConst(1),EConst(2))
Eval: 3
&lt;/pre&gt;
As you can see, even for these very short input strings
the output is pretty verbose.
It does, however, show you what token it is trying to parse
and where in the input stream that token is, so by paying attention
to the position and offset numbers you can see where it is backtracking.

&lt;br/&gt;&lt;br/&gt;
When you have found the problem and are done debugging, you can remove
the &lt;code&gt;DebugStandardTokenParsers&lt;/code&gt; class and take out all of the
&lt;code&gt;!!!&lt;/code&gt; wrapping operations, or you can leave everything in place
and disable the wrapper output by changing the
definition of the implicit &lt;code&gt;!!!&lt;/code&gt; operator to this:

&lt;pre name="hlcode" class="scala"
&gt;def !!![T](p:Parser[T]) = p
&lt;/pre&gt;
Or, if you want to make it possible to enable debugging output later,
change &lt;code&gt;!!!&lt;/code&gt; to return either &lt;code&gt;p&lt;/code&gt; or
&lt;code&gt;new Wrap(p)&lt;/code&gt; depending on some debugging configuration value.

&lt;a name="updated-example"&gt;&lt;/a&gt;
&lt;h3&gt;Updated Example&lt;/h3&gt;
Below is the complete program with all of the above changes.

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.parsing.combinator.syntactical.StandardTokenParsers

sealed abstract class Expr {
    def eval():Int
}

case class EConst(value:Int) extends Expr {
    def eval():Int = value
}

case class EAdd(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval + right.eval
}

case class ESub(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval - right.eval
}

case class EMul(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval * right.eval
}

case class EDiv(left:Expr, right:Expr) extends Expr {
    def eval():Int = left.eval / right.eval
}

case class EUMinus(e:Expr) extends Expr {
    def eval():Int = -e.eval
}

trait DebugStandardTokenParsers extends StandardTokenParsers {
    class Wrap[+T](name:String,parser:Parser[T]) extends Parser[T] {
        def apply(in: Input): ParseResult[T] = {
            val first = in.first
            val pos = in.pos
            val offset = in.offset
            val t = parser.apply(in)
            println(name+".apply for token "+first+
                    " at position "+pos+" offset "+offset+" returns "+t)
            t
        }
    }

    implicit def toWrapped(name:String) = new {
        def !!![T](p:Parser[T]) = new Wrap(name,p) //for debugging
        //def !!![T](p:Parser[T]) = p              //for production
    }
}

object ExprParser extends DebugStandardTokenParsers {
    lexical.delimiters ++= List("+","-","*","/","(",")")

    def value = numericLit ^^ { s =&amp;gt; EConst(s.toInt) }

    def parens:Parser[Expr] = "(" ~&amp;gt; expr &amp;lt;~ ")"

    def unaryMinus:Parser[EUMinus] = "-" ~&amp;gt; term ^^ { EUMinus(_) }

    def term = "term" !!! ( value |  "term-parens" !!! parens | unaryMinus )

    def binaryOp(level:Int):Parser[((Expr,Expr)=&amp;gt;Expr)] = {
        level match {
            case 1 =&amp;gt;
                "add" !!! "+" ^^^ { (a:Expr, b:Expr) =&amp;gt; EAdd(a,b) } |
                "sub" !!! "-" ^^^ { (a:Expr, b:Expr) =&amp;gt; ESub(a,b) }
            case 2 =&amp;gt;
                "mul" !!! "*" ^^^ { (a:Expr, b:Expr) =&amp;gt; EMul(a,b) } |
                "div" !!! "/" ^^^ { (a:Expr, b:Expr) =&amp;gt; EDiv(a,b) }
            case _ =&amp;gt; throw new RuntimeException("bad precedence level "+level)
        }
    }
    val minPrec = 1
    val maxPrec = 2

    def binary(level:Int):Parser[Expr] =
        if (level&amp;gt;maxPrec) term
        else binary(level+1) * binaryOp(level)

    def expr = "expr" !!! ( binary(minPrec) | term )

    def parse(s:String) = {
        val tokens = new lexical.Scanner(s)
        phrase(expr)(tokens)
    }

    def parse(p:Parser[Expr], s:String) = {
        val tokens = new lexical.Scanner(s)
        phrase(p)(tokens)
    }

    def apply(s:String):Expr = {
        parse(s) match {
            case Success(tree, _) =&amp;gt; tree
            case e: NoSuccess =&amp;gt;
                   throw new IllegalArgumentException("Bad syntax: "+s)
        }
    }

    def test(exprstr: String) =
        printParseResult(parse(exprstr))

    def test(p:Parser[Expr], exprstr: String) =
        printParseResult(parse(p,exprstr))

    def printParseResult(pr:ParseResult[Expr]) = {
        pr match {
            case Success(tree, _) =&amp;gt;
                println("Tree: "+tree)
                val v = tree.eval()
                println("Eval: "+v)
            case e: NoSuccess =&amp;gt; Console.err.println(e)
        }
    }
    
    //A main method for testing
    def main(args: Array[String]) = test(args(0))
}
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-7901178083384944858?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/7901178083384944858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=7901178083384944858' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/7901178083384944858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/7901178083384944858'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/07/debugging-scala-parser-combinators.html' title='Debugging Scala Parser Combinators'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-63042369280290473</id><published>2011-07-19T16:49:00.000-07:00</published><updated>2011-07-19T16:49:32.115-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Multithread Coroutine Scheduler</title><content type='html'>&lt;h1&gt;Multithread Coroutine Scheduler&lt;/h1&gt;

A scheduler that uses multiple worker threads
for continuations-based Scala coroutines.

&lt;br/&gt;&lt;br/&gt;
In my recent series of posts that
&lt;a href="http://jim-mcbeath.blogspot.com/2011/04/java-nio-complete-scala-server.html"&gt;
ended&lt;/a&gt; with a complete Scala server
that uses continuations-based coroutines to store per-client state,
I asserted that the single-threaded scheduler implementation in that example
could relatively easily be replaced by a scheduler
that uses multiple threads.
In this post I provide a simple working example of such a
multithread scheduler.

&lt;h3&gt;Contents&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#overview"&gt;Overview&lt;/a&gt;
&lt;li&gt;&lt;a href="#tasks"&gt;Managing Tasks&lt;/a&gt;
&lt;li&gt;&lt;a href="#scheduler"&gt;Scheduler&lt;/a&gt;
&lt;li&gt;&lt;a href="#synchronization"&gt;Synchronization&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="overview"&gt;&lt;/a&gt;
&lt;h3&gt;Overview&lt;/h3&gt;

We can use the standard
&lt;a href="http://en.wikipedia.org/wiki/Thread_pool_pattern"&gt;thread-pool&lt;/a&gt;
approach in which we have a pool
of worker threads that independently pull from a common task queue.
Java 1.5 introduced a set of classes and interfaces in the
&lt;a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/package-summary.html"&gt;
&lt;code&gt;java.util.concurrent&lt;/code&gt; package&lt;/a&gt;
to support various kinds of thread pools
or potentially other task scheduling mechanisms.
Rather than writing our own, we will use an
&lt;a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/Executor.html"&gt;
&lt;code&gt;Executor&lt;/code&gt;&lt;/a&gt;
from that package.

&lt;br/&gt;&lt;br/&gt;
We have an additional requirement that makes our situation a little bit
more complex than the typical thread-pool: our collection of tasks includes
both tasks that are ready to run and tasks that are currently blocked
but will become ready to run at some point in the future.

&lt;br/&gt;&lt;br/&gt;
We will implement a new scheduler class &lt;code&gt;JavaExecutorCoScheduler&lt;/code&gt;
that maintains a list of blocked tasks and
uses a Java &lt;code&gt;Executor&lt;/code&gt; to manage runnable tasks.

&lt;br/&gt;&lt;br/&gt;
The updated complete source code for this post is available
on github in my &lt;a href="https://github.com/jimmc/nioserver"&gt;nioserver&lt;/a&gt;
project under the tag
&lt;a href="https://github.com/jimmc/nioserver/tree/blog-executor"&gt;
blog-executor&lt;/a&gt;.

&lt;a name="tasks"&gt;&lt;/a&gt;
&lt;h3&gt;Managing Tasks&lt;/h3&gt;

As mentioned above, we need to deal with two kinds of tasks:
tasks that are ready to run and tasks that are blocked.
The standard
&lt;code&gt;Executor&lt;/code&gt;
class allows us to submit a task for execution, but does not handle
blocked tasks.
Since we don't want to submit blocked tasks to the &lt;code&gt;Executor&lt;/code&gt;,
we have to queue them up ourselves.
We have two issues to attend to:
&lt;ol&gt;
&lt;li&gt;When our scheduler is passed a task, we must put it into our own
    queue of blocked tasks if it is not currently ready to run.
&lt;li&gt;When a previously blocked task becomes ready to run,
    we must remove it from our queue of
    blocked tasks and pass it to the &lt;code&gt;Executor&lt;/code&gt;.
&lt;/ol&gt;

The first issue is straightforward, as our framework already allows us to
test the blocker for a task and see if the task is ready to run.
In order to properly take care of the second issue, we will make a small
change to our framework to allow us to notice when a blocker has probably
stopped blocking so that we can run the corresponding task.
We do this by modifying our &lt;code&gt;CoScheduler&lt;/code&gt; class to add
a method to notify it that a blocker has probably become unblocked:

&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;    def unblocked(b:Blocker):Unit
&lt;/pre&gt;

We call this method from &lt;code&gt;CoQueue&lt;/code&gt; in the two places where
we previously called &lt;code&gt;scheduler.coNotify&lt;/code&gt;:
in the &lt;code&gt;blockingEnqueue&lt;/code&gt; method after we have enqueued an item
to notify the scheduler that the dequeue side is probably unblocked,
and in the &lt;code&gt;blockingDequeue&lt;/code&gt; method after we have dequeued an item
to notify the scheduler that the enqueue side is probably unblocked.
Those two methods in &lt;code&gt;CoQueue&lt;/code&gt; now look like this:

&lt;pre name="hlcode" class="scala"
&gt;    def blockingEnqueue(x:A):Unit @suspendable = {
        enqueueBlocker.waitUntilNotBlocked
        enqueue(x)
        &lt;b&gt;scheduler.unblocked(dequeueBlocker)&lt;/b&gt;
    }

    def blockingDequeue():A @suspendable = {
        dequeueBlocker.waitUntilNotBlocked
        val x = dequeue
        &lt;b&gt;scheduler.unblocked(enqueueBlocker)&lt;/b&gt;
        x
    }
&lt;/pre&gt;

The implementation of &lt;code&gt;unblocked&lt;/code&gt; in our default scheduler
&lt;code&gt;DefaultCoScheduler&lt;/code&gt; is just a call to &lt;code&gt;coNotify&lt;/code&gt;,
so the behavior of that system will remain the same as it was before we added
the calls to &lt;code&gt;unblocked&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Because we need to ensure that all of our NIO read and write operations
are handled sequentially, we continue to manage those tasks separately
with our &lt;code&gt;NioSelector&lt;/code&gt; class,
where all of the reads are executed on one thread and all of the writes
are executed on another thread.

&lt;a name="scheduler"&gt;&lt;/a&gt;
&lt;h3&gt;Scheduler&lt;/h3&gt;

We already have a scheduler framework that defines a &lt;code&gt;CoScheduler&lt;/code&gt;
class as the parent class for our scheduler implementations,
which requires that we implement the methods
&lt;code&gt;setRoutineContinuation&lt;/code&gt;, &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt;
and the newly added &lt;code&gt;unblocked&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
In our &lt;code&gt;JavaExecutorCoSchduler&lt;/code&gt;,
our &lt;code&gt;setRoutineContinuation&lt;/code&gt; method is responsible for storing
or executing the task.
It checks to see if the task is currently blocked, storing it
in our list of blocked tasks if so.
Otherwise, it passes it to the thread pool (which is managed by an
&lt;a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ExecutorService.html"&gt;
&lt;code&gt;ExecutorService&lt;/code&gt;&lt;/a&gt;),
which takes care of managing the threads and running the task.
We define a simple case class, &lt;code&gt;RunnableCont&lt;/code&gt;, to turn our task
into a &lt;code&gt;Runnable&lt;/code&gt; that is usable by the pool.

&lt;br/&gt;&lt;br/&gt;
Our &lt;code&gt;unblocked&lt;/code&gt; method gets passed a blocker which is probably
now unblocked.
We test that, and if in fact it is still blocked we do nothing.
If it is unblocked, then we remove it from our list of blocked tasks
and pass it to the pool.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; method in this scheduler doesn't
actually do anything, since the pool is taking care of running everything.
We just return &lt;code&gt;SomeRoutinesBlocked&lt;/code&gt; so that the caller goes
into a wait state.

&lt;br/&gt;&lt;br/&gt;
In addition to the above three methods, we will have our thread pool,
a lock that we use when managing our blocked and runnable tasks,
and a set of blocked tasks waiting to become unblocked.
For this implementation we choose to use a thread pool of a fixed size,
thus the call to
&lt;a href="http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)"&gt;
&lt;code&gt;Executors.newFixedThreadPool&lt;/code&gt;&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
Here is our complete &lt;code&gt;JavaExecutorCoScheduler&lt;/code&gt; class:

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import java.lang.Runnable
import java.util.concurrent.Executors
import java.util.concurrent.ExecutorService

import scala.collection.mutable.LinkedHashMap
import scala.collection.mutable.SynchronizedMap

class JavaExecutorCoScheduler(numWorkers:Int) extends CoScheduler {
    type Task = Option[Unit=&amp;gt;Unit]
    case class RunnableCont(task:Task) extends Runnable {
        def run() = task foreach { _() }
    }

    private val pool = Executors.newFixedThreadPool(numWorkers)
    private val lock = new java.lang.Object
    private val blockedTasks = new LinkedHashMap[Blocker,Task] with
            SynchronizedMap[Blocker,Task]

    private[scoroutine] def setRoutineContinuation(b:Blocker,task:Task) {
        lock.synchronized {
            if (b.isBlocked) {
                blockedTasks(b) = task
            } else {
                pool.execute(RunnableCont(task))
                coNotify
            }
        }
    }

    def unblocked(b:Blocker):Unit = {
        lock.synchronized {
            if (!b.isBlocked)
                blockedTasks.remove(b) foreach { task =&amp;gt;
                    pool.execute(RunnableCont(task)) }
        }
        coNotify
    }

    def runNextUnblockedRoutine():RunStatus = SomeRoutinesBlocked
}
&lt;/pre&gt;

&lt;a name="synchronization"&gt;&lt;/a&gt;
&lt;h3&gt;Synchronization&lt;/h3&gt;

Although not necessitated by the above changes,
I added one more change to &lt;code&gt;CoScheduler&lt;/code&gt;
to improve its synchronization behavior.

&lt;br/&gt;&lt;br/&gt;
While exploring various multi-threading mechanisms as alternatives to
using &lt;code&gt;Executor&lt;/code&gt;,
I wrote a scheduler called &lt;code&gt;MultiThreadCoScheduler&lt;/code&gt;
in which I implemented my own thread pool
and in which the master thread directly
allocated tasks to the worker threads in the pool.
Although that scheduler was quite a bit larger than the one presented
above, it provided much more control over the threads, allowing me to
change the number of worker threads on the fly
and to be able to tell in my master
thread whether there were any running worker threads.

&lt;br/&gt;&lt;br/&gt;
In &lt;code&gt;MultiThreadCoScheduler&lt;/code&gt;,
the main thread would call &lt;code&gt;coWait&lt;/code&gt;
to wait until it needed to wake up and hand out another task,
and the worker threads would call &lt;code&gt;coNotify&lt;/code&gt; when they were
done processing a task and were ready to be assigned the next task.
Similarly, a call to &lt;code&gt;coNotify&lt;/code&gt; would be issued whenever
a new task was placed into the task queue.

&lt;br/&gt;&lt;br/&gt;
Unfortunately, Java's &lt;code&gt;wait&lt;/code&gt; and
&lt;code&gt;notify&lt;/code&gt; methods,
which are the calls underlying our &lt;code&gt;coWait&lt;/code&gt;
and &lt;code&gt;coNotify&lt;/code&gt; methods,
do not quite behave the way we would like.
If we compare those calls to the Java NIO
&lt;code&gt;select&lt;/code&gt; and &lt;code&gt;wakeup&lt;/code&gt; calls,
we note that if a call is made to &lt;code&gt;wakeup&lt;/code&gt; &lt;i&gt;before&lt;/i&gt;
a call to &lt;code&gt;select&lt;/code&gt;,
the &lt;code&gt;select&lt;/code&gt; call will return immediately.
The &lt;code&gt;wait&lt;/code&gt;/&lt;code&gt;notify&lt;/code&gt; calls do not behave this way;
if a call is made to &lt;code&gt;notify&lt;/code&gt; when there is no thread waiting
in a &lt;code&gt;wait&lt;/code&gt; call on that
&lt;a href="http://www.artima.com/insidejvm/ed2/threadsynch.html"&gt;
monitor&lt;/a&gt;, the &lt;code&gt;notify&lt;/code&gt; call
does nothing, and the following call to &lt;code&gt;wait&lt;/code&gt; will wait until
the next call to &lt;code&gt;notify&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
This small difference in semantics actually makes a pretty big difference
in behavior, because it means when using &lt;code&gt;wait&lt;/code&gt; and
&lt;code&gt;notify&lt;/code&gt; you must be concerned with which happens first.
Let's see how that works.

&lt;br/&gt;&lt;br/&gt;
In a typical scenario we have a resource with a boolean state that
indicates when a thread can access that resource,
for example, a queue with a boolean state of "has some data" that indicates when
a reader thread can pull an item from the queue (and perhaps another boolean
state of "queue is full" that indicates when a writer thread can put an item
into the queue).
In the case of &lt;code&gt;MultiThreadCoScheduler&lt;/code&gt;
we have a task with a "ready" flag that tells us when we can
assign that task to a worker,
and a worker with an "idle" flag that tells us when we can
assign a task to that worker.
When a task becomes ready to run, we want a thread
(other than the master, since it may be waiting)
to add the task to our queue of
tasks and then notify the master that a task is available.
Meanwhile, when the master is looking for an available task to assign
to an idle worker, it will query to
see if a task is available, and if not it will then wait until one becomes
available.
The problem sequence would be if the master checks for available tasks,
finds none, then before the master executes its wait, the non-master puts
a ready task into the queue and issues a notify to the master.
The result of this sequence would be a ready task in the queue, but a
master waiting for a notify.

&lt;br/&gt;&lt;br/&gt;
When all of the synchronization is done within a single class, you can
ensure that the above problem sequencing of operations does not happen
by arranging that the code that places a ready task into the queue and
notifies the master happens within one &lt;code&gt;synchronized&lt;/code&gt; block,
and the code used by the master to query the queue for a ready task and
then to wait happens within one &lt;code&gt;synchronized&lt;/code&gt; block on the same
monitor.
But when dealing with subclasses, we run into the
"&lt;a href="http://www.scala-lang.org/node/9811"&gt;inheritance anomaly&lt;/a&gt;"
(or "inheritance-synchronization anomaly").
The essence of this problem is that the base class provides a method
that is synchronized, but the subclass would like to include more
functionality within that synchronized block.
If, as is often the case, the subclass does not have access to the monitor
being used by the base class to control its synchronization,
there is no way for it to do this.

&lt;br/&gt;&lt;br/&gt;
In our case, we can implement something that is sufficient for our
current needs by
making a small change to our &lt;code&gt;coWait&lt;/code&gt;
and &lt;code&gt;coNotify&lt;/code&gt; methods in &lt;code&gt;CoScheduler&lt;/code&gt;
so that they behave in the same manner as
&lt;code&gt;select&lt;/code&gt; and &lt;code&gt;wakeup&lt;/code&gt;:
if a call to &lt;code&gt;coNotify&lt;/code&gt; is made before a call to &lt;code&gt;coWait&lt;/code&gt;,
the call to &lt;code&gt;coWait&lt;/code&gt; will return immediately.
We do this by changing the implementation of &lt;code&gt;coWait&lt;/code&gt; and
&lt;code&gt;coNotify&lt;/code&gt; in &lt;code&gt;CoScheduler&lt;/code&gt; from this:

&lt;pre name="hlcode" class="scala"
&gt;    def coWait():Unit = {
        defaultLock.synchronized {
            defaultLock.wait()
        }
    }

    def coNotify():Unit = {
        defaultLock.synchronized {
            defaultLock.notify
        }
    }
&lt;/pre&gt;

to this:

&lt;pre name="hlcode" class="scala"
&gt;    private var notified = false
    def coWait():Unit = {
        defaultLock.synchronized {
            if (!notified)
                defaultLock.wait()
            notified = false
        }
    }

    def coNotify():Unit = {
        defaultLock.synchronized {
            notified = true
            defaultLock.notify
        }
    }
&lt;/pre&gt;

With the above change to our base class, our subclass no longer needs to
be concerned about the problem sequence described above,
because the call to &lt;code&gt;coWait&lt;/code&gt; will return immediately if there
was a call to &lt;code&gt;coNotify&lt;/code&gt; since the most recent previous call
to &lt;code&gt;coWait&lt;/code&gt;.
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-63042369280290473?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/63042369280290473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=63042369280290473' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/63042369280290473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/63042369280290473'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/07/multithread-coroutine-scheduler.html' title='Multithread Coroutine Scheduler'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4500294478085907734</id><published>2011-06-25T07:46:00.000-07:00</published><updated>2011-06-25T07:46:15.757-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='life'/><title type='text'>Sledgehammer Words</title><content type='html'>Words are tools that we use to
clarify our concepts, express our emotions and
persuade others to our positions.
We use those tools to craft mental models which we deliver to our listener.
The better the job we do with those tools,
the more effectively we can communicate our message.

&lt;br/&gt;&lt;br/&gt;
The words we use every day are our basic tools.
Like screwdrivers and pliers, these words are simple but versatile,
performing adequately for most tasks.
Occasionally we might want to use a more esoteric word for
a specific task, as we might pull out a pair of
&lt;a href="http://www.grainger.com/Grainger/PROTO-Bent-Needle-Nose-Pliers-3R209"&gt;
bent needle nose pliers&lt;/a&gt;
when that tool is just right for the job.

&lt;br/&gt;&lt;br/&gt;
The better your selection of tools, the better job you can do at making
a beautiful and effective work.
In a pinch you can use a slot-head screwdriver to set a Phillips screw,
but you stand a higher chance of damaging the screw head and it is more
difficult to set it just right.
Similarly but more subtly, you may be able to use a Phillips
screwdriver to set a
&lt;a href="http://www.sizes.com/tools/screw_drive.htm#Frearson"&gt;
Frearson&lt;/a&gt; screw, but you will be able to do
a better job if you have a Frearson driver.
Most of us will probably not need this level of distinction and can get
by with just a Phillips, or indeed perhaps with just a slot-head driver,
but if you want to be able to craft the best results over the widest
range of projects, having that Frearson screwdriver in your toolbox
will provide one more area in which you can do things better.

&lt;br/&gt;&lt;br/&gt;
Swear words are the sledgehammers of our verbal toolbox.
Like a sledgehammer, a swear word can pack a lot of punch,
and like a sledgehammer it lacks precision.
Sometimes a sledgehammer is the right tool for the job:
when you need to smash a hole in something, one good whack with a
sledgehammer can be far more effective than trying to use pliers
and screwdrivers to do the same thing.

&lt;br/&gt;&lt;br/&gt;
But for most of us, most of the time, that's not the job we are trying to do.
Most of the time we are more interested in making a neat hole, and
we should pull out the electric drill, or the hole saw, or even the
Sawzall to do the job; or we just need to tap in a small nail,
where a standard hammer would work nicely.
If we smash it with a sledgehammer, it's likely that we will then need
to spend a lot of time cleaning things up afterwards, which would probably
be more work than using one of the other tools in the first place.

&lt;br/&gt;&lt;br/&gt;
Some people seem to have a very small toolbox
and are constantly swinging around that sledgehammer.
They use it for almost everything; rather than pulling out a
screwdriver to set a screw, they whack it with their sledgehammer.
To me, everything these people say seems like a pile of smashed rubble.
I doubt that's really the message they want to deliver.

&lt;br/&gt;&lt;br&gt;
Even a single use of a sledgehammer word can derail
any kind of nuance or subtlety,
and casual use will likely overwhelm everything else in the message.

&lt;br/&gt;&lt;br/&gt;
So go ahead and use a sledgehammer when it is appropriate,
but do so deliberately and fully conscious of your intended result.
Make an effort to add a good assortment of tools to your toolbox,
understand what you are trying to accomplish,
learn to use the best tool for the job and use it well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4500294478085907734?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4500294478085907734/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4500294478085907734' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4500294478085907734'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4500294478085907734'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/06/sledgehammer-words.html' title='Sledgehammer Words'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-418289224632987482</id><published>2011-04-15T11:08:00.000-07:00</published><updated>2011-04-15T11:08:34.093-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Java Nio Complete Scala Server</title><content type='html'>The capstone to this series of posts:
a complete multi-client stateful application server in Scala
using Java NIO non-blocking IO for both reading and writing,
and delimited continuations as coroutines
for both IO and application processing.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#background"&gt;Background&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioapplication"&gt;NioApplication&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioserver"&gt;NioServer&lt;/a&gt;
&lt;li&gt;&lt;a href="#niolistener"&gt;NioListener&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioconnection"&gt;NioConnection&lt;/a&gt;
&lt;li&gt;&lt;a href="#echoserver"&gt;EchoServer&lt;/a&gt;
&lt;li&gt;&lt;a href="#three-questions-server"&gt;ThreeQuestionsServer&lt;/a&gt;
&lt;li&gt;&lt;a href="#limitations"&gt;Limitations&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="background"&gt;&lt;/a&gt;
&lt;h3&gt;Background&lt;/h3&gt;

In the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html"&gt;
initial post&lt;/a&gt;
of this series on
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/guide/nio/"&gt;
Java NIO&lt;/a&gt;
in Scala I mentioned a set of
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;
Limitations&lt;/a&gt;
of the first example server.
In the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-for-character-decoding-in.html"&gt;
next&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2011/04/java-nio-and-scala-coroutines.html"&gt;
three&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2011/04/java-nio-for-writing.html"&gt;
posts&lt;/a&gt;
after that initial post
I addressed some of those limitations.
In this post I address the remaining limitation in that original list:
the application code (an echo loop in the example) is buried in the
&lt;code&gt;NioConnection&lt;/code&gt; class,
which makes that application code more difficult to maintain and
makes the server code not directly reusable as a library.

&lt;br/&gt;&lt;br/&gt;
With the changes described in the next section,
all of the application-specific behavior will be
encapsulated in an instance of an application-specific
subclass of a new class, &lt;code&gt;NioApplication&lt;/code&gt;.
Since the remainder of the classes presented so far will now be
independent of the application and reusable
without any modifications for multiple applications,
they will be moved into a separate package, &lt;code&gt;net.jimmc.nio&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Other than adding &lt;code&gt;package net.jimmc.nio&lt;/code&gt;,
there were no changes to &lt;code&gt;LineDecoder&lt;/code&gt;
and &lt;code&gt;NioSelector&lt;/code&gt;,
and there were no changes to the coroutine package
&lt;code&gt;net.jimmc.scoroutine&lt;/code&gt;
for this latest set of changes.
For the files that were changed, listed below,
the listings show the complete new version of the file,
with changes from the previous version highlighted in &lt;b&gt;bold&lt;/b&gt;.

&lt;br/&gt;&lt;br/&gt;
The complete source for this series of posts is available on github in my
&lt;a href="https://github.com/jimmc/nioserver"&gt;nioserver&lt;/a&gt; project,
with the specific version after the changes specified in this post tagged as
&lt;a href="https://github.com/jimmc/nioserver/tree/blog-complete"&gt;
blog-complete&lt;/a&gt;.

&lt;a name="nioapplication"&gt;&lt;/a&gt;
&lt;h3&gt;NioApplication&lt;/h3&gt;

Extracting the application-specific code out of &lt;code&gt;NioConnection&lt;/code&gt;
is pretty simple:
in &lt;code&gt;NioConnection.startApp&lt;/code&gt;,
rather than starting up a built-in echo loop,
we add a hook that allows us to call back to an application-specific
method that implements whatever behavior the application wants for
dealing with a connection.
To do this, we define a new abstract class &lt;code&gt;NioApplication&lt;/code&gt;
that includes a &lt;code&gt;runConnection&lt;/code&gt; method that we can call
from &lt;code&gt;NioConnection.startApp&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
We will also use the &lt;code&gt;NioApplication&lt;/code&gt; class as a convenience
class where we can bundle up some of the arguments that get passed
around a lot, in particular the coroutine scheduler and the
read and write selectors.
This gives us the opportunity to override the coroutine scheduler
with one more appropriate for the application,
although we will not do so in this example.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;package net.jimmc.nio

import net.jimmc.scoroutine.DefaultCoScheduler

import scala.util.continuations._

abstract class NioApplication {
    val readSelector = new NioSelector()
    val writeSelector = new NioSelector()
    val sched = new DefaultCoScheduler

    def runConnection(conn:NioConnection):Unit @suspendable
}&lt;/b&gt;
&lt;/pre&gt;

&lt;a name="nioserver"&gt;&lt;/a&gt;
&lt;h3&gt;NioServer&lt;/h3&gt;

We simplify the &lt;code&gt;NioServer&lt;/code&gt; class by removing
&lt;code&gt;object NioServer&lt;/code&gt;, which will instead be in the application
main object.
We replace three parameters in the constructor with the
single &lt;code&gt;app&lt;/code&gt; parameter
and likewise replace three arguments in the call to
&lt;code&gt;NioListener&lt;/code&gt; with the single &lt;code&gt;app&lt;/code&gt; argument.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;package net.jimmc.nio&lt;/b&gt;

import net.jimmc.scoroutine.DefaultCoScheduler

import java.net.InetAddress

class NioServer(&lt;b&gt;app:NioApplication,&lt;/b&gt; hostAddr:InetAddress, port:Int) {
    val listener = new NioListener(&lt;b&gt;app,&lt;/b&gt; hostAddr, port)

    def start() {
        listener.start(true)
        //run the NIO read and write selectors each on its own thread
        (new Thread(&lt;b&gt;app.&lt;/b&gt;writeSelector,"WriteSelector")).start
        (new Thread(&lt;b&gt;app.&lt;/b&gt;readSelector,"ReadSelector")).start
        Thread.currentThread.setName("CoScheduler")
        &lt;b&gt;app.&lt;/b&gt;sched.run    //run the coroutine scheduler on our thread, renamed
    }
}
&lt;/pre&gt;

&lt;a name="niolistener"&gt;&lt;/a&gt;
&lt;h3&gt;NioListener&lt;/h3&gt;

Three parameters in the constructor have been replaced by the single
&lt;code&gt;app&lt;/code&gt; parameter.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;package net.jimmc.nio&lt;/b&gt;

import net.jimmc.scoroutine.CoScheduler

import java.net.{InetAddress,InetSocketAddress}
import java.nio.channels.{ServerSocketChannel,SocketChannel}
import java.nio.channels.SelectionKey
import scala.util.continuations._

class NioListener(&lt;b&gt;app:NioApplication,&lt;/b&gt; hostAddr:InetAddress, port:Int) {

    val serverChannel = ServerSocketChannel.open()
    serverChannel.configureBlocking(false);
    val isa = new InetSocketAddress(hostAddr,port)
    serverChannel.socket.bind(isa)

    def start(continueListening: =&amp;gt;Boolean):Unit = {
        reset {
            while (continueListening) {
                val socket = accept()
                NioConnection.newConnection(&lt;b&gt;app,&lt;/b&gt; socket)
            }
        }
    }

    private def accept():SocketChannel @suspendable = {
        shift { k =&amp;gt;
            &lt;b&gt;app.&lt;/b&gt;readSelector.register(serverChannel,SelectionKey.OP_ACCEPT, {
                val conn = serverChannel.accept()
                conn.configureBlocking(false)
                k(conn)
            })
        }
    }
}
&lt;/pre&gt;

&lt;a name="nioconnection"&gt;&lt;/a&gt;
&lt;h3&gt;NioConnection&lt;/h3&gt;

We modify the constructor and the companion to replace three parameters
with the single &lt;code&gt;app&lt;/code&gt; parameter, and we replace our echo loop
in &lt;code&gt;startApp&lt;/code&gt; with a call to the application
&lt;code&gt;runConnection&lt;/code&gt; method,
followed by a call to our &lt;code&gt;close&lt;/code&gt; method to make sure we
close the socket when the application is done with it.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;package net.jimmc.nio&lt;/b&gt;

import net.jimmc.scoroutine.{CoQueue,CoScheduler}

import java.nio.ByteBuffer
import java.nio.channels.SelectionKey
import java.nio.channels.SocketChannel
import scala.util.continuations._

object NioConnection {
    def newConnection(&lt;b&gt;app:NioApplication,&lt;/b&gt; socket:SocketChannel) {
        val conn = new NioConnection(&lt;b&gt;app,&lt;/b&gt; socket)
        conn.start()
    }
}

class NioConnection(&lt;b&gt;app:NioApplication,&lt;/b&gt; socket:SocketChannel) {

    private val buffer = ByteBuffer.allocateDirect(2000)
    private val lineDecoder = new LineDecoder
    private val inQ = new CoQueue[String](&lt;b&gt;app.&lt;/b&gt;sched, 10)
    private val outQ = new CoQueue[String](&lt;b&gt;app.&lt;/b&gt;sched, 10)

    def start():Unit = {
        startReader
        startWriter
        startApp
    }

    private def startApp() {
        reset {
            &lt;b&gt;app.runConnection(this)
            close()&lt;/b&gt;
        }
    }

    private def startReader() {
        reset {
            while (socket.isOpen)
                readWait
        }
    }

    private def readWait&lt;b&gt;:Unit @suspendable&lt;/b&gt; = {
        buffer.clear()
        val count = read(buffer)
        if (count&amp;lt;1) {
            socket.close()
            shiftUnit[Unit,Unit,Unit]()
        } else {
            buffer.flip()
            lineDecoder.processBytes(buffer, inQ.blockingEnqueue(_))
        }
    }

    private def read(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            &lt;b&gt;app.&lt;/b&gt;readSelector.register(socket, SelectionKey.OP_READ, {
                val n = socket.read(b)
                k(n)
            })
        }
    }

    def readLine():String @suspendable = inQ.blockingDequeue

    private def startWriter() {
        reset {
            while (socket.isOpen)
                writeWait
        }
    }

    private def write(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            &lt;b&gt;app.&lt;/b&gt;writeSelector.register(socket, SelectionKey.OP_WRITE, {
                val n = socket.write(b)
                k(n)
            })
        }
    }

    private def writeBuffer(b:ByteBuffer):Unit @suspendable = {
        write(b)
        if (b.remaining&amp;gt;0 &amp;amp;&amp;amp; socket.isOpen)
            writeBuffer(b)
        else
            shiftUnit[Unit,Unit,Unit]()
    }

    private def writeWait():Unit @suspendable = {
        val str = outQ.blockingDequeue
        if (str eq closeMarker) {
            socket.close
            shiftUnit[Unit,Unit,Unit]()
        } else
            writeBuffer(ByteBuffer.wrap(str.getBytes("UTF-8")))
    }

    def writeLine(s:String) = write(s+"\n")
    def write(s:String) = outQ.blockingEnqueue(s)

    def isOpen = socket.isOpen
    private val closeMarker = new String("")
    def close():Unit @suspendable = write(closeMarker)
}
&lt;/pre&gt;

&lt;a name="echoserver"&gt;&lt;/a&gt;
&lt;h3&gt;EchoServer&lt;/h3&gt;

We move the application-specific main object out of &lt;code&gt;NioServer&lt;/code&gt;
and place it into our sample application class, which we call
&lt;code&gt;EchoServer&lt;/code&gt;, along with a subclassed &lt;code&gt;NioApplication&lt;/code&gt;
that provides our application behavior.

&lt;br/&gt;&lt;br/&gt;
Highlighted differences are as compared to the previous version
of &lt;code&gt;NioServer&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;import net.jimmc.nio.{NioApplication,NioConnection,NioServer}&lt;/b&gt;
import net.jimmc.scoroutine.DefaultCoScheduler

import java.net.InetAddress
&lt;b&gt;import scala.util.continuations._&lt;/b&gt;

object &lt;b&gt;EchoServer&lt;/b&gt; {
    def main(args:Array[String]) {
        &lt;b&gt;val app = new EchoApplication&lt;/b&gt;
        val hostAddr:InetAddress = null //listen on local connection
        val port = 1234
        val server = new NioServer(&lt;b&gt;app,&lt;/b&gt;hostAddr,port)
        server.start()
    }
}

&lt;b&gt;class EchoApplication extends NioApplication {
    def runConnection(conn:NioConnection):Unit @suspendable = {
        while (conn.isOpen) {
            conn.writeLine(conn.readLine)
        }
    }
}&lt;/b&gt;
&lt;/pre&gt;

The above class is the complete application definition for our
echo server when built on top of our generic nio package.
After compiling, run with this command:
&lt;pre name="hlcode" class="bash"
&gt;$ scala EchoServer
&lt;/pre&gt;

With all the above changes, we have once again internally transformed
our application, but besides starting it up with a different name
it's external behavior is still the same.
However, we have reached the point where defining a new server-based
application is easy.

&lt;a name="three-questions-server"&gt;&lt;/a&gt;
&lt;h3&gt;ThreeQuestionsServer&lt;/h3&gt;

The example in this section shows a slightly more complex application
that maintains some local per-client state as it progresses through a
short series of steps interacting with the client.
In this simple application, the server asks up to
&lt;a href="http://www.imdb.com/title/tt0071853/quotes#qt0470601"&gt;
three questions&lt;/a&gt;
of the client and collects responses,
with each next question sometimes depending on the previous answers.
The per-client state is contained both in local variables and in
the location of execution within the application.
Each time the processing for a client is suspended the state for that
client is captured in a continuation to be restored when the next piece
of input is available.
The continuation includes all of the above per-client state information,
so we don't have to write any application-specific
code to save and restore that data.

&lt;br/&gt;&lt;br/&gt;
By defining the &lt;code&gt;ReaderWriter&lt;/code&gt; interface trait,
the application is written so as to be able to run either in server mode
using an instance of &lt;code&gt;ConnReader&lt;/code&gt;,
in which case it accepts connections from clients,
or in standalone mode using an instance of &lt;code&gt;SysReader&lt;/code&gt;,
in which case it only interacts with the console.

&lt;br/&gt;&lt;br/&gt;
When our application running in server mode finishes handling a client
and exits from the
&lt;code&gt;run&lt;/code&gt; method,
control returns to &lt;code&gt;NioConnection&lt;/code&gt;,
which closes the connection.

&lt;pre name="hlcode" class="scala"
&gt;import net.jimmc.nio.{NioApplication,NioServer,NioConnection}

import java.io.{BufferedReader,InputStreamReader,PrintWriter}
import java.net.InetAddress

import scala.util.continuations._

object ThreeQuestionsConsole {
    def main(args:Array[String]) {
        val in = new BufferedReader(new InputStreamReader(System.in))
        val out = new PrintWriter(System.out)
        val io = new SysReader(in,out)
        reset {
            (new ThreeQuestions(io)).run
        }
    }
}

object ThreeQuestionsServer {
    def main(args:Array[String]) {
        val app = new ThreeQuestionsApp
        val hostAddr:InetAddress = null //localhost
        val port = 1234
        val server = new NioServer(app,hostAddr,port)
        server.start()
    }
}

class ThreeQuestionsApp extends NioApplication {
    def runConnection(conn:NioConnection):Unit @suspendable = {
        val io = new ConnReader(conn)
        (new ThreeQuestions(io)).run
    }
}

trait ReaderWriter {
    def readLine():String @suspendable
    def writeLine(s:String):Unit @suspendable
}

class SysReader(in:BufferedReader,out:PrintWriter) extends ReaderWriter {
    def readLine() = in.readLine
    def writeLine(s:String) = { out.println(s); out.flush() }
}

class ConnReader(conn:NioConnection) extends ReaderWriter {
    def readLine():String @suspendable = conn.readLine
    def writeLine(s:String):Unit @suspendable = conn.writeLine(s)
}

class ThreeQuestions(io:ReaderWriter) {
    def run():Unit @suspendable = {
        val RxArthur = ".*arthur.*".r
        val RxGalahad = ".*galahad.*".r
        val RxLauncelot = ".*(launcelot|lancelot).*".r
        val RxRobin = ".*robin.*".r
        val RxHolyGrail = ".*seek the holy grail.*".r
        val RxSwallow = ".*african or european.*".r
        val RxAssyriaCapital =
            ".*(assur|shubat.enlil|kalhu|calah|nineveh|dur.sharrukin).*".r
        val name = ask("What is your name?").toLowerCase
        val quest = ask("What is your quest?").toLowerCase
        val holy = quest match {
            case RxHolyGrail() =&amp;gt; true
            case _ =&amp;gt; false
        }
        if (holy) {
            val q3Type = name match {
                case RxRobin() =&amp;gt; 'capital
                case RxArthur() =&amp;gt; 'swallow
                case _ =&amp;gt; 'color
            }
            val a3 = (q3Type match {
                case 'capital =&amp;gt; ask("What is the capital of Assyria?")
                case 'swallow =&amp;gt; ask("What is the air-speed velocity of an unladen swallow?")
                case 'color =&amp;gt; ask("What is your favorite color?")
            }).toLowerCase
            (q3Type,a3,name) match {
                //Need to use an underscore in regex patterns with alternates
                case ('capital,RxAssyriaCapital(_),_) =&amp;gt; accept
                case ('capital,_,_) =&amp;gt; reject
                case ('swallow,RxSwallow(),_) =&amp;gt; rejectMe
                case ('swallow,_,_) =&amp;gt; reject
                case ('color,"blue",RxLauncelot(_)) =&amp;gt; accept
                case ('color,_,RxLauncelot(_)) =&amp;gt; reject
                case ('color,"yellow",RxGalahad()) =&amp;gt; accept
                case ('color,_,RxGalahad()) =&amp;gt; reject
                case ('color,_,_) =&amp;gt; accept
            }
        } else {
            reject
        }
    }

    def ask(s:String):String @suspendable = { io.writeLine(s); io.readLine }
    def accept:Unit @suspendable = io.writeLine("You may pass")
    def reject:Unit @suspendable = io.writeLine("you: Auuuuuuuugh!")
    def rejectMe:Unit @suspendable = io.writeLine("me: Auuuuuuuugh!")
}
&lt;/pre&gt;

To run in console or server mode, use one of the following two commands:
&lt;pre name="hlcode" class="bash"
&gt;$ scala ThreeQuestionsConsole
$ scala ThreeQuestionsServer
&lt;/pre&gt;

&lt;a name="limitations"&gt;&lt;/a&gt;
&lt;h3&gt;Limitations&lt;/h3&gt;

I am calling this version complete because it addresses all of the issues
in the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;
Limitations&lt;/a&gt; section of my original post,
but it is far from production-ready.
Before putting this code into production I would address the following issues.

&lt;ul&gt;
&lt;li&gt;Although the application now uses more than one thread, it still runs
    all of the application code on a single thread.
    The scheduler should be replaced by one that can choose how many
    threads to use and distribute the execution of the coroutines among
    those threads.
&lt;li&gt;This version still has not addressed all of the issues raised in the
    &lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-for-character-decoding-in.html#limitations"&gt;
    Limitations&lt;/a&gt; section of the second post in this series,
    on character decoding.  In particular:
    &lt;ul&gt;
    &lt;li&gt;Error handling should be improved.
    &lt;li&gt;It only supports UTF-8 encoding.
    &lt;/ul&gt;
    For an example of this problem, type a Control-C into your telnet
    window when connected to the EchoServer application.
&lt;li&gt;The application should parse its command line arguments so that
    it has the flexibility to, for example, use a different port number
    without requiring a code change.
&lt;li&gt;The application should
    &lt;a href="http://jim-mcbeath.blogspot.com/2010/01/reload-that-config-file.html"&gt;
    read a configuration file&lt;/a&gt;.
&lt;li&gt;Error handling in general needs to be improved.
&lt;li&gt;Logging should be added.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-418289224632987482?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/418289224632987482/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=418289224632987482' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/418289224632987482'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/418289224632987482'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/04/java-nio-complete-scala-server.html' title='Java Nio Complete Scala Server'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-2367862502461710213</id><published>2011-04-08T08:58:00.000-07:00</published><updated>2011-04-08T08:58:30.851-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Java NIO for Writing</title><content type='html'>Using Java NIO non-blocking IO for writing as well as reading
is almost - but not quite - straightforward.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#background"&gt;Background&lt;/a&gt;
&lt;li&gt;&lt;a href="#implementation"&gt;Implementation&lt;/a&gt;
&lt;li&gt;&lt;a href="#two-selectors"&gt;Two Selectors&lt;/a&gt;
&lt;li&gt;&lt;a href="#close"&gt;Close&lt;/a&gt;
&lt;li&gt;&lt;a href="#summary"&gt;Summary&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="background"&gt;&lt;/a&gt;
&lt;h3&gt;Background&lt;/h3&gt;

One of the limitations pointed out in the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;
Limitations&lt;/a&gt;
section of the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html"&gt;
original post&lt;/a&gt; in this series
was that we were still directly writing our output data to the socket
rather than using
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#java-nonblocking-io"&gt;
non-blocking IO&lt;/a&gt; and
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
continuations&lt;/a&gt; as we were doing
when reading our input data.
If a client stops reading its input
(or if there is sufficient network congestion that it looks that way
from our end)
then our socket output buffer
may fill up.
If that happens, then one of two things will happen when we try to write
our data to that socket: either the call will block, or the data will
not all be written.
If the call blocks, then we have a blocked thread that we can not use
for processing other clients until it is unblocked.
If there are many clients who are not reading their input,
we could have many blocked threads.
Since one of the goals of this exercise is to be able to run many clients
on a relatively small number of threads, having blocked threads is bad.
To avoid this problem, we use non-blocking output and continuations
for writing to the output,
just as we did for reading the input.

&lt;br/&gt;&lt;br/&gt;
The complete source for this series of posts is available on github in my
&lt;a href="https://github.com/jimmc/nioserver"&gt;nioserver&lt;/a&gt; project,
with the specific version after the changes specified in this post tagged as
&lt;a href="https://github.com/jimmc/nioserver/tree/blog-write"&gt;
blog-write&lt;/a&gt;.

&lt;a name="implementation"&gt;&lt;/a&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;

We model the output code on the input code by making these changes:

&lt;ul&gt;
&lt;li&gt;We write a suspending &lt;code&gt;write&lt;/code&gt; method that registers
    our interest in writing to the output socket connection.
&lt;li&gt;We add an output queue to receive data from the application.
&lt;li&gt;We modify the &lt;code&gt;writeLine&lt;/code&gt;
    method to add a line to the output queue rather than writing
    directly to the output socket.
&lt;li&gt;We run a separate control loop that reads from the output queue
    and writes to the output socket.
&lt;/ul&gt;

&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;//In class NioConnection
    &lt;b&gt;private val outQ = new CoQueue[String](sched, 10)&lt;/b&gt;

    def start():Unit = {
        startReader
        &lt;b&gt;startWriter&lt;/b&gt;
        startApp
    }

    &lt;b&gt;private def startWriter() {
        reset {
            while (socket.isOpen)
                writeWait
        }
    }

    private def write(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            selector.register(socket, SelectionKey.OP_WRITE, {
                val n = socket.write(b)
                k(n)
            })
        }
    }

    private def writeBuffer(b:ByteBuffer):Unit @suspendable = {
        write(b)
        if (b.remaining&amp;gt;0 &amp;amp;&amp;amp; socket.isOpen)
            writeBuffer(b)
        else
            shiftUnit[Unit,Unit,Unit]()
    }

    private def writeWait:Unit @suspendable = {
        val str = outQ.blockingDequeue
        writeBuffer(ByteBuffer.wrap(str.getBytes("UTF-8")))
    }&lt;/b&gt;

    def writeLine(s:String)&lt;b&gt;:Unit @suspendable = write(s+"\n")
    def write(s:String):Unit @suspendable = outQ.blockingEnqueue(s)&lt;/b&gt;
&lt;/pre&gt;

This seems pretty straightforward, but unfortunately it doesn't work.
The problem is that we have attempted to register our channel twice
(once for read and once for write) with the same selector.
The documentation for
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html"&gt;
&lt;code&gt;SelectableChannel&lt;/code&gt;&lt;/a&gt; says,
"&lt;i&gt;A channel may be registered at most once with any particular selector.&lt;/i&gt;"
If we call
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html#register(java.nio.channels.Selector,%20int,%20java.lang.Object)"&gt;
&lt;code&gt;register&lt;/code&gt;&lt;/a&gt;
for our channel for write when it is
already registered for read, the read registration is overwritten by
the write registration and is lost.

&lt;br/&gt;&lt;br/&gt;
In his 
&lt;a href="http://rox-xmlrpc.sourceforge.net/niotut/"&gt;
Rox Java NIO Tutorial&lt;/a&gt; James Greenfield
&lt;a href="http://rox-xmlrpc.sourceforge.net/niotut/#General%20principles"&gt;
explicitly recommends&lt;/a&gt; that you
"&lt;i&gt;Use a single selecting thread&lt;/i&gt;" and
"&lt;i&gt;Modify the selector from the selecting thread only.&lt;/i&gt;"
We could take this approach,
adding some code to combine the read and write
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectionKey.html#field_summary"&gt;
interest flags&lt;/a&gt;
when we are in that position, but
unlike in James' case
we would also need
to add some code to demultiplex the separate callbacks for read and
write.
Instead, we use a different approach:
we use separate selectors for reading and writing, and we give each of
them its own thread.

&lt;a name="two-selectors"&gt;&lt;/a&gt;
&lt;h3&gt;Two Selectors&lt;/h3&gt;

Depending on the implementation, using two selectors and two threads this
way could cause problems.
However, based on my understanding of the documentation,
&lt;a href="http://www.docjar.com/html/api/sun/nio/ch/EPollSelectorImpl.java.html"&gt;
the code&lt;/a&gt; in
the Sun implementation and the operation of the
&lt;a href="http://linux.die.net/man/2/select"&gt;
POSIX select&lt;/a&gt;
operation,
I believe this approach should work (at least on POSIX systems).
This would need to be tested on all supported
platforms for a production system.

&lt;br/&gt;&lt;br/&gt;
To use separate read and write selectors, we replace the current
&lt;code&gt;selector&lt;/code&gt; parameter in &lt;code&gt;NioConnection&lt;/code&gt; with
two parameters &lt;code&gt;readSelector&lt;/code&gt; and &lt;code&gt;writeSelector&lt;/code&gt;
of the same type.

&lt;pre name="hlcode" class="scala"
&gt;//In object NioConnection:
    def newConnection(sched:CoScheduler, &lt;b&gt;readSelector&lt;/b&gt;:NioSelector,
            &lt;b&gt;writeSelector:NioSelector,&lt;/b&gt; socket:SocketChannel) {
        val conn = new NioConnection(sched,&lt;b&gt;readSelector&lt;/b&gt;,
            &lt;b&gt;writeSelector,&lt;/b&gt;socket)
        conn.start()
    }

class NioConnection(sched:CoScheduler, &lt;b&gt;readSelector&lt;/b&gt;:NioSelector, 
        &lt;b&gt;writeSelector:NioSelector,&lt;/b&gt; socket:SocketChannel) {
    ...
    private def read(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            &lt;b&gt;readSelector&lt;/b&gt;.register(socket, SelectionKey.OP_READ, {
                val n = socket.read(b)
                k(n)
            })
        }
    }

    private def write(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            &lt;b&gt;writeSelector&lt;/b&gt;.register(socket, SelectionKey.OP_WRITE, {
                val n = socket.write(b)
                k(n)
            })
        }
    }

    ...
}
&lt;/pre&gt;

We also change &lt;code&gt;NioListener&lt;/code&gt; to pass through those
two arguments, and we choose to use the &lt;code&gt;readSelector&lt;/code&gt;
to handle our &lt;code&gt;accept&lt;/code&gt; calls.

&lt;pre name="hlcode" class="scala"
&gt;//In NioListener
class NioListener(sched:CoScheduler, &lt;b&gt;readSelector&lt;/b&gt;:NioSelector,
        &lt;b&gt;writeSelector:NioSelector,&lt;/b&gt; hostAddr:InetAddress, port:Int) {
    ...
    def start(continueListening: =&amp;gt;Boolean):Unit = {
        reset {
            while (continueListening) {
                val socket = accept()
                NioConnection.newConnection(sched,
                    &lt;b&gt;readSelector,writeSelector&lt;/b&gt;,socket)
            }
        }
    }

    private def accept():SocketChannel @suspendable = {
        shift { k =&amp;gt;
            &lt;b&gt;readSelector&lt;/b&gt;.register(serverChannel,SelectionKey.OP_ACCEPT, {
                val conn = serverChannel.accept()
                conn.configureBlocking(false)
                k(conn)
            })
        }
    }
}
&lt;/pre&gt;

Finally, we instantiate the new write selector in &lt;code&gt;NioServer&lt;/code&gt;,
pass it in to &lt;code&gt;NioListener&lt;/code&gt;, and start it running
in a new thread.

&lt;pre name="hlcode" class="scala"
&gt;//In NioServer
class NioServer(hostAddr:InetAddress, port:Int) {
    val &lt;b&gt;readSelector&lt;/b&gt; = new NioSelector()
    &lt;b&gt;val writeSelector = new NioSelector()&lt;/b&gt;
    val sched = new DefaultCoScheduler
    val listener = new NioListener(sched, 
        &lt;b&gt;readSelector, writeSelector,&lt;/b&gt; hostAddr, port)

    def start() {
        listener.start(true)
        //run the NIO &lt;b&gt;read and write selectors each&lt;/b&gt; on its own thread
        &lt;b&gt;(new Thread(writeSelector,"WriteSelector")).start&lt;/b&gt;
        (new Thread(&lt;b&gt;readSelector,"ReadSelector"&lt;/b&gt;)).start
        Thread.currentThread.setName("CoScheduler")
        sched.run    //run the coroutine scheduler on our thread, renamed
    }
}

&lt;/pre&gt;

&lt;a name="close"&gt;&lt;/a&gt;
&lt;h3&gt;Close&lt;/h3&gt;

Our current example has no terminating condition, so never attempts to
close the connection.
Looking ahead, we expect to have applications that will want to do that,
so we add a &lt;code&gt;close&lt;/code&gt; method to &lt;code&gt;NioConnection&lt;/code&gt;,
and an &lt;code&gt;isOpen&lt;/code&gt; method that allows us to see when it is closed.

&lt;br/&gt;&lt;br/&gt;
We can't just add a close method that directly closes the socket,
because there may still be output data waiting to be written.
Thus we need an implementation that somehow waits until all of the queued
output data has been written to the output before closing the socket.

&lt;br/&gt;&lt;br/&gt;
One easy way to do this is to have a special marker string that we put
into the output queue when the application requests to close the socket.
When our socket output code sees that marker, we know it has already written
out all of the data that came before that marker in the output queue,
so we can close the socket.
By doing the socket close in the same method that does the writes to
the socket, and by ensuring that that method is called on the
(write) selection thread,
we also ensure that the close happens on the selection thread.

&lt;br/&gt;&lt;br/&gt;
The compiler shares constant strings, so to make sure we have a unique
string for our marker that can't be passed in by any code outside of
our &lt;code&gt;close&lt;/code&gt; method, we use &lt;code&gt;new String()&lt;/code&gt;.
In &lt;code&gt;writeWait&lt;/code&gt;, where we check for that marker,
we use the identity comparison &lt;code&gt;eq&lt;/code&gt; when checking for the marker,
and we add a call to &lt;code&gt;shiftUnit&lt;/code&gt; to make both sides of the
&lt;code&gt;if&lt;/code&gt; statement be CPS.

&lt;br/&gt;&lt;br/&gt;
A call to our &lt;code&gt;close&lt;/code&gt; method will return right away,
but the socket will not get closed until after all of the data in
the output queue has been written to the output socket.
The application can tell when the socket has actually been closed
by calling the &lt;code&gt;isOpen&lt;/code&gt; method.

&lt;pre name="hlcode" class="scala"
&gt;//In NioConnection
    private def writeWait():Unit @suspendable = {
        val str = outQ.blockingDequeue
        &lt;b&gt;if (str eq closeMarker) {
            socket.close
            shiftUnit[Unit,Unit,Unit]()
        } else&lt;/b&gt;
            writeBuffer(ByteBuffer.wrap(str.getBytes("UTF-8")))
    }

    &lt;b&gt;def isOpen = socket.isOpen
    private val closeMarker = new String("")
    def close():Unit @suspendable = write(closeMarker)&lt;/b&gt;
&lt;/pre&gt;

&lt;a name="summary"&gt;&lt;/a&gt;
&lt;h3&gt;Summary&lt;/h3&gt;

As in the previous two posts, we have modified the program to make an
internal improvement that has not changed its basic external behavior.
We have, however, changed its behavior for one of the corner cases -
in this case what happens when an output socket fills up, such as might
happen when there is excessive network latency - which is a necessary
improvement for a production application, particularly if one expects
the kind of high volume that would make those corner cases more likely.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-2367862502461710213?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/2367862502461710213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=2367862502461710213' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2367862502461710213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2367862502461710213'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/04/java-nio-for-writing.html' title='Java NIO for Writing'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-2593194217667705639</id><published>2011-04-02T18:33:00.000-07:00</published><updated>2011-04-02T18:33:27.882-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Java NIO and Scala Coroutines</title><content type='html'>I present a multi-client server in Scala that uses coroutines
to allow modularization of stateful client processing
in a way that is independent of threads.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#background"&gt;Background&lt;/a&gt;
&lt;li&gt;&lt;a href="#coroutines"&gt;Coroutines&lt;/a&gt;
&lt;li&gt;&lt;a href="#architecture"&gt;Architecture&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioselector"&gt;NioSelector&lt;/a&gt;
&lt;li&gt;&lt;a href="#coscheduler"&gt;CoScheduler&lt;/a&gt;
&lt;li&gt;&lt;a href="#coqueue"&gt;CoQueue&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioconnection"&gt;NioConnection&lt;/a&gt;
&lt;li&gt;&lt;a href="#linedecoder"&gt;LineDecoder&lt;/a&gt;
&lt;li&gt;&lt;a href="#niolistener"&gt;NioListener&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioserver"&gt;NioServer&lt;/a&gt;
&lt;li&gt;&lt;a href="#summary"&gt;Summary&lt;/a&gt;
&lt;li&gt;&lt;a href="#caveats"&gt;Caveats&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="background"&gt;&lt;/a&gt;
&lt;h3&gt;Background&lt;/h3&gt;

In my
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html"&gt;
previous&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-for-character-decoding-in.html"&gt;
two&lt;/a&gt;
posts I presented a server in Scala that uses
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/guide/nio/"&gt;
Java NIO&lt;/a&gt; non-blocking IO and
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
continuations&lt;/a&gt; to allow
scaling to a large number of clients.
As I pointed out in the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;
Limitations&lt;/a&gt;
section of that first post,
that example used one thread for all execution.
On a multi-core machine, as is common today,
we would prefer to have multiple threads running to allow
us to take advantage of all of the processing power available to us,
yet we don't want to allocate a thread to every client.

&lt;br/&gt;&lt;br/&gt;
It would be nice if we could add our own
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html"&gt;
&lt;code&gt;SelectableChannel&lt;/code&gt;&lt;/a&gt;
types to the set of NIO channel types that we can use with the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html#select()"&gt;
&lt;code&gt;select&lt;/code&gt;&lt;/a&gt;
call so that we
could have one place where we do all our scheduling,
but that feature is not available.
We thus have to come up with another mechanism for handling all of the
other potentially blocking tasks we will want to do.
Fortunately, we already have such a mechanism: coroutines.

&lt;a name="coroutines"&gt;&lt;/a&gt;
&lt;h3&gt;Coroutines&lt;/h3&gt;

Coroutines
provide a separation of the maintenance of task state from
the execution of code for that task,
allowing us to bind execution of the task to different threads as we desire.
When one of our task coroutines becomes blocked waiting for an unavailable
resource, we suspend it by storing its continuation, allowing us to
use that thread for another purpose, such as to restore and run
a different previously stored continuation that is now runnable.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html"&gt;
earlier post&lt;/a&gt; on coroutines I presented an implementation
of a coroutine package that included a scheduler (&lt;code&gt;CoScheduler&lt;/code&gt;)
and a blocking queue (&lt;code&gt;CoQueue&lt;/code&gt;).
We will modify the server implementation
of my previous two "Java NIO" posts
to make use of those classes.

&lt;br/&gt;&lt;br/&gt;
As pointed out in that earlier coroutines post,
the default scheduler implementation in the example can easily be
replaced by another implementation with no other changes to the code.
In particular,
that new implementation could use a thread pool or a group of actors
to execute the coroutines that are ready to run,
assuming the coroutine code itself is multi-thread safe.
We will not write that multi-thread scheduler for this post,
but will assume that it can be written later.

&lt;a name="architecture"&gt;&lt;/a&gt;
&lt;h3&gt;Architecture&lt;/h3&gt;

At a high level, we want to modify our server so that we have a queue
between our socket reader and the application that will eventually
consume the data.
We can then set up a small processing loop that reads the socket data,
converts it to a string and writes it to that queue.
The application will read the contents of the queue, process it,
and write back its results to the connection.
We will let the socket reader continue to run on the select thread, but
we will run the application on a separate thread (or threads),
ensuring that the select loop can quickly get to all
connections and preventing the application processing of any one connection
from delaying the IO of other connections.

&lt;br/&gt;&lt;br/&gt;
With this architecture we have two processing loops:
&lt;ol&gt;
&lt;li&gt;Read data from socket, write to queue.
&lt;li&gt;Read data from queue, process it, write data (to socket, for now).
&lt;/ol&gt;

Given that for now we are writing directly to the connection socket on output
(and ignoring the possibility that the output socket might be blocked),
the second loop only has one potential blocking point:
if there is no data in the queue, it will block when trying to read
from the queue.
The first loop has two potential blocking points:
when it reads data from the socket (if there is no data available),
and when it writes data to the queue (if the queue is full).
The difficulty here is that the potentially blocking socket read must be
handled by the NIO select call,
but the potentially blocking write to the queue can't be handled by
the NIO select call and thus must be handled by our own scheduler.

&lt;br/&gt;&lt;br/&gt;
Having one processing loop that when blocked is sometimes managed by one
scheduler (NIO select) and sometimes by another (our coroutine scheduler)
is not necessarily a problem.
Each scheduler just sees a blocking resource that has a
continuation associated with it; when the blocking resource becomes
available, the continuation is called and the process continues.
The new issue that arises when trying to combine two schedulers like
this is that an action by one scheduler can potentially unblock a task
that is currently controlled by (i.e. in a wait state on) the other scheduler.
Every time we perform an action that might unblock a task
we need to ensure that the appropriate scheduler is not stuck waiting
on the other tasks.
In other words, we need to wake up or notify the schedulers at appropriate
points in our code.

&lt;br/&gt;&lt;br/&gt;
In this post, code which has changed is highlighed in &lt;b&gt;bold&lt;/b&gt;
(when not using Syntax Highlighting).
Changes for &lt;code&gt;CoScheduler&lt;/code&gt; and &lt;code&gt;CoQueue&lt;/code&gt; are
as compared to the code in my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html"&gt;
post&lt;/a&gt;
on coroutines;
changes to
&lt;code&gt;NioSelector&lt;/code&gt;,
&lt;code&gt;NioConnection&lt;/code&gt;,
&lt;code&gt;LineDecoder&lt;/code&gt;,
&lt;code&gt;NioListener&lt;/code&gt; and
&lt;code&gt;NioServer&lt;/code&gt; are as compared to the code in my
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html"&gt;
previous&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-for-character-decoding-in.html"&gt;
two&lt;/a&gt;
posts.

&lt;br/&gt;&lt;br/&gt;
The complete source for this post is available on github in my
&lt;a href="https://github.com/jimmc/nioserver"&gt;nioserver&lt;/a&gt; project,
with the specific version used in this post tagged as
&lt;a href="https://github.com/jimmc/nioserver/tree/blog-coroutines"&gt;
blog-coroutines&lt;/a&gt;.
There are also tags for the previous two posts, so you can compare
using those tags to see the changes between the versions as used
in each post.

&lt;a name="nioselector"&gt;&lt;/a&gt;
&lt;h3&gt;NioSelector&lt;/h3&gt;

As mentioned above,
we have to cooperate with the coroutine scheduler.
In particular, we have to be able to deal with the situation that
we have no active connections, so we are in a select call,
then another thread registers interest in an operation.
The documentation for the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html#selop"&gt;
&lt;code&gt;select&lt;/code&gt;&lt;/a&gt; call states:
&lt;blockquote&gt;
Changes made to the interest sets of a selector's keys while a selection
operation is in progress have no effect upon that operation; they will
be seen by the next selection operation.
&lt;/blockquote&gt;
To terminate the select operation early so that it retries with the
newly registered channel, we add a call to
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html#wakeup()"&gt;
&lt;code&gt;wakeup&lt;/code&gt;&lt;/a&gt;
just after registering our interest.

&lt;br/&gt;&lt;br/&gt;
Unfortunately, this is not enough.
The documentation for the &lt;code&gt;select&lt;/code&gt; call 
is not very precise about
whether it is actually possible to call the &lt;code&gt;register&lt;/code&gt;
call from another thread while the &lt;code&gt;select&lt;/code&gt; call is
blocked waiting for a previously registered channel to become active.
The documentation for
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html"&gt;
&lt;code&gt;SelectableChannel&lt;/code&gt;&lt;/a&gt;
does explicitly say
"&lt;i&gt;Selectable channels are safe for use by multiple concurrent threads&lt;/i&gt;",
but the documentation for the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html#register(java.nio.channels.Selector,%20int,%20java.lang.Object)"&gt;
&lt;code&gt;register&lt;/code&gt;&lt;/a&gt;
method says
"&lt;i&gt;This method will then synchronize on the selector's key set and
    therefore may block if invoked concurrently with another registration
    or selection operation involving the same selector.&lt;/i&gt;"
In fact, the standard Sun implementation does quite a bit of
synchronization, so quite easily gets deadlocked when used by
multiple threads.
In particular, the OS-level select call in the Java &lt;code&gt;select&lt;/code&gt;
method is inside a pair of &lt;code&gt;synchronized&lt;/code&gt; blocks that lock
the set of &lt;code&gt;SelectionKey&lt;/code&gt;s associated with that selector.
If, while the first thread is blocked on the select,
a second thread calls &lt;code&gt;SelectableChannel.register&lt;/code&gt;,
it locks the channel, then attempts to lock on the key set to which
that channel is being added, so it blocks.
If a third thread then tries to register that channel with a second
selector, which the documentation implies is allowed,
the third thread will attempt to lock the channel, which will
block until the second thread unblocks and releases its lock on the channel.

&lt;br/&gt;&lt;br/&gt;
In his 
&lt;a href="http://rox-xmlrpc.sourceforge.net/niotut/"&gt;
Rox Java NIO Tutorial&lt;/a&gt; James Greenfield
&lt;a href="http://rox-xmlrpc.sourceforge.net/niotut/#General%20principles"&gt;
explicitly recommends&lt;/a&gt; that you
"&lt;i&gt;Use a single selecting thread&lt;/i&gt;" and
"&lt;i&gt;Modify the selector from the selecting thread only.&lt;/i&gt;"
From the description of how &lt;code&gt;register&lt;/code&gt; works above, you can see why.

&lt;br/&gt;&lt;br/&gt;
To get around this problem and ensure that all changes to the selection keys
happen on the thread that is calling select,
we modify &lt;code&gt;NioSelect.register&lt;/code&gt; so that,
rather than calling &lt;code&gt;SelectableChannel.register&lt;/code&gt; directly,
it packages the arguments up and puts them into a queue which is
processed by the selection thread
in order to make all of the calls to &lt;code&gt;SelectableChannel.register&lt;/code&gt;
just before it calls &lt;code&gt;select&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Fortunately, the semantics of the &lt;code&gt;wakeup&lt;/code&gt; call ensure that
we won't get ourselves into a position where we have put our registration
request into the queue but the &lt;code&gt;select&lt;/code&gt; call doesn't see it
and blocks on all the other channels.
This is because &lt;code&gt;wakeup&lt;/code&gt; is defined such that a call to it
that happens while the selector is not currently in a select operation
will cause the next &lt;code&gt;select&lt;/code&gt; to wake up immediately.

&lt;br/&gt;&lt;br/&gt;
With this change, all of the key set operations happen on the selection thread
and, since the socket read operation is in a callback that gets executed
by the selection thread in &lt;code&gt;NioSelector.executeCallbacks&lt;/code&gt;,
all socket reads (and likewise accepts) will happen on the
selection thread.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;
&lt;pre name="hlcode" class="scala"
&gt;//In class NioSelector
&lt;b&gt;import scala.collection.mutable.SynchronizedQueue&lt;/b&gt;

    &lt;b&gt;private case class RegistrationRequest(
        channel:SelectableChannel,op:Int,callback:Function0[Unit])
    private val regQ = new SynchronizedQueue[RegistrationRequest]&lt;/b&gt;

    def register(channel:SelectableChannel, op:Int, body: =&amp;gt; Unit) {
        val callback:Function0[Unit] = { () =&amp;gt; { body }}
        &lt;b&gt;regQ.enqueue(RegistrationRequest(channel,op,callback))
        selector.wakeup()&lt;/b&gt;
    }

    def selectOnce(timeout:Long) {
        &lt;b&gt;while (regQ.size&gt;0) {
            val req = regQ.dequeue()
            req.channel.register(selector,req.op,req.callback)
        }&lt;/b&gt;
        ...
    }
}
&lt;/pre&gt;

&lt;a name="coscheduler"&gt;&lt;/a&gt;
&lt;h3&gt;CoScheduler&lt;/h3&gt;

For our coroutine scheduler,
we have to be able to deal with the situation that
we have no coroutines that are currently runnable,
then at some point one of those coroutines becomes runnable
by the actions of another thread.
In the architecture described above, this can happen when new data
that has been read from a connection is placed into the input queue.
To allow us to wait for this kind of event and to be awakened when
it happens, we use Java's
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#wait()"&gt;
&lt;code&gt;wait&lt;/code&gt;/&lt;code&gt;notify&lt;/code&gt;&lt;/a&gt;
model.  We can't override those methods, since &lt;code&gt;notify&lt;/code&gt;
is final, so we define our own versions,
which we call &lt;code&gt;coWait&lt;/code&gt; and &lt;code&gt;coNotify&lt;/code&gt;.
Given those methods, we also extend &lt;code&gt;Runnable&lt;/code&gt;
and replace the old &lt;code&gt;run&lt;/code&gt; method with one
that runs coroutines until none are available to run,
then waits until we are notified and continues the loop.


&lt;pre name="hlcode" class="scala"
&gt;trait CoScheduler &lt;b&gt;extends Runnable&lt;/b&gt; { cosched =&amp;gt;
    //we add the following items
    &lt;b&gt;private val defaultLock = new java.lang.Object
    def coWait():Unit = { defaultLock.synchronized { defaultLock.wait() } }
    def coNotify():Unit = { defaultLock.synchronized { defaultLock.notify } }

    def run {
        while (true) {
            runUntilBlockedOrDone
            coWait
        }
    }&lt;/b&gt;
}
&lt;/pre&gt;

A &lt;code&gt;coNotify&lt;/code&gt; method that accepts as an argument the coroutine
or blocker that has potentially changed state would allow for a more
efficient implementation, but for now we choose the simple implementation
given above that does not attempt that optimization.

&lt;a name="coqueue"&gt;&lt;/a&gt;
&lt;h3&gt;CoQueue&lt;/h3&gt;

We use an instance of &lt;code&gt;CoQueue&lt;/code&gt; as the queue between the
socket read loop and the application processing loop.
The socket read loop calls &lt;code&gt;blockingEnqueue&lt;/code&gt; to place an
item into the queue, and the application processing loop calls
&lt;code&gt;blockingDequeue&lt;/code&gt; to take an element out of the queue.
The result of either of these actions could be to unblock another
coroutine, so we modify those methods to add a call to &lt;code&gt;coNotify&lt;/code&gt;
in case they are being called from a coroutine that is not currently
being managed by our coroutine scheduler.
Since we are calling the enqueue and dequeue methods from different threads,
we use a
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/mutable/SynchronizedQueue.html"&gt;
&lt;code&gt;SynchronizedQueue&lt;/code&gt;&lt;/a&gt;
rather than a plain
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html"&gt;
&lt;code&gt;Queue&lt;/code&gt;&lt;/a&gt;.
Those two methods now look like this:

&lt;pre name="hlcode" class="scala"
&gt;import scala.collection.mutable.&lt;b&gt;Synchronized&lt;/b&gt;Queue

class CoQueue ... extends &lt;b&gt;Synchronized&lt;/b&gt;Queue[A] { ...

    def blockingEnqueue(x:A):Unit @suspendable = {
        enqueueResource.waitUntilNotBlocked
        enqueue(x)
        &lt;b&gt;dequeueResource.coNotify&lt;/b&gt;
    }

    def blockingDequeue():A @suspendable = {
        dequeueResource.waitUntilNotBlocked
        &lt;b&gt;val x =&lt;/b&gt; dequeue
        &lt;b&gt;enqueueResource.coNotify&lt;/b&gt;
        &lt;b&gt;x&lt;/b&gt;
    }
&lt;/pre&gt;

&lt;a name="nioconnection"&gt;&lt;/a&gt;
&lt;h3&gt;NioConnection&lt;/h3&gt;

We add a &lt;code&gt;CoQueue&lt;/code&gt; which we use as our input queue between
the socket reader loop and the application loop.
For this example, we pick an arbitrary limit of 10;
if our application gets behind by more than 10 items,
the socket reader code will suspend when attempting to write to the queue.
If more data arrives while that code is thus suspended,
it will back up in the system's input buffer for that connection,
and eventually the client will get an error when trying to write
to its output connection.

&lt;br/&gt;&lt;br/&gt;
In order to initialize the &lt;code&gt;CoQueue&lt;/code&gt;
we need to pass in a &lt;code&gt;CoScheduler&lt;/code&gt;, so we add that
parameter to our constructor and to the convenience method
in our companion object.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;import net.jimmc.scoroutine.{CoQueue,CoScheduler}&lt;/b&gt;

//In object NioConnection
    def newConnection(&lt;b&gt;sched:CoScheduler,&lt;/b&gt; selector:NioSelector, socket:SocketChannel) {
        val conn = new NioConnection(&lt;b&gt;sched,&lt;/b&gt;selector,socket)
    }

class NioConnection(&lt;b&gt;sched:CoScheduler,&lt;/b&gt; selector:NioSelector, socket:SocketChannel) {
    //Add CoQueue
    &lt;b&gt;private val inQ = new CoQueue[String](sched, 10)&lt;/b&gt;
}
&lt;/pre&gt;

Now that we have a queue, we modify our socket reader code to place our
input data (after conversion to a Java string) into our queue rather than
writing it straight to the output socket.
We want to block when the queue is full, so we call the
&lt;code&gt;blockingEnqueue&lt;/code&gt; method.
Since we now know that's the only action we will be taking,
we fold the &lt;code&gt;readAction&lt;/code&gt; method back into &lt;code&gt;readWhile&lt;/code&gt;.
Because &lt;code&gt;blockingEnqueue&lt;/code&gt; is suspendable,
the &lt;code&gt;else&lt;/code&gt; branch of the
&lt;code&gt;if (count&amp;lt;1)&lt;/code&gt; code block is suspendable, so we need to
make the &lt;code&gt;if&lt;/code&gt; branch suspendable as well.
We do this by adding a &lt;code&gt;shiftUnit&lt;/code&gt; call as the final value
in the &lt;code&gt;if&lt;/code&gt; branch.
The &lt;code&gt;readWhile&lt;/code&gt; method now looks like this:

&lt;pre name="hlcode" class="scala"
&gt;    private def readWait = {
        buffer.clear()
        val count = read(buffer)
        if (count&amp;lt;1) &lt;b&gt;{&lt;/b&gt;
            socket.close()
            &lt;b&gt;shiftUnit[Unit,Unit,Unit]()&lt;/b&gt;
        &lt;b&gt;}&lt;/b&gt; else &lt;b&gt;{&lt;/b&gt;
            &lt;b&gt;//Moved here from readAction&lt;/b&gt;
            buffer.flip()
            lineDecoder.processBytes(buffer, &lt;b&gt;inQ.blockingEnqueue(_)&lt;/b&gt;)
        &lt;b&gt;}&lt;/b&gt;
    }
&lt;/pre&gt;

We now have input data going into our queue, but nobody is
reading it.
For this example, we implement a simple echo loop that reads
from the input queue using a new &lt;code&gt;readLine&lt;/code&gt; method
and writes to the output using our existing &lt;code&gt;writeLine&lt;/code&gt; method.
We do this inside a &lt;code&gt;reset&lt;/code&gt; block so that
it becomes another coroutine that can be managed by our
coroutine scheduler.
Our previous &lt;code&gt;start&lt;/code&gt; method started up the socket reader loop.
We rename that one to &lt;code&gt;startReader&lt;/code&gt;, add a
&lt;code&gt;startApp&lt;/code&gt; method that starts up our echo loop,
and call both of those from a new &lt;code&gt;start&lt;/code&gt; method.
Our &lt;code&gt;start&lt;/code&gt; method now looks like this:

&lt;pre name="hlcode" class="scala"
&gt;//In class NioConnection
    def start():Unit = {
        &lt;b&gt;startReader
        startApp
    }   
            
    private def startApp() {
        reset {
            while (socket.isOpen)
                writeLine(readLine())
        }
    }

    private def startReader() {&lt;/b&gt;
        reset {
            while (socket.isOpen)
                readWait
        }
    }

    &lt;b&gt;def readLine():String @suspendable = inQ.blockingDequeue&lt;/b&gt;
&lt;/pre&gt;

&lt;a name="linedecoder"&gt;&lt;/a&gt;
&lt;h3&gt;LineDecoder&lt;/h3&gt;

Our &lt;code&gt;processBytes&lt;/code&gt; method is now getting passed a callback
that is suspendable, so we need to modify the signature of our
method to accept that.
It passes that callback to &lt;code&gt;processChars&lt;/code&gt;, so that
signature needs to be changed in the same way.
Since &lt;code&gt;processChars&lt;/code&gt; is now calling a suspendable method, 
it too is suspendable, so its return signature
needs to be modified to note that,
and since &lt;code&gt;processBytes&lt;/code&gt; calls &lt;code&gt;processChars&lt;/code&gt;,
it too needs to be modified to have a suspendable return signature.

&lt;pre name="hlcode" class="scala"
&gt;//In class LineDecoder
&lt;b&gt;import scala.util.continuations._&lt;/b&gt;

    def processBytes(b:ByteBuffer,
        lineHandler:(String)=&amp;gt;Unit &lt;b&gt;@suspendable&lt;/b&gt;):Unit &lt;b&gt;@suspendable&lt;/b&gt; = ...

    private def processChars(cb:CharBuffer,
        lineHandler:(String)=&amp;gt;Unit &lt;b&gt;@suspendable&lt;/b&gt;)&lt;b&gt;:Unit @suspendable =&lt;/b&gt; { ... }
&lt;/pre&gt;

&lt;a name="niolistener"&gt;&lt;/a&gt;
&lt;h3&gt;NioListener&lt;/h3&gt;

&lt;code&gt;NioListener&lt;/code&gt; calls &lt;code&gt;NioConnection.newConnection&lt;/code&gt;,
and that call now requires a &lt;code&gt;CoScheduler&lt;/code&gt; argument,
so we add that to our constructor and pass it through when we call
&lt;code&gt;newConnection&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;import net.jimmc.scoroutine.CoScheduler&lt;/b&gt;

class NioListener(&lt;b&gt;sched:CoScheduler,&lt;/b&gt; selector:NioSelector, hostAddr:InetAddress, port:Int) {

    def start(continueListening: =&amp;gt;Boolean):Unit = {
        reset {
            while (continueListening) {
                val socket = accept()
                NioConnection.newConnection(&lt;b&gt;sched,&lt;/b&gt;selector,socket)
            }
        }
    }
}
&lt;/pre&gt;

&lt;a name="nioserver"&gt;&lt;/a&gt;
&lt;h3&gt;NioServer&lt;/h3&gt;

&lt;code&gt;NioServer&lt;/code&gt; instantiates the &lt;code&gt;NioListener&lt;/code&gt;, so
we need to pass it an instance of &lt;code&gt;CoScheduler&lt;/code&gt;.
We create an instance of &lt;code&gt;DefaultCoScheduler&lt;/code&gt; and pass that in.
We now need two threads, one for our coroutine scheduler and one
for the NIO scheduler.
In our &lt;code&gt;start&lt;/code&gt; method,
we create and start a second &lt;code&gt;Thread&lt;/code&gt; for the NIO scheduler,
then rename our own thread and run the coroutine scheduler on it.

&lt;pre name="hlcode" class="scala"
&gt;&lt;b&gt;import net.jimmc.scoroutine.DefaultCoScheduler&lt;/b&gt;

class NioServer(hostAddr:InetAddress, port:Int) {
    val selector = new NioSelector()
    &lt;b&gt;val sched = new DefaultCoScheduler&lt;/b&gt;
    val listener = new NioListener(&lt;b&gt;sched,&lt;/b&gt; selector, hostAddr, port)

    def start() {
        listener.start(true)
        &lt;b&gt;//run the NIO selector on its own thread
        (new Thread(selector,"NioSelector")).start
        Thread.currentThread.setName("CoScheduler")
        sched.run    //run the coroutine scheduler on our thread, renamed&lt;/b&gt;
    }
}
&lt;/pre&gt;

&lt;a name="summary"&gt;&lt;/a&gt;
&lt;h3&gt;Summary&lt;/h3&gt;

As in the previous post, we have once again transformed our example
application in a way which provides an internal improvement - in this
case the ability to use multiple threads - but which
has not changed its basic external behavior:
we still have a simple echo server.
We also have not yet addressed all of the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;Limitations&lt;/a&gt;
from the first post in this series.
Stay tuned for more.

&lt;a name="caveats"&gt;&lt;/a&gt;
&lt;h3&gt;Caveats&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Although I have asserted that it is possible to write a multi-threading
    scheduler to the &lt;code&gt;CoScheduler&lt;/code&gt; API,
    I have not yet actually done this.
    It is possible that this may be more difficult than I expect.
&lt;li&gt;Multi-threaded code is generally tricky stuff.
    I have not spent a lot of time running this example code,
    so it is certainly possible that there are race conditions or other
    concurrency problems.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-2593194217667705639?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/2593194217667705639/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=2593194217667705639' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2593194217667705639'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2593194217667705639'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/04/java-nio-and-scala-coroutines.html' title='Java NIO and Scala Coroutines'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-8733039353792769070</id><published>2011-03-28T15:01:00.000-07:00</published><updated>2011-03-28T15:01:20.877-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Java NIO for Character Decoding in Scala</title><content type='html'>The Java NIO package includes some handy character encoding and
decoding methods that can be used from Scala.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#background"&gt;Background&lt;/a&gt;
&lt;li&gt;&lt;a href="#java-nio-character-coders"&gt;Java NIO Character Coders&lt;/a&gt;
&lt;li&gt;&lt;a href="#linedecoder"&gt;LineDecoder&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioconnection"&gt;NioConnection&lt;/a&gt;
&lt;li&gt;&lt;a href="#limitations"&gt;Limitations&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="background"&gt;&lt;/a&gt;
&lt;h3&gt;Background&lt;/h3&gt;

In my
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html"&gt;
previous post&lt;/a&gt;
I described a simple Scala server using NIO and continuations,
and mentioned in the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#limitations"&gt;
Limitations&lt;/a&gt;
section that the example did not convert the data bytes to characters.
In this post I show how that can easily be added by using another
feature of the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/guide/nio/"&gt;
Java NIO&lt;/a&gt; package:
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/package-summary.html"&gt;
character-set encoders and decoders&lt;/a&gt;.

&lt;a name="java-nio-character-coders"&gt;&lt;/a&gt;
&lt;h3&gt;Java NIO Character Coders&lt;/h3&gt;

The &lt;code&gt;java.nio.charset&lt;/code&gt; package includes a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html"&gt;
&lt;code&gt;Charset&lt;/code&gt;&lt;/a&gt;
class that represents a mapping between the 16-bit Unicode
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Character.html#unicode"&gt;
code-units&lt;/a&gt; that Java uses for its internal representation for
characters and strings,
and a sequence of bytes as are stored in a file or transmitted
through a socket connection.
Each such mapping is represented by a separate instance of the
&lt;code&gt;Charset&lt;/code&gt; class.
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html#iana"&gt;
Standard character mappings&lt;/a&gt; such as "UTF-8" and "ISO-8859-1"
can be retrieved using the static
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html#forName(java.lang.String)"&gt;
&lt;code&gt;forName&lt;/code&gt;&lt;/a&gt;
method.

&lt;br/&gt;&lt;br/&gt;
Given an instance of &lt;code&gt;Charset&lt;/code&gt;,
a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/CharsetEncoder.html"&gt;
&lt;code&gt;CharsetEncoder&lt;/code&gt;&lt;/a&gt;
for that character mapping can
be retrieved by calling the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html#newEncoder()"&gt;
&lt;code&gt;newEncoder&lt;/code&gt;&lt;/a&gt;
method on that instance.
That encoder can then be used to convert a Java string into a sequence
of bytes suitable for writing to a file or connection.

&lt;br/&gt;&lt;br/&gt;
Similarly, the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html#newDecoder()"&gt;
&lt;code&gt;newDecoder&lt;/code&gt;&lt;/a&gt;
method on &lt;code&gt;Charset&lt;/code&gt; retrieves a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/CharsetDecoder.html"&gt;
&lt;code&gt;CharsetDecoder&lt;/code&gt;&lt;/a&gt;
that can be used for the complementary task of
converting bytes from a file or connection into a Java string.

&lt;br/&gt;&lt;br/&gt;
The encoding and decoding methods convert data between a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/CharBuffer.html"&gt;
&lt;code&gt;CharBuffer&lt;/code&gt;&lt;/a&gt;
and a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/ByteBuffer.html"&gt;
&lt;code&gt;ByteBuffer&lt;/code&gt;&lt;/a&gt;.
Since the &lt;code&gt;java.nio&lt;/code&gt; socket I/O calls we are using read and write
their data to and from &lt;code&gt;ByteBuffer&lt;/code&gt;s,
it is convenient for the encoding and decoding to use those objects.

&lt;a name="linedecoder"&gt;&lt;/a&gt;
&lt;h3&gt;LineDecoder&lt;/h3&gt;

Using the &lt;code&gt;java.nio.charset&lt;/code&gt; classes described above,
we write a &lt;code&gt;LineDecoder&lt;/code&gt;
class containing a &lt;code&gt;processBytes&lt;/code&gt; method that takes as input a
&lt;code&gt;ByteBuffer&lt;/code&gt;
(which is what we have to read into when using a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SocketChannel.html"&gt;
&lt;code&gt;SocketChannel&lt;/code&gt;&lt;/a&gt;)
and converts that byte data to Java characters.
For this example, we also break up that character data into separate lines
when we see line break characters,
converting each line of characters to a Java &lt;code&gt;String&lt;/code&gt;.
One buffer of data might contain multiple lines of character data,
so rather than returning a set of lines,
our method accepts a callback to which we pass each line
as we decode it.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;
&lt;pre name="hlcode" class="scala"
&gt;import java.nio.{ByteBuffer,CharBuffer}
import java.nio.charset.{Charset,CharsetDecoder,CharsetEncoder,CoderResult}
import scala.annotation.tailrec

class LineDecoder {

    //Encoders and decoders are not multi-thread safe, so create one
    //for each connection in case we are using multiple threads.
    val utf8Charset = Charset.forName("UTF-8")
    val utf8Encoder = utf8Charset.newEncoder
    val utf8Decoder = utf8Charset.newDecoder

    def processBytes(b:ByteBuffer, lineHandler:(String)=&amp;gt;Unit):Unit =
        processChars(utf8Decoder.decode(b),lineHandler)

    @tailrec
    private def processChars(cb:CharBuffer, lineHandler:(String)=&amp;gt;Unit) {
        val len = lengthOfFirstLine(cb)
        if (len&amp;gt;=0) {
            val ca = new Array[Char](len)
            cb.get(ca,0,len)
            eatLineEnding(cb)
            val line = new String(ca)
            lineHandler(line)
            processChars(cb, lineHandler)       //handle multiple lines
        }
    }

    //Assuming the first character in the buffer is an eol char,
    //consume it and a possible matching CR or LF in case the EOL is 2 chars.
    private def eatLineEnding(cb:CharBuffer) {
        //Eat the first character and see what it is
        cb.get match {
            case '\n' =&amp;gt; if (cb.remaining&amp;gt;0 &amp;amp;&amp;amp; cb.charAt(0)=='\r') cb.get
            case '\r' =&amp;gt; if (cb.remaining&amp;gt;0 &amp;amp;&amp;amp; cb.charAt(0)=='\n') cb.get
            case _ =&amp;gt; //ignore everything else
        }
    }

    private def lengthOfFirstLine(cb:CharBuffer):Int = {
        (0 until cb.remaining) find { i =&amp;gt;
            List('\n','\r').indexOf(cb.charAt(i))&amp;gt;=0 } getOrElse -1
    }
}
&lt;/pre&gt;

Here is an imperative version of &lt;code&gt;lengthOfFirstLine&lt;/code&gt;
that does the same thing as the functional version above.

&lt;pre name="hlcode" class="scala"
&gt;    private def lengthOfFirstLine(cb:CharBuffer):Int = {
        var cbLen = cb.remaining
        for (i &amp;lt;- 0 until cbLen) {
            val ch = cb.charAt(i)
            if (ch == '\n' || ch == '\r')
                return i
        }
        return -1
    }
&lt;/pre&gt;

&lt;a name="nioconnection"&gt;&lt;/a&gt;
&lt;h3&gt;NioConnection&lt;/h3&gt;

One of the classes shown in my previous post was the
&lt;a href="http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html#nioconnection"&gt;
NioConnection&lt;/a&gt;
class,
whose responsibilities include processing input data from the client.
It does this in the method &lt;code&gt;readAction&lt;/code&gt;,
which initially looks like this:

&lt;pre name="hlcode" class="scala"
&gt;//The old version
    private def readAction(b:ByteBuffer) {
        b.flip()
        socket.write(b)
        b.clear()
    }
&lt;/pre&gt;

We replace the direct call to &lt;code&gt;socket.write&lt;/code&gt;
with a call to &lt;code&gt;LineDecoder.processBytes&lt;/code&gt;,
which is responsible for decoding the input data,
and we pass it our new &lt;code&gt;writeLine&lt;/code&gt; method
that accepts a line of characters
and writes it back to the client.
Also, we don't actually need the call to &lt;code&gt;b.clear&lt;/code&gt; here,
which is effectively at the bottom of our &lt;code&gt;readWhile&lt;/code&gt; loop,
since we call that method at the top of the loop.

&lt;pre name="hlcode" class="scala"
&gt;    private val lineDecoder = new LineDecoder

    private def readAction(b:ByteBuffer) {
        b.flip()
        lineDecoder.processBytes(b, writeLine)
    }

    def writeLine(line:String) {
        socket.write(ByteBuffer.wrap((line+"\n").getBytes("UTF-8")))
    }
&lt;/pre&gt;

Now when we receive some input data, it gets passed to
&lt;code&gt;LineDecoder.processBytes&lt;/code&gt;,
which converts it to characters, breaks it up into separate lines,
and calls our &lt;code&gt;writeLine&lt;/code&gt; method for each line.
The &lt;code&gt;writeLine&lt;/code&gt; method uses
&lt;code&gt;String.getBytes&lt;/code&gt;
to convert the characters in the line back to bytes,
wraps those bytes into a &lt;code&gt;ByteBuffer&lt;/code&gt;
and writes them directly to the output channel.

&lt;br/&gt;&lt;br/&gt;
As compared to the example in the previous post, this example should
behave the same externally,
but we are now passing around Java strings rather than NIO buffers,
which, assuming we want to deal with string data rather than binary data,
will make it simpler to write the rest of the real application.

&lt;a name="limitations"&gt;&lt;/a&gt;
&lt;h3&gt;Limitations&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;As with the example in the previous post,
    the current example only shows how to use the NIO calls
    on the read side of the connection.
    We could use a &lt;code&gt;CharsetEncoder&lt;/code&gt; on the write side
    rather than using &lt;code&gt;String.getBytes&lt;/code&gt; and
    &lt;code&gt;ByteBuffer.wrap&lt;/code&gt;.
&lt;li&gt;Partial input lines (characters not terminated by an EOL character)
    are ignored by this implementation.
&lt;li&gt;The example uses the convenience method version of
    &lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/CharsetDecoder.html#decode(java.nio.ByteBuffer)"&gt;
    &lt;code&gt;decode&lt;/code&gt;&lt;/a&gt;,
    which assumes that the input &lt;code&gt;ByteBuffer&lt;/code&gt; contains complete
    character sequences.
    It is possible that a multi-byte character sequence will be
    split such that only the first part of that sequence appears at the
    end of the input buffer,
    with the remainder of the sequence appearing at the start of the next
    buffer of input data.
    The above implementation will not properly handle this situation.
    The underlying
    &lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/CharsetDecoder.html#decode(java.nio.ByteBuffer,%20java.nio.CharBuffer,%20boolean)"&gt;
    &lt;code&gt;decode&lt;/code&gt;&lt;/a&gt; method does handle this situation properly,
    but the remaining code in this example is not set up for this situation.
&lt;li&gt;The &lt;code&gt;decode&lt;/code&gt;
    convenience method throws exceptions rather than returning
    a status code as the full &lt;code&gt;decode&lt;/code&gt; method does.
    Since these exceptions are nowhere caught in the code, such an
    exception would cause that task to abort.
    A more robust solution would have a mechanism to catch exceptions or
    restart an aborted task.
&lt;li&gt;The example assumes UTF-8 encoding.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-8733039353792769070?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/8733039353792769070/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=8733039353792769070' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8733039353792769070'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8733039353792769070'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/03/java-nio-for-character-decoding-in.html' title='Java NIO for Character Decoding in Scala'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4016495859623603527</id><published>2011-03-22T13:30:00.000-07:00</published><updated>2011-03-22T13:30:13.990-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Java NIO and Scala Continuations</title><content type='html'>Combining Java's NIO non-blocking input/output
with Scala's delimited continuations
allows making a multi-client stateful server that does not require a
a dedicated thread for every active client.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#motivation"&gt;Motivation&lt;/a&gt;
&lt;li&gt;&lt;a href="#java-nonblocking-io"&gt;Java Nonblocking IO&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioselector"&gt;NioSelector&lt;/a&gt;
&lt;li&gt;&lt;a href="#niolistener"&gt;NioListener&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioconnection"&gt;NioConnection&lt;/a&gt;
&lt;li&gt;&lt;a href="#nioserver"&gt;NioServer&lt;/a&gt;
&lt;li&gt;&lt;a href="#limitations"&gt;Limitations&lt;/a&gt;
&lt;li&gt;&lt;a href="#resources"&gt;Resources&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="motivation"&gt;&lt;/a&gt;
&lt;h3&gt;Motivation&lt;/h3&gt;

When writing a high-volume server, you want it to be able to handle
a large number of clients in a timely manner.
You want the server to be able to respond quickly to each request
even when there are a lot of clients.
In particular, when you have a large number of clients for whom you
are storing state but are not at this moment making a request,
you want those clients to have a negligible effect on your response time,
and when you have other clients currently making requests that may take
some time to execute, you don't want those requests significantly
increasing your response time to other requests.

&lt;br/&gt;&lt;br/&gt;
From the above, we thus have these requirements:
&lt;ul&gt;
&lt;li&gt;The size of the state stored for each active client should be minimized.
&lt;li&gt;The time to notice that a client has made a request should not
    depend significantly on the number of active clients not making a request.
&lt;li&gt;A client request should never wait on a request from another client.
&lt;/ul&gt;

A simple Java implementation of a server might store its per-client
state in a thread for each
active client, and block waiting for a request on each thread.
Java's thread scheduler then takes care of noticing when input is
is available from one of those blocking reads
and continuing execution on the associated thread.
However, threads are relatively expensive and consume more memory than
necessary.

&lt;br/&gt;&lt;br/&gt;
An implementation that stores the state for each client manually
could store that state much more compactly and thus handle many more clients,
but in addition to being more complicated to implement,
the application must now include a mechanism to multiplex multiple
clients onto fewer threads than there are active clients
without having any clients blocked by other clients.

&lt;br/&gt;&lt;br/&gt;
Scala's
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
delimited continuations&lt;/a&gt; give us a another option for storing
the state of a client that is simpler to implement than manual state
storage and that uses far less memory than using a thread for each client
(see slide 19 of Phillip Haller's
&lt;a href="http://lamp.epfl.ch/~phaller/doc/ScalaActors.pdf"&gt;presentation&lt;/a&gt;
on Actors, where he says an app can handle up to 5,000
threads, but up to 1,200,000 actors using closures).

&lt;br/&gt;&lt;br/&gt;
That leaves us to solve the issue of how to multiplex many clients
without having them block each other.
If using the standard
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/io/package-summary.html"&gt;java.io&lt;/a&gt;
package, the only option for reading
from multiple streams without blocking is to poll using the
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/io/InputStream.html#available()"&gt;
&lt;code&gt;available&lt;/code&gt;&lt;/a&gt; method.
Besides the wasted CPU time,
as the number of active clients grows the polling loop takes longer
to run, meaning additional latency before the application notices
that any particular
stream is ready for reading and thus increased response time.
The Java NIO package provides an efficient solution for this problem.

&lt;br/&gt;&lt;br/&gt;
In the "Nio*" sections below
I present a few Scala classes that break up the NIO work into
chunks that I hope are easy to understand.
The last piece is a simple main class to test it all.

&lt;a name="java-nonblocking-io"&gt;&lt;/a&gt;
&lt;h3&gt;Java Nonblocking IO&lt;/h3&gt;

Starting with version 1.4,
Java has a package called NIO (new input/output) that includes
the ability to do non-blocking input and output.

&lt;br/&gt;&lt;br/&gt;
The non-blocking IO model in
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/guide/nio/"&gt;Java NIO&lt;/a&gt;
is based on
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Channel.html"&gt;&lt;code&gt;Channel&lt;/code&gt;&lt;/a&gt;s
and
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html"&gt;&lt;code&gt;Selector&lt;/code&gt;&lt;/a&gt;s,
similar to
&lt;a href="http://en.wikipedia.org/wiki/File_descriptor"&gt;file descriptors&lt;/a&gt;
and the
&lt;a href="http://linux.die.net/man/2/select"&gt;select&lt;/a&gt;
call in POSIX operating systems.
A &lt;code&gt;Channel&lt;/code&gt; represents a connection to a file, socket,
or some other data source or sink.
A &lt;code&gt;Selector&lt;/code&gt; allows collecting a set of &lt;code&gt;Channel&lt;/code&gt;s
together and waiting until any of them are ready for input or output.

&lt;br/&gt;&lt;br/&gt;
In order to use a &lt;code&gt;Channel&lt;/code&gt; with a &lt;code&gt;Selector&lt;/code&gt;,
you must
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html#register%28java.nio.channels.Selector,%20int,%20java.lang.Object%29"&gt;
&lt;code&gt;register&lt;/code&gt;&lt;/a&gt;
the &lt;code&gt;Channel&lt;/code&gt; with the &lt;code&gt;Selector&lt;/code&gt;
(and the &lt;code&gt;Channel&lt;/code&gt; must be a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectableChannel.html"&gt;
&lt;code&gt;SelectableChannel&lt;/code&gt;&lt;/a&gt;).
When you register a channel you also specify which
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectionKey.html#field_summary"&gt;
operations&lt;/a&gt; are of interest, such as READ or WRITE.
These operations are represented by bits that can be combined by
ORing them together to create a value that indicates interest
in multiple operations.

&lt;br/&gt;&lt;br/&gt;
Registering a channel with a selector creates a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/SelectionKey.html"&gt;
&lt;code&gt;SelectionKey&lt;/code&gt;&lt;/a&gt;.
When you later execute the select method on the selector,
it returns a set of &lt;code&gt;SelectionKey&lt;/code&gt;s,
from which you can extract the information passed to the register call
when the &lt;code&gt;SelectionKey&lt;/code&gt; was created.

&lt;a name="nioselector"&gt;&lt;/a&gt;
&lt;h3&gt;NioSelector&lt;/h3&gt;

The &lt;code&gt;register&lt;/code&gt; call to
&lt;code&gt;java.nio.channels.SelectableChannel&lt;/code&gt;
includes an "attachment" parameter that
allows the application to attach an arbitrary object to that registration
and retrieve it later when the associated channel becomes selected.
In our server, we attach a callback function to be executed
when the channel becomes selected.
We package this up in our own &lt;code&gt;register&lt;/code&gt; method
in our &lt;code&gt;NioSelector&lt;/code&gt; class.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;import java.nio.channels.SelectableChannel
import java.nio.channels.spi.SelectorProvider

/** Handle nio channel selection. */
class NioSelector {

    val selector = SelectorProvider.provider.openSelector()

    def register(channel:SelectableChannel, op:Int, body: =&amp;gt; Unit) {
        val callback:Function0[Unit] = { () =&amp;gt; { body }}
        channel.register(selector, op, callback)
    }
}
&lt;/pre&gt;

We add a &lt;code&gt;selectOnce&lt;/code&gt; method that waits for one or
more of the registered channels to be selected,
or until the specified time has elapsed if no channels
are selected during that time.
The underlying call to
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/Selector.html#selectedKeys()"&gt;
&lt;code&gt;selectedKeys&lt;/code&gt;&lt;/a&gt;
in &lt;code&gt;java.nio.channels.Selector&lt;/code&gt; returns to us a
&lt;code&gt;java.util.Set&lt;/code&gt; containing the information about
which channels are selected.
Before we process each channel,
we need to clear the current state so that we are able to re-register
interest in that channel during that processing.
We make a copy of the state for all selected channels
and then clear that state before processing any channels.

&lt;pre name="hlcode" class="scala"
&gt;import scala.collection.JavaConversions
import java.nio.channels.SelectionKey

//In class NioSelector

    def selectOnce(timeout:Long) {
        selector.select(timeout)
        val jKeys:java.util.Set[SelectionKey] = selector.selectedKeys
        val keys = JavaConversions.asScalaSet(jKeys).toList
        selector.selectedKeys.clear()
        keys foreach { _.interestOps(0) }
        val callbacks = keys map { _.attachment.asInstanceOf[()=&amp;gt;Unit] }
        executeCallbacks(callbacks) //Execute callbacks for all selected keys
    }
&lt;/pre&gt;

The &lt;code&gt;executeCallbacks&lt;/code&gt; method is responsible for executing
the callbacks for the selected channels.
If we assume that at least some of these callbacks may take a relatively
long time to complete, then
in a real server we might want to throw all of these callbacks into
a thread pool or farm them out to actors
so that we can avoid having one long-running callback
block out the rest of the channels.
For now, though, we will ignore that issue and use the simple
implementation of just executing each callback sequentially in
the same thread.

&lt;pre name="hlcode" class="scala"
&gt;//In class NioSelector

    def executeCallbacks(callbacks:List[()=&amp;gt;Unit]) {
        callbacks foreach { _() }
    }
&lt;/pre&gt;

For convenience, we add a &lt;code&gt;selectLoop&lt;/code&gt; method that will
continue to call &lt;code&gt;selectOnce&lt;/code&gt; until we are ready to stop
processing, and we make our class implement the &lt;code&gt;Runnable&lt;/code&gt;
interface.

&lt;pre name="hlcode" class="scala"
&gt;//In NioSelector class

    def run() {
        selectLoop(true)
    }

    def selectLoop(continueProcessing: =&amp;gt; Boolean) {
        while (continueProcessing) {
            selectOnce(0)
        }
    }
&lt;/pre&gt;

Here is the complete &lt;code&gt;NioSelector&lt;/code&gt; class:

&lt;pre name="hlcode" class="scala"
&gt;import scala.collection.JavaConversions
import java.nio.channels.SelectableChannel
import java.nio.channels.SelectionKey
import java.nio.channels.spi.SelectorProvider

/** Handle nio channel selection. */
class NioSelector extends Runnable {

    val selector = SelectorProvider.provider.openSelector()

    def register(channel:SelectableChannel, op:Int, body: =&amp;gt; Unit) {
        val callback:Function0[Unit] = { () =&amp;gt; { body }}
        channel.register(selector, op, callback)
    }

    def run() {
        selectLoop(true)
    }

    def selectLoop(continueProcessing: =&amp;gt; Boolean) {
        while (continueProcessing) {
            selectOnce(0)
        }
    }

    def selectOnce(timeout:Long) {
        selector.select(timeout)
        val jKeys:java.util.Set[SelectionKey] = selector.selectedKeys
        val keys = JavaConversions.asScalaSet(jKeys).toList
        selector.selectedKeys.clear()
        keys foreach { _.interestOps(0) }
        val callbacks = keys map { _.attachment.asInstanceOf[()=&amp;gt;Unit] }
        executeCallbacks(callbacks) //Execute callbacks for all selected keys
    }

    def executeCallbacks(callbacks:List[()=&amp;gt;Unit]) {
        callbacks foreach { _() }
    }
}
&lt;/pre&gt;

Our main method will create an instance of &lt;code&gt;NioSelector&lt;/code&gt;,
do some other initialization, and eventually call
&lt;code&gt;NioSelector.run&lt;/code&gt;.

&lt;a name="niolistener"&gt;&lt;/a&gt;
&lt;h3&gt;NioListener&lt;/h3&gt;

The above &lt;code&gt;NioSelector&lt;/code&gt; class gives us our main event loop,
but how do we get things hooked up to start generating events?
The basic socket flow on the server side for reading data follows these steps:
&lt;ul&gt;
&lt;li&gt;Listen for new connections on a socket.
&lt;li&gt;When a new connection is made, initialize that connection and wait
    for input data on that connection.
&lt;li&gt;When input data is received, process that input data.
&lt;li&gt;Eventually, close the connection.
&lt;/ul&gt;

Our &lt;code&gt;NioListener&lt;/code&gt; class takes care of creating the socket on
which we listen, accepting new connections, and passing them off to
another class for per-connection initialization.

&lt;br/&gt;&lt;br/&gt;
We start with the code that creates the listener socket.
Since we will be passing this socket to our selector, we must create a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/ServerSocketChannel.html"&gt;
&lt;code&gt;ServerSocketChannel&lt;/code&gt;&lt;/a&gt;
rather than a regular socket.
We don't want to block waiting for this socket, so we call
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/channels/spi/AbstractSelectableChannel.html#configureBlocking%28boolean%29"&gt;
&lt;code&gt;configureBlocking(false)&lt;/code&gt;&lt;/a&gt;.

&lt;pre name="hlcode" class="scala"
&gt;import java.net.{InetAddress,InetSocketAddress}
import java.nio.channels.ServerSocketChannel

class NioListener(selector:NioSelector, hostAddr:InetAddress, port:Int) {

    val serverChannel = ServerSocketChannel.open()
    serverChannel.configureBlocking(false);
    val isa = new InetSocketAddress(hostAddr,port)
    serverChannel.socket.bind(isa)
}
&lt;/pre&gt;

Now that we have created our socket, we must register it with our selector.
In that call, we will tell the selector that we are interested in the
ACCEPT operation on our socket channel.
The third argument to our register method is the callback;
this is where continuations come in.

&lt;br/&gt;&lt;br/&gt;
Blocking code is generally simpler and thus easier to write and understand than
asynchronous (non-blocking) code.
By using continuations we can capture the current state of the code,
explicitly save it for later execution, and continue running the thread
at a different point.
The code within our delimited continuation is blocked, but the thread does
not get blocked.

&lt;br/&gt;&lt;br/&gt;
Rather than explicitly creating and passing in a callback
with application state,
we arrange
things such that the callback passed to our register method
includes a call to our continuation, so that when that callback is called,
it executes our continuation and makes our code continue from where we
left off.

&lt;br/&gt;&lt;br/&gt;
We implement our own &lt;code&gt;accept&lt;/code&gt; method that looks to the calling
code like the blocking
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/net/ServerSocket.html#accept%28%29"&gt;
 &lt;code&gt;ServerSocket.accept&lt;/code&gt;&lt;/a&gt; method,
but that uses continuations as described above to do the blocking.
We then call our &lt;code&gt;accept&lt;/code&gt; method from within a &lt;code&gt;reset&lt;/code&gt;
block that delimits the continuation and determines how much code is
saved in the continuation (everything within the &lt;code&gt;reset&lt;/code&gt;)
and the point at which execution continues (just past the &lt;code&gt;reset&lt;/code&gt;)
in the thread when the &lt;code&gt;accept&lt;/code&gt; call blocks.

&lt;pre name="hlcode" class="scala"
&gt;import java.nio.channels.SelectionKey
import java.nio.channels.SocketChannel
import scala.util.continuations._

//In NioListener class
    def start(continueListening: =&amp;gt;Boolean):Unit = {
        reset {
            while (continueListening) {
                val socket = accept()
                NioConnection.newConnection(selector,socket)
            }
        }
    }

    private def accept():SocketChannel @suspendable = {
        shift { k =&amp;gt;
            selector.register(serverChannel,SelectionKey.OP_ACCEPT, {
                val conn = serverChannel.accept()
                conn.configureBlocking(false)
                k(conn)
            })
        }
    }
&lt;/pre&gt;

When we call our &lt;code&gt;start&lt;/code&gt; method, it enters the
&lt;code&gt;reset&lt;/code&gt; block and calls &lt;code&gt;accept&lt;/code&gt;, which
registers our interest in the ACCEPT operation,
saves the continuation as part of our callback
in the &lt;code&gt;register&lt;/code&gt; method
and immediately returns control to the end of the
&lt;code&gt;reset&lt;/code&gt; block, thus the &lt;code&gt;start&lt;/code&gt; method returns quickly.
When a new connection arrives, the select code in &lt;code&gt;NioSelector&lt;/code&gt;
calls our callback from its &lt;code&gt;executeCallbacks&lt;/code&gt; method.
Our callback accepts the new connection and sets it to non-blocking,
then executes our continuation, which will "return" from
our blocked &lt;code&gt;accept&lt;/code&gt; method into the loop in our &lt;code&gt;start&lt;/code&gt;
method. The code processes the new connection, then loops and calls
&lt;code&gt;accept&lt;/code&gt; again, which reregisters interest in an ACCEPT
operation on our socket and
returns execution to the caller of the continuation, which returns control to
the &lt;code&gt;executeCallbacks&lt;/code&gt; method in &lt;code&gt;NioSelector&lt;/code&gt;.

&lt;a name="nioconnection"&gt;&lt;/a&gt;
&lt;h3&gt;NioConnection&lt;/h3&gt;

Our &lt;code&gt;NioConnection&lt;/code&gt; class is responsible for creating our
connection object, registering it with the selector,
reading input data when available and processing that data.
The processing that we do in this example is to echo the data back to
the client.
In order to keep this example simple,
we are not using NIO and continuations on the writing side,
rather we use a simple &lt;code&gt;write&lt;/code&gt;
call to write our data directly to the socket.

&lt;br/&gt;&lt;br/&gt;
As with &lt;code&gt;NioListener&lt;/code&gt;,
we create our own &lt;code&gt;read&lt;/code&gt; call that uses continuations to
block, but otherwise looks similar to the standard Java
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/io/InputStream.html#read(byte[])"&gt;
&lt;code&gt;InputStream.read(byte[])&lt;/code&gt;&lt;/a&gt; call,
except that we read into a
&lt;a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/ByteBuffer.html"&gt;
&lt;code&gt;ByteBuffer&lt;/code&gt;&lt;/a&gt;
rather than a byte array.
As with &lt;code&gt;NioListener&lt;/code&gt;,
our &lt;code&gt;start&lt;/code&gt; method that calls our &lt;code&gt;read&lt;/code&gt; method
(via our intermediate method &lt;code&gt;readWait&lt;/code&gt;)
contains a &lt;code&gt;reset&lt;/code&gt; block that delimits the continuation.

&lt;pre name="hlcode" class="scala"
&gt;import java.nio.ByteBuffer
import java.nio.channels.SelectionKey
import java.nio.channels.SocketChannel
import scala.util.continuations._

object NioConnection {
    def newConnection(selector:NioSelector, socket:SocketChannel) {
        val conn = new NioConnection(selector,socket)
        conn.start()
    }
}

class NioConnection(selector:NioSelector, socket:SocketChannel) {

    private val buffer = ByteBuffer.allocateDirect(2000)

    def start():Unit = {
        reset {
            while (socket.isOpen)
                readWait
        }
    }

    private def readWait = {
        buffer.clear()
        val count = read(buffer)
        if (count&amp;lt;1)
            socket.close()
        else
            readAction(buffer)
    }

    private def read(b:ByteBuffer):Int @suspendable = {
        if (!socket.isOpen)
            -1  //indicate EOF
        else shift { k =&amp;gt;
            selector.register(socket, SelectionKey.OP_READ, {
                val n = socket.read(b)
                k(n)
            })
        }
    }

    private def readAction(b:ByteBuffer) {
        b.flip()
        socket.write(b)
        b.clear()
    }
}
&lt;/pre&gt;

The program flow is similar to the flow in &lt;code&gt;NioListener&lt;/code&gt;
When we call our &lt;code&gt;start&lt;/code&gt; method, it enters the &lt;code&gt;reset&lt;/code&gt;
block and (through the call to &lt;code&gt;readWait&lt;/code&gt;) calls &lt;code&gt;read&lt;/code&gt;,
which registers our interest in the READ operation,
saves the continuation as part of our callback
in the &lt;code&gt;register&lt;/code&gt; method
and immediately returns control to the end of the
&lt;code&gt;reset&lt;/code&gt; block, thus the &lt;code&gt;start&lt;/code&gt; method returns quickly.
When data is available to be read from the socket,
the select code in &lt;code&gt;NioSelector&lt;/code&gt;
calls our callback from its &lt;code&gt;executeCallbacks&lt;/code&gt; method.
Our callback reads the data from the connection
then executes our continuation, which will "return" from
our blocked &lt;code&gt;read&lt;/code&gt; method into the &lt;code&gt;readWait&lt;/code&gt;
method. The code processes the input data in &lt;code&gt;readAction&lt;/code&gt;
(which just echos the data back to the client),
then (if the socket is still open)
loops and ends up calling
&lt;code&gt;read&lt;/code&gt; again, which reregisters interest in a READ
operation on our socket and
returns execution to the caller of the continuation,
which returns control to
the &lt;code&gt;executeCallbacks&lt;/code&gt; method in &lt;code&gt;NioSelector&lt;/code&gt;.

&lt;a name="nioserver"&gt;&lt;/a&gt;
&lt;h3&gt;NioServer&lt;/h3&gt;

Here is a short main program to test the above modules.

&lt;pre name="hlcode" class="scala"
&gt;import java.net.InetAddress

object NioServer {
    def main(args:Array[String]) {
        val hostAddr:InetAddress = null //listen on local connection
        val port = 1234
        val server = new NioServer(hostAddr,port)
        server.start()
    }
}

class NioServer(hostAddr:InetAddress, port:Int) {
    val selector = new NioSelector()
    val listener = new NioListener(selector, hostAddr, port)

    def start() {
        listener.start(true)
        selector.run()
    }
}
&lt;/pre&gt;

Create a directory and place into that directory the above four files
(&lt;code&gt;NioSelector.scala&lt;/code&gt;, &lt;code&gt;NioListener.scala&lt;/code&gt;,
&lt;code&gt;NioConnection.scala&lt;/code&gt; and &lt;code&gt;NioServer.scala&lt;/code&gt;),
&lt;code&gt;cd&lt;/code&gt; into that directory, then execute the following commands to
compile the files and execute the test program:

&lt;pre name="hlcode" class="bash"
&gt;scalac -P:continuations:enable *.scala
scala NioServer
&lt;/pre&gt;

If you get an "Address already in use" error and traceback,
edit &lt;code&gt;NioServer.scala&lt;/code&gt; and pick another port number.

&lt;br/&gt;&lt;br/&gt;
After starting the server, connect to it using telnet:
&lt;pre name="hlcode" class="bash"
&gt;telnet localhost 1234
&lt;/pre&gt;

In the telnet client, each time you enter a line of text
the server should echo that line of text back to you.

&lt;a name="limitations"&gt;&lt;/a&gt;
&lt;h3&gt;Limitations&lt;/h3&gt;

The above example demonstrates a very basic echo server
in a bit over 100 lines of code
that uses NIO and continuations for reading data,
but it has some limitations:
&lt;ul&gt;
&lt;li&gt;The processing of the input data is buried in the code that reads the data.
    A well-factored architecture would allow that processing code to be
    separately maintained.
&lt;li&gt;We are using one thread for all execution.
    In the above example we are just echoing clients' input back to them,
    but if we want to do some heavier processing we will need to move that
    work off of the select thread onto one or more worker threads
    so as not to block everyone while handling a request for one client.
&lt;li&gt;Communication between the select thread and the worker threads will
    require some kind of buffering, which introduces more points at which
    execution can be blocked, thus more opportunities to use continuations.
&lt;li&gt;If we wish to treat the data as characters rather than bytes then we
    need to do that conversion.
&lt;li&gt;We are not using IO and continuations when writing data to the client,
    so if for some reason our socket is not ready for writing
    the application will not work properly
    (either it will block waiting for output,
    or we will lose some output data).
&lt;li&gt;We are not storing any significant amount of state in our continuations.
    If all we are doing is a simple echo server, we don't really need
    continuations.
    A more sophisticated example would better illustrate the convenience
    of using continuations to store program state.
&lt;/ul&gt;

I hope to address these points in future posts.

&lt;a name="resources"&gt;&lt;/a&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;My explanation of
    &lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
    delimited continuations&lt;/a&gt;.
&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/New_I/O"&gt;Wikipedia&lt;/a&gt;
    article on Java NIO.
&lt;li&gt;&lt;a href="http://rox-xmlrpc.sourceforge.net/niotut/"&gt;
    Rox Java NIO Tutorial&lt;/a&gt;,
    in which James Greenfield builds a server in Java using NIO
    and gives a number of tips and warnings.
&lt;li&gt;&lt;a href="http://tutorials.jenkov.com/java-nio/index.html"&gt;
    Jenkov's Java NIO Tutorial&lt;/a&gt;
    goes over each part of the entire NIO package in detail.
&lt;li&gt;&lt;a href="http://mina.apache.org/"&gt;MINA&lt;/a&gt;,
    Apache's network application framework that uses NIO.
&lt;li&gt;&lt;a href="https://github.com/rschildmeijer/loft"&gt;Loft&lt;/a&gt;,
    a single-threaded web server in Scala using NIO and continuations
    (under development).
&lt;li&gt;A &lt;a href="http://www.scala-lang.org/node/2096#comment-7674"&gt;post&lt;/a&gt;
    from July 2009
    on the Scala mailing list with an (uncompiled) example of a simple
    single-threaded server using NIO.
    The post demonstrates the approach of creating versions of the standard
    calls (accept, read, write) that block using continuations.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4016495859623603527?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4016495859623603527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4016495859623603527' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4016495859623603527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4016495859623603527'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html' title='Java NIO and Scala Continuations'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-2350204372172685894</id><published>2010-12-22T06:27:00.000-08:00</published><updated>2010-12-22T23:18:32.973-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala Pros and Cons</title><content type='html'>Some pros and cons of the Scala
programming language as compared to Java
from a business management perspective.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#introduction"&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#pros"&gt;Pros&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#higher-productivity"&gt;Higher Productivity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#higher-quality"&gt;Higher Quality&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#better-developers-pro"&gt;Better Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#rapidly-improving-ecosystem"&gt;Rapidly Improving Ecosystem&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;a href="#cons"&gt;Cons&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#learning-curve"&gt;Learning Curve&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#limited-developers"&gt;Limited Developer Pool&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#better-developers-con"&gt;Better Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#limited-support"&gt;Limited Commercial Support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#tool-immaturity"&gt;Tool Immaturity&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;a href="#former-cons"&gt;Former Cons&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#risk-breaking"&gt;Risk of Breaking Changes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#risk-abandonment"&gt;Risk of Abandonment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#limited-documentation"&gt;Limited Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;a href="#references"&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#web-sites"&gt;Web Sites Using Scala&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#other-posts"&gt;Other Posts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;

&lt;a name="introduction"&gt;&lt;/a&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;

I have been promoting
&lt;a href="http://www.scala-lang.org"&gt;Scala&lt;/a&gt; for quite some time now,
and I have encouraged all of the developers in my office to try it out,
hoping that at some point we can start using it for new projects.

&lt;br/&gt;&lt;br/&gt;
It was suggested to me that I put together a list of the pros and cons
of Scala to help other people in the company evaluate whether they should
consider using it for their projects.
After pondering this I realized that this would likely be of interest
to a wider audience than just at my company,
and that it would make a good topic for my blog.
Thus this post in which I list what I believe to be the most important
factors that a company that wants to stay on the JVM platform
should consider when
deciding whether or not to move from
&lt;a href="http://www.java.com"&gt;Java&lt;/a&gt; to Scala.

&lt;br/&gt;&lt;br/&gt;
Because this comparison is specifically Scala versus Java,
I do not include the fact that Scala runs on the JVM,
has performance on par with Java,
interoperates easily
with Java and after compiling is essentially indistinguihsable from Java.
While these might be benefits for some companies
when comparing Scala against other language such as
&lt;a href="http://www.ruby-lang.org"&gt;Ruby&lt;/a&gt; or
&lt;a href="http://www.python.org"&gt;Python&lt;/a&gt;
or other JVM languages,
they do not distinguish Scala from Java.

&lt;br/&gt;&lt;br/&gt;
The intended audience for this post is not the developers who might be
using Scala, but the technical managers who are involved in
the decision about what technology to use.
I don't discuss Scala's cool features such as closures and
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
continuations&lt;/a&gt;,
easy interoperation with legacy Java code,
&lt;a href="http://jim-mcbeath.blogspot.com/2008/08/scala-traits.html"&gt;traits&lt;/a&gt;
with code,
or a sophisticated type system with type inference,
but focus instead on the business-level
questions of how using Scala rather than Java
might affect the success of a project, a team, or the company.

&lt;br/&gt;&lt;br/&gt;
My company has not yet produced a product using Scala, so my experience is
limited to some prototypes at the office and
&lt;a href="http://alumnus.caltech.edu/~jimmc/stringart/"&gt;some&lt;/a&gt;
&lt;a href="https://github.com/jimmc/mimprint"&gt;personal&lt;/a&gt;
&lt;a href="https://github.com/jimmc/scoroutine"&gt;work&lt;/a&gt;
at home.
The points below are based on that experience plus my interpretation
of comments from others that I have read in the last couple of years.
Everyone's viewpoint differs, so you may disagree with my statements
or my evaluation of their importance.
Similarly, the specifics of your situation may make your experience
with Scala significantly different than mine.
It is easy to find both praise and dismissal of Scala on the 'net.
As they say,
&lt;a href="http://books.google.com/books?id=Qt-bRFetWw0C&amp;pg=PA282"&gt;YMMV&lt;/a&gt;.

&lt;a name="pros"&gt;&lt;/a&gt;
&lt;h3&gt;Pros&lt;/h3&gt;

&lt;a name="higher-productivity"&gt;&lt;/a&gt;
&lt;h4&gt;Higher Productivity&lt;/h4&gt;

&lt;a href="http://www.scala-lang.org/node/3069"&gt;Studies&lt;/a&gt;
and anecdotal evidence indicate that Scala programs are from
&lt;a href="http://archive.fosdem.org/2009/interview/martin+odersky"&gt;1/2&lt;/a&gt;
to
&lt;a href="http://stackoverflow.com/questions/2952732/samples-of-scala-and-java-code-where-scala-code-looks-simpler-has-fewer-lines/2953040#2953040"&gt;
1/10&lt;/a&gt;
the number of lines of code as compared to
a functionally equivalent Java program
The larger the application, the more apparent this difference becomes.
If you believe, as I do, that a given programmer can produce roughly the
same number of lines of code per day independent of the language used,
you can see how this reduction of lines of code can translate into a
substantial increase in productivity
and a faster time-to-market.

&lt;a name="higher-quality"&gt;&lt;/a&gt;
&lt;h4&gt;Higher Quality&lt;/h4&gt;

Scala encourages a functional programming style, which in turn
leads to code with fewer bugs.
Also, fewer lines of code means fewer bugs.
Fewer bugs means higher productivity and a higher quality product.

&lt;br/&gt;&lt;br/&gt;
Scala's
&lt;a href="http://www.scala-lang.org/node/242"&gt;
Actor&lt;/a&gt; library and its encouragement of immutable variables
help to avoid race conditions among multiple threads and thus
improve reliability in concurrent applications.

&lt;a name="better-developers-pro"&gt;&lt;/a&gt;
&lt;h4&gt;Better Developers&lt;/h4&gt;

Programmers who learn Scala pick up programming practices such
as functional programming that they can (at least to some extent)
apply to other languages, such as Java.
This makes them
&lt;a href="http://jim-mcbeath.blogspot.com/2009/01/you-should-learn-scala.html"&gt;
better Java programmers&lt;/a&gt;.

Providing an environment in which developers can learn something new
should also make for a more positive environment for those developers,
so they will be happier with their jobs and less likely to leave the company.
Likewise, a shop that uses a new and advanced language such as Scala
is more likely to attract top-notch developers
(which Paul Graham has named as
&lt;a href="http://www.paulgraham.com/pypar.html"&gt;"The Python Paradox"&lt;/a&gt;).

&lt;a name="rapidly-improving-ecosystem"&gt;&lt;/a&gt;
&lt;h4&gt;Rapidly Improving Ecosystem&lt;/h4&gt;

The early adopters of Scala such as Twitter are well down that road.
Here at the end of 2010
it is tempting to say that the early adopter window is closed
and anyone who starts with Scala in 2011 or later is no longer in
that category.

&lt;br/&gt;&lt;br/&gt;
Some of the &lt;a href="#cons"&gt;cons&lt;/a&gt; listed below may never change,
or change very slowly,
but at least the tools issue will certainly improve.
The rate of improvement of the Scala ecosystem is much faster than
most other languages at the moment.
The IDE tools are improving, the testing tools are improving, the
documentation is improving, and the functionality of available libraries
is improving.
All of these improvements will continue to make the Scala ecosystem
more attractive over time.
If you evaluate the current situation and find it close to satisfactory,
and the problem areas are the ones that will improve with time,
then you probably won't have to wait very long for that to happen.

&lt;a name="cons"&gt;&lt;/a&gt;
&lt;h3&gt;Cons&lt;/h3&gt;

&lt;a name="learning-curve"&gt;&lt;/a&gt;
&lt;h4&gt;Learning Curve&lt;/h4&gt;

Scala incorporates features and concepts that are not familiar to
many programmers, such as functional programming and continuations.
It can take some time to learn these concepts.

Some people say the Scala syntax is more complicated than Java,
but in fact the
&lt;a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"&gt;
language spec&lt;/a&gt;
for Scala is significantly smaller (191 pages)
than the
&lt;a href="http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf"&gt;
language spec&lt;/a&gt; for Java (684 pages).
The difference is that Scala is much more regular and allows things
to be combined in ways that can't be done in Java, which provides
more power and expressiveness but also can take some time to learn.

&lt;br/&gt;&lt;br/&gt;
I have seen comments to the effect that it could take about two months
for a Java developer to come up to speed on Scala,
although a developer who already has experience
using other languages with functional constructs
such as Python or Ruby should be able to
pick it up faster.

&lt;a name="limited-developers"&gt;&lt;/a&gt;
&lt;h4&gt;Limited Developer Pool&lt;/h4&gt;

There are far fewer developers who already know Scala than who already
know Java, which means it will be more difficult if you need to
staff up quickly and don't want to spend any time training.

&lt;br/&gt;&lt;br/&gt;
Because of the more advanced concepts embodied in Scala,
or at least concepts which are different from the concepts
that many Java developers have internalized,
it may be possible that some Java developers will have a difficult
time coming up to speed on Scala.
This would effectively decrease
the overall pool of available developers even if you are willing to
spend the time to train someone on Scala.

&lt;br/&gt;&lt;br/&gt;
Until a much larger number of current Java developers try to learn Scala
it will be difficult to know how much of a problem this will be.
One possibility is that many current Java developers will not want to
put the effort into learning the new concepts required to use Scala
effectively, and that a large chunk of the growth in the pool of
Scala developers will come from new developers who were educated
with those concepts from the start.

&lt;a name="better-developers-con"&gt;&lt;/a&gt;
&lt;h4&gt;Better Developers&lt;/h4&gt;

Wait, wasn't this listed as a
&lt;a href="#better-developers-pro"&gt;pro&lt;/a&gt; for Scala?
Yes, this one is a two-edged sword.
Even after accepting the Learning Curve and the current
Limited Developer Pool,
there is a possibility that not every
Java developer has what it takes to become an effective Scala developer.
Scala provides tools that allow highly competent developers to write
very concise code, and if you give them those tools they will use them.
Developers who are less competent may then have trouble understanding
that code.  Java suffers less from this particular problem because it
doesn't provide the kinds of high-level constructs (such as higher-order
functions and continuations) that can cause difficulty for less
capable developers.
If you take the Scala road and hire highly capable developers,
you may be making a commitment to continue to hire only highly capable
developers for some time.

&lt;br/&gt;&lt;br/&gt;
You should ask yourself this question:
would your company culture allow you to hire and keep a developer who is
twice as expensive and
&lt;a href="http://forums.construx.com/blogs/stevemcc/archive/2008/03/27/productivity-variations-among-software-developers-and-teams-the-origin-of-quot-10x-quot.aspx"&gt;
ten times&lt;/a&gt;
as productive?
If your answer is "probably not", then you should stick with Java
where you can hire those developers who are
half as expensive and one-tenth as productive.

&lt;a name="limited-support"&gt;&lt;/a&gt;
&lt;h4&gt;Limited Commercial Support&lt;/h4&gt;

If you have a large Java project and you find you quickly need some
additional developers, you can call a consulting company such as
&lt;a href="http://www.accenture.com"&gt;Accenture&lt;/a&gt; or
&lt;a href="http://www.cognizant.com"&gt;Cognizant&lt;/a&gt;
and they can throw an army of Java developers your way.
If your project is written in Scala,
I suspect they will happily tell you they can provide you with those resources,
but they might have more difficulty
doing that due to the above-mentioned Limited Developer Pool.

&lt;br/&gt;&lt;br/&gt;
You could probably post a request to the
&lt;a href="http://www.scala-lang.org/node/199"&gt;Scala&lt;/a&gt; or
&lt;a href="http://groups.google.com/group/liftweb"&gt;Lift&lt;/a&gt;
mailing lists
and find some contractors with Scala experience to help you out, but there
is not yet much in the way of explicit commercial support for Scala.

&lt;br/&gt;&lt;br/&gt;
The recently founded company
&lt;a href="http://www.scalasolutions.com/"&gt;Scala Solutions&lt;/a&gt;
is the first I am aware of that is advertising support for Scala
in mission-critical applications.
Large companies considering Scala
may not be comfortable without having more choice here.

&lt;a name="tool-immaturity"&gt;&lt;/a&gt;
&lt;h4&gt;Tool Immaturity&lt;/h4&gt;

The development tools for Scala are not as advanced as for Java.
In particular, the IDE plugins are not yet as sophisticated,
so developers who use IDEs may encounter some frustration.
On the other hand, this issue should be addressed by Scala's
&lt;a href="#rapidly-improving-ecosystem"&gt;Rapidly Improving Ecosystem&lt;/a&gt;.

&lt;a name="former-cons"&gt;&lt;/a&gt;
&lt;h3&gt;Former Cons&lt;/h3&gt;

While browsing the web you may run across people complaining about
the items listed in this section.
As I note for each item,
as of this posting at the end of 2010
I believe these issues have been resolved.

&lt;a name="risk-breaking"&gt;&lt;/a&gt;
&lt;h4&gt;Risk of Breaking Changes&lt;/h4&gt;

As a new and evolving language, Scala has gone through some breaking changes
a few times as new versions have been released.  This has caused a lot of
pain for some people, a number of whom have declared in their blog posts
that this demonstrates that Scala is just an academic language and not
ready for the real world.
I believe that, while there still may be some changes to be made,
with the release of version 2.8 the language has reached a level of
stability that makes it suitable for production use.

&lt;a name="risk-abandonment"&gt;&lt;/a&gt;
&lt;h4&gt;Risk of Abandonment&lt;/h4&gt;

Scala is a relatively new language, and some people feel that it may not
"make it" and survive as an ongoing language, in which case any code
written in Scala would become hard to maintain.
I believe this risk is now relatively small, given how it has been growing
over the last few years and the number of big names who have adopted Scala
(see the list &lt;a href="#web-sites"&gt;below&lt;/a&gt;).

&lt;a name="limited-documentation"&gt;&lt;/a&gt;
&lt;h4&gt;Limited Documentation&lt;/h4&gt;

While this may still be an issue for non-English teams,
I believe there are now enough Scala books in English that this
is no longer an issue for anyone who reads English.
The &lt;a href="http://www.scala-lang.org/node/959"&gt;Books on Scala&lt;/a&gt;
page lists 19 books about Scala and Lift, including
five Scala books in English and five in other languages
(some of which are translations of the English versions)
that are currently available.

&lt;a name="references"&gt;&lt;/a&gt;
&lt;h3&gt;References&lt;/h3&gt;

&lt;a name="web-sites"&gt;&lt;/a&gt;
&lt;h4&gt;Web Sites using Scala&lt;/h4&gt;

The Scala web site has a page on
&lt;a href="http://www.scala-lang.org/node/1658"&gt;Scala in the Enterprise&lt;/a&gt;,
last updated a couple of months ago (as of this posting),
listing quite a few companies that are using Scala.

&lt;br/&gt;&lt;br/&gt;
Here are a few well-known web sites using Scala or Scala/Lift
and some tech articles about them:

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.foursquare.com"&gt;Foursquare&lt;/a&gt; -
    &lt;a href="http://www.scala-lang.org/node/5130"&gt;Scala Forum&lt;/a&gt;
&lt;li&gt;&lt;a href="http://www.linkedin.com"&gt;LinkedIn&lt;/a&gt; -
    &lt;a href="http://www.scala-lang.org/node/6436"&gt;Scala Forum&lt;/a&gt;
&lt;li&gt;&lt;a href="http://www.twitter.com"&gt;Twitter&lt;/a&gt; -
    &lt;a href="http://www.artima.com/scalazine/articles/twitter_on_scala.html"&gt;
    Artima&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="other-posts"&gt;&lt;/a&gt;
&lt;h4&gt;Other Posts&lt;/h4&gt;

There are a fair number of posts listing technical pros and cons of Scala,
or comparing Scala to various other languages such as
&lt;a href="http://clojure.org/"&gt;Clojure&lt;/a&gt;
or the others mentioned above,
but it is more difficult to find commentary that is more specifically
directed at the business or management questions.
Below are links to some posts that, while often containing some
technical content, also contain at least some more general comments.
The posts are listed from newest to oldest.

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.scala-lang.org/node/7728"&gt;A thread&lt;/a&gt;
    from September 2010 asking if there is a place for
    management to get support on Scala decisions.
&lt;li&gt;&lt;a href="http://www.quora.com/How-big-is-the-market-for-Scala-programmers-vs-Java-programmers-vs-Python-programmers"&gt;
    Some comments&lt;/a&gt; from August 2010
    on looking for Scala developers (vs Java or Python).
&lt;li&gt;&lt;a href="http://www.quora.com/Why-is-Scala-so-fashionable"&gt;
    "Why is Scala so fashionable?"&lt;/a&gt; from July 2010 -
    mostly comments about the Scala technology, but a few others.
    &lt;a href="http://blog.lostlake.org/"&gt;David Pollack&lt;/a&gt;
    says Scala "lives up to its hype" and has a great community.
&lt;li&gt;&lt;a href="http://www.slideshare.net/razvanc/why-scalaexecutive-overview"&gt;
    "Why Scala"&lt;/a&gt; from May 2010,
    a 15 slide "Executive Overview", although the first half is all technical.
&lt;li&gt;&lt;a href="http://hedemark.net/blog/?p=80"&gt;
    "Is this the time to switch to scala?"&lt;/a&gt; from January 2010 -
    Geir Hedemarks blog entry in which he takes a step back and
    asks why a company might be asking if it is time to switch to Scala
    (as opposed to asking some other questions).
    (In July 2010 he accepted a job managing a group of Scala developers.)
&lt;li&gt;&lt;a href="http://grahamhackingscala.blogspot.com/2010/01/why-your-company-should-let-you-use.html"&gt;
    "Why Your Company Should Let You Use Scala at Work"&lt;/a&gt;, January 2010
    blog post by Graham Lea discussing switching from Java to Scala.
&lt;li&gt;&lt;a href="http://pchiusano.blogspot.com/2009/08/scala-success-story-commercial-usage-of.html"&gt;
    "A Scala Success Story"&lt;/a&gt; from September 2009
    about Capital IQ; mostly technical.
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/1383320/poll-what-is-stopping-you-from-switching-from-java-to-scala"&gt;
    "What is stopping you from switching (from Java) to Scala?"&lt;/a&gt;,
    stackoverflow poll from September 2009.
&lt;li&gt;&lt;a href="http://blog.dhananjaynene.com/2009/08/why-should-i-switch-to-scala/"&gt;
    "Why should I switch to Scala?"&lt;/a&gt;, August 2009 post with both technical
    and some business-related concerns.
&lt;/ul&gt;

Originally posted 2010-12-22.&lt;br/&gt;
Updated 2010-12-22: removed "Higher Performance" item per James Iry's comment, fixed Foursquare per harryh's comment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-2350204372172685894?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/2350204372172685894/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=2350204372172685894' title='23 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2350204372172685894'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/2350204372172685894'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/12/scala-pros-and-cons.html' title='Scala Pros and Cons'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>23</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4064199900176256620</id><published>2010-11-30T06:46:00.000-08:00</published><updated>2010-11-30T08:40:17.337-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Nondeterministic Evaluation in Scala</title><content type='html'>Scala continuations can be used to implement nondeterministic evaluation.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#background"&gt;Background&lt;/a&gt;
&lt;li&gt;&lt;a href="#goal"&gt;Goal&lt;/a&gt;
&lt;li&gt;&lt;a href="#implementation"&gt;Implementation&lt;/a&gt;
&lt;li&gt;&lt;a href="#examples"&gt;Examples&lt;/a&gt;
&lt;li&gt;&lt;a href="#other-implementations"&gt;Other Implementations&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="background"&gt;&lt;/a&gt;
&lt;h3&gt;Background&lt;/h3&gt;
Nondeterministic evaluation is an approach in which choices among multiple
alternatives are factored out of an application so as to allow
writing the application as if all alternatives were simultaneously being
considered.
Factoring the application in this way allows using different strategies
for searching the space of available choices with no or only small changes
to the application code.

&lt;br/&gt;&lt;br/&gt;
A truly nondeterministic evaluation would potentially give different
results (possibly the same set of results in a different order) each time
it is run.
In the simplest implementations, the evaluation is actually deterministic
and does give the same results every time it is run.
A different way of interpreting the phrase "nondeterministic evaluation"
is to think of it from the point of view of the application built on top
of such an evaluator:
the application is written &lt;i&gt;as if&lt;/i&gt; the evaluation were nondeterministic,
and the decision as to whether the implementation is deterministic or
not is entirely within the evaluation package.
In other words, the application is not determining the order of evaluation
among alternatives, therefore from its perspective the evaluation is
nondeterministic.
If at some later time the evaluator is replaced by one with the same API
but which is truly nondeterministic, the correctly-written application will
continue to deliver valid results.

&lt;br/&gt;&lt;br/&gt;
One of the benefits of a nondeterministic evaluation
environment is that an application using that environment can
(with some assumptions about the application's avoidance of side-effects)
transparently be run in a multi-thread, multi-processor or
multi-host version of that
environment so as to parallelize the computation.
Development of such an application can be done on a single-processor
workstation using a small dataset, then moved to a more powerful environment
for use on the real problem with a much larger dataset.

&lt;br/&gt;&lt;br/&gt;
One simple model of nondeterministic evaluation is
the amb evaluator discussed in the MIT "Wizard Book",
&lt;a href="http://mitpress.mit.edu/sicp/"&gt;
Structure and Interpretation of Computer Programs&lt;/a&gt; (SICP) in
&lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html"&gt;
Section 4.3&lt;/a&gt;.
The &lt;a href="http://www-formal.stanford.edu/jmc/basis1/node7.html#SECTION00025000000000000000"&gt;amb&lt;/a&gt;
operator was introduced by John McCarthy in his 1963 paper
&lt;a href="http://www-formal.stanford.edu/jmc/basis1/basis1.html"&gt;
A Basis For a Mathematical Theory of Computation&lt;/a&gt;.

&lt;a name="goal"&gt;&lt;/a&gt;
&lt;h3&gt;Goal&lt;/h3&gt;
Before getting into the implementation, let's take a look at an example
to show where we want to go.
SICP starts its section on
&lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_Temp_608"&gt;
Logic Puzzles&lt;/a&gt;
with this example:
&lt;blockquote&gt;
The following puzzle (taken from Dinesman 1968) is typical of a large class of simple logic puzzles:
&lt;blockquote&gt;
Baker, Cooper, Fletcher, Miller, and Smith live on different floors of
an apartment house that contains only five floors. Baker does not live
on the top floor. Cooper does not live on the bottom floor. Fletcher
does not live on either the top or the bottom floor. Miller lives
on a higher floor than does Cooper. Smith does not live on a floor
adjacent to Fletcher's. Fletcher does not live on a floor adjacent to
Cooper's. Where does everyone live?
&lt;/blockquote&gt;
We can determine who lives on each floor in a straightforward way by
enumerating all the possibilities and imposing the given restrictions:
&lt;pre
&gt;(define (multiple-dwelling)
  (let ((baker (amb 1 2 3 4 5))
        (cooper (amb 1 2 3 4 5))
        (fletcher (amb 1 2 3 4 5))
        (miller (amb 1 2 3 4 5))
        (smith (amb 1 2 3 4 5)))
    (require
     (distinct? (list baker cooper fletcher miller smith)))
    (require (not (= baker 5)))
    (require (not (= cooper 1)))
    (require (not (= fletcher 5)))
    (require (not (= fletcher 1)))
    (require (&amp;gt; miller cooper))
    (require (not (= (abs (- smith fletcher)) 1)))
    (require (not (= (abs (- fletcher cooper)) 1)))
    (list (list 'baker baker)
          (list 'cooper cooper)
          (list 'fletcher fletcher)
          (list 'miller miller)
          (list 'smith smith))))
&lt;/pre&gt;
Evaluating the expression &lt;code&gt;(multiple-dwelling)&lt;/code&gt; produces the result
&lt;pre
&gt;((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1))
&lt;/pre&gt;
&lt;/blockquote&gt;

Here is what the above looks like in my Scala implementation
(see the &lt;a href="#examples"&gt;Examples&lt;/a&gt; section below for boilerplate):
&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax"
  onclick="highlightSyntaxFlipButton()"/&gt;

&lt;br/&gt;
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/amb/MultipleDwelling.scala"&gt;
MultipleDwelling.scala&lt;/a&gt;
&lt;pre name="hlcode" class="scala"
&gt;class MultipleDwelling extends AmbEval[List[(String,Int)]] {
    def distinct(vals:List[Int]):Boolean = {
        vals.distinct.length == vals.length
    }
    generate {
        val baker = amb(List(1,2,3,4,5))
        val cooper = amb(List(1,2,3,4,5))
        val fletcher = amb(List(1,2,3,4,5))
        val miller = amb(List(1,2,3,4,5))
        val smith = amb(List(1,2,3,4,5))
        require(distinct(List(baker,cooper,fletcher,miller,smith)))
        require(baker!=5)
        require(cooper!=1)
        require(fletcher!=5)
        require(fletcher!=1)
        require(miller&gt;cooper)
        require(scala.math.abs(smith-fletcher)!=1)
        require(scala.math.abs(fletcher-cooper)!=1)
        yld(List(
            ("baker",baker),
            ("cooper",cooper),
            ("fletcher",fletcher),
            ("miller",miller),
            ("smith",smith)))
    }
}
&lt;/pre&gt;

As you can see, it is quite similar to the lisp implementation.
In particular, the code that solves this specific problem includes
very little other than the statement of the problem:
a set of possible values, a set of requirements, and a yielding
of the solution.
The implementation choices about how to make choices among the
alternatives listed in the &lt;code&gt;amb&lt;/code&gt; expressions and how to
evaluate those alternatives are all handled in the code for the
&lt;code&gt;AmbEval&lt;/code&gt; class.

&lt;br/&gt;&lt;br/&gt;
Of course, you could write something very similar to the above using
&lt;a href="http://www.scala-lang.org/node/111"&gt;
for-comprehensions&lt;/a&gt;
with guard statements,
and that would likely be more practical for most situations,
but besides the fact that you can't currently use a for-comprehension
in suspendable (CPS) code and thus can't use that construct in a
generator without modifications such as introduced in this post,
that's just not as interesting as the amb evaluator.

&lt;br/&gt;&lt;br/&gt;
The authors of SICP note that the evaluation of their
straightforward implementation of multiple-dwelling is "very slow".
When I execute my version on my desktop machine, it runs in
less than 1/100th of a second.
If that line was written in the original version of 1980,
that same program could have taken 10,000 times as long to run,
a couple of minutes -
a long time for what seems like a small and simple program.

&lt;a name="implementation"&gt;&lt;/a&gt;
&lt;h3&gt;Implementation&lt;/h3&gt;
In this post I implement a simple nondeterministic evaluator
modeled on the amb evaluator introduced above
that allows specific problems to be stated as in the above example
and solved by the nondeterministic evaluator.

&lt;br/&gt;&lt;br/&gt;
In SICP they build the amb evaluator in lisp along with a
REPL that knows how to retrieve and print the multiple results
returned by the evaluation.
Rather than building my own Scala REPL,
I chose to treat the nondeterministic evaluation as a generator.
Thus a nondeterministic computation is implemented as
a special kind of generator,
and it returns its values as does a generator.

&lt;br/&gt;&lt;br/&gt;
In my previous blog post
I showed how to use Scala's
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
delimited continuations&lt;/a&gt;
 to create a
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/standalone-generic-scala-generator.html"&gt;
standalone generator&lt;/a&gt;.
I now extend that generator to support nondeterministic evaluation.
The use of continuations is an essential part of this implementation.
Unlike in my previous examples of the use of continuations,
where continuations were captured but only executed a single time,
in this case a single continuation is executed multiple times.

&lt;br/&gt;&lt;br/&gt;
As seen in the example in the
&lt;a href="#goal"&gt;Goal&lt;/a&gt;
section above,
the implementation uses a class called &lt;code&gt;AmbEval&lt;/code&gt;,
which defines the &lt;code&gt;amb&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt; methods.

&lt;br/&gt;
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/AmbEval.scala"&gt;
AmbEval.scala&lt;/a&gt;
&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.collection.Iterator
import scala.util.continuations._

class AmbEval[T] extends StandaloneGenerator[T] {
    def amb[A](seq:Iterable[A]):A @cpsParam[Unit,Unit] = {
        shift { k:(A=&amp;gt;Unit) =&amp;gt;
            val it = seq.iterator
            reset[Unit,Unit] {
                //Use of var for v is workaround for Scala bug #3501
                var v:A = null.asInstanceOf[A]
                while (it.hasNext) {
                    v = it.next
                    k(v)
                    stepUntilDone
                }
            }
        }
    }

    def require(condition: =&amp;gt;Boolean):Unit @cpsParam[Unit,Unit] = {
        if (!condition) { amb(List()) }
    }
}
&lt;/pre&gt;

The &lt;code&gt;amb&lt;/code&gt; method is mostly straightforward:
it iterates through the values provided to it and calls the supplied
continuation for each value.
The continuation represents the remainder of the code following the
call to &lt;code&gt;amb&lt;/code&gt;, which means all of the code following the
call to &lt;code&gt;amb&lt;/code&gt; will be executed multiple times, once with
each value supplied to the call to &lt;code&gt;amb&lt;/code&gt;.
Each of these calls is "searching" the solution space using one value
from the set of alternatives supplied to &lt;code&gt;amb&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
When the calling code calls &lt;code&gt;amb&lt;/code&gt; a second time,
&lt;code&gt;amb&lt;/code&gt; once again
turns around and calls the remaining code multiple times, once for
each value passed to &lt;code&gt;amb&lt;/code&gt;.
Thus the application code following the second call to &lt;code&gt;amb&lt;/code&gt; gets
executed once for each combination of
every value in the first call to &lt;code&gt;amb&lt;/code&gt; times
every value in the second call to &lt;code&gt;amb&lt;/code&gt;,
which is the cross-product of those two set of alternatives.
Likewise if the application has a third or fourth call to &lt;code&gt;amb&lt;/code&gt;;
each call to &lt;code&gt;amb&lt;/code&gt; multiplies the number of times
the following code is executed.

&lt;br/&gt;&lt;br/&gt;
One subtlety of the implementation is the call to &lt;code&gt;stepUntilDone&lt;/code&gt;.
The call to the continuation &lt;code&gt;k(v)&lt;/code&gt;, which calls the code
in the application following its call to &lt;code&gt;amb&lt;/code&gt;, will return
when that code has finished running; but if that code calls &lt;code&gt;yld&lt;/code&gt;,
it will be suspended, and control will return to &lt;code&gt;amb&lt;/code&gt; after
that suspension.
At this point we need to suspend the &lt;code&gt;amb&lt;/code&gt; code as well, and
when it is resumed, ensure that we resume the continuation code
that we called as &lt;code&gt;k(v)&lt;/code&gt;.
As long as the continuation called from &lt;code&gt;amb&lt;/code&gt; continues to
suspend itself by calling &lt;code&gt;yld&lt;/code&gt;, we need to continue suspending
ourself in the same way.
Once the continuation called from &lt;code&gt;amb&lt;/code&gt; finishes and returns to
&lt;code&gt;amb&lt;/code&gt; without suspending itself, then &lt;code&gt;amb&lt;/code&gt;
can continue in its loop and invoke the continuation with the next
selected value.

&lt;br/&gt;&lt;br/&gt;
Note that using the &lt;code&gt;yld&lt;/code&gt; method to return values from the
generator and allowing multiple calls to &lt;code&gt;yld&lt;/code&gt; by
ensuring that every branch finishes its execution gives
us a capability not in the lisp implementation defined in
SICP and shown above.
In that implementation, a valid choice is indicating by successfully
reaching the end of a branch; the valid value is the return value of
the function.
In our implementation, a single branch can return multiple valid values
by calling &lt;code&gt;yld&lt;/code&gt; multiple times.
This means that, in the Scala spirit,
you are free to write your own code that imperatively determines for itself
the validity of some of the potential alternatives rather than always using
the &lt;code&gt;amb&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt; methods to prune out
invalid alternatives.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;stepUntilDone&lt;/code&gt; method implements the above algorithm to
ensure that the called continuation completes.
In order to know if the called continuation has suspended itself or
completed execution, &lt;code&gt;stepUntilDone&lt;/code&gt; needs to examine private data
in the
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/StandaloneGenerator.scala"&gt;
&lt;code&gt;StandaloneGenerator&lt;/code&gt;&lt;/a&gt; class, so the
cleanest solution is to add the &lt;code&gt;stepUntilDone&lt;/code&gt;
method to that class:

&lt;pre name="hlcode" class="scala"
&gt;//Part of StandaloneGenerator.scala
    def stepUntilDone:Unit @suspendable = {
        //Use of var for saveStep is workaround for Scala bug #3501
        var saveStep:(Unit=&amp;gt;Unit) = null
        while (nextStep.isDefined) {
            saveStep = nextStep.get
            suspend     //sets nextStep to point here
            saveStep()
        }
    }
&lt;/pre&gt;

Note that the previously saved continuation from &lt;code&gt;nextStep&lt;/code&gt;
is saved in the local variable &lt;code&gt;saveStep&lt;/code&gt;, which in turn
is captured as part of the continuation then saved by the call to
&lt;code&gt;suspend&lt;/code&gt;.
This provides a stacking mechanism that allows us to nest multiple calls
to &lt;code&gt;amb&lt;/code&gt; and properly manage the required backtracking.
Since we are using local storage for this, we can have multiple
instances of nondeterministic generators simultaneously suspended.

&lt;br/&gt;&lt;br/&gt;
In both SICP and my implementation,
the &lt;code&gt;amb&lt;/code&gt; function does a depth-first search of the
space of alternatives.
This means it is not suitable for problems that includes multiple
sets of alternatives where one or more sets are infinite.
In fact, it really only does well on collections of sets that are
small enough that the cross-product of all of the sets of choices
can be fully enumerated,
since the code choosing among alternatives makes its choice trivially
and without any knowledge of which choices might be better than others.
Also, the search space must not include any non-terminating branches,
since eventually the algorithm would select that branch and get stuck.

&lt;br/&gt;&lt;br/&gt;
As in SICP, the &lt;code&gt;require&lt;/code&gt; method simply calls
&lt;code&gt;amb&lt;/code&gt; with no alternatives if its predicate is false,
thus pruning the search tree at that point.

&lt;br/&gt;&lt;br/&gt;
I did not attempt to implement any kind of side-effects backtracking
such as capturing changes to global variables and undoing them after
a branch finishes.
Instead, regarding attempting to use
code with side effects within a nondeterministic evaluation block,
I offer this common suggestion:
&lt;a href="http://www.jargon.net/jargonfile/d/Dontdothatthen.html"&gt;
Don't Do That!&lt;/a&gt;

&lt;a name="examples"&gt;&lt;/a&gt;
&lt;h3&gt;Examples&lt;/h3&gt;
Using the &lt;code&gt;AmbEval&lt;/code&gt; class we can easily code up the
&lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.2"&gt;
examples&lt;/a&gt;
given in SICP.
We start with the imports we use for all of the examples below:

&lt;pre name="hlcode" class="scala"
&gt;import scala.collection.Iterator
import scala.util.continuations._
import net.jimmc.scoroutine._
&lt;/pre&gt;

Our examples will all be implemented as generator classes that extend
the &lt;code&gt;AmbEval&lt;/code&gt; class.
We can see the results of each such computation by creating an instance of
that class and printing out all of the values returned by the iterator.
To simplify this, we define a little helper function to dump all of
the results of an iterator:
&lt;pre name="hlcode" class="scala"
&gt;def dump[T](gen:Iterator[T]) {
    for (x &lt;- gen) println(x)
}
&lt;/pre&gt;

Here's how we can use this to print the results of evaluating the
&lt;code&gt;MultipleDwelling&lt;/code&gt; class given in the
&lt;a href="#goal"&gt;Goal&lt;/a&gt; section above:
&lt;pre name="hlcode" class="scala"
&gt;dump(new MultipleDwelling)
&lt;/pre&gt;
output:
&lt;pre name="hlcode" class="scala"
&gt;List((baker,3), (cooper,2), (fletcher,4), (miller,5), (smith,1))
&lt;/pre&gt;

Here are a few more examples, with their output given in a comment
at the end of each code block.

&lt;h5&gt;A Pythagorean Triple Between&lt;/h5&gt;
This problem is from SICP (exercise 4.35):
&lt;blockquote&gt;
"implement a procedure that finds Pythagorean triples, i.e., triples
 of integers (i,j,k) between the given bounds such that i &amp;lt; j and
 i^2 + j^2  = k^2"
&lt;/blockquote&gt;
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/amb/APythagoreanTripleBetween.scala"&gt;
APythagoreanTripleBetween.scala&lt;/a&gt;
&lt;pre name="hlcode" class="scala"
&gt;class APythagoreanTripleBetween(low:Int,high:Int) extends AmbEval[(Int,Int,Int)] {
    generate {
        val i = amb(low to high)
        val j = amb(i to high)
        val k = amb(j to high)
        require(i*i + j*j == k*k)
        yld((i,j,k))
    }
}
&lt;/pre&gt;
output from &lt;code&gt;dump(new APythagoreanTripleBetween(1,20))&lt;/code&gt;:
&lt;pre name="hlcode" class="scala"
&gt;(3,4,5)
(5,12,13)
(6,8,10)
(8,15,17)
(9,12,15)
(12,16,20)
&lt;/pre&gt;

&lt;h5&gt;Liars&lt;/h5&gt;
This problem is from SICP (exercise 4.42):
&lt;blockquote&gt;
Solve the following ``Liars'' puzzle (from Phillips 1934):
&lt;blockquote&gt;
    Five schoolgirls sat for an examination. Their parents -- so they
    thought -- showed an undue degree of interest in the result. They
    therefore agreed that, in writing home about the examination,
    each girl should make one true statement and one untrue one. The
    following are the relevant passages from their letters:
&lt;ul&gt;
    &lt;li&gt; Betty: ``Kitty was second in the examination. I was only third.''
    &lt;li&gt; Ethel: ``You'll be glad to hear that I was on top. Joan was second.''
    &lt;li&gt; Joan: ``I was third, and poor old Ethel was bottom.''
    &lt;li&gt; Kitty: ``I came out second. Mary was only fourth.''
    &lt;li&gt; Mary: ``I was fourth. Top place was taken by Betty.'' 
&lt;/ul&gt;
    What in fact was the order in which the five girls were placed? 
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/amb/Liars.scala"&gt;
Liars.scala&lt;/a&gt;
&lt;pre name="hlcode" class="scala"
&gt;class Liars extends AmbEval[List[(String,Int)]] {
    def distinct(vals:List[Int]):Boolean = {
        vals.distinct.length == vals.length
    }
    generate {
        val betty = amb(List(1,2,3,4,5))
        val ethel = amb(List(1,2,3,4,5))
        val joan = amb(List(1,2,3,4,5))
        val kitty = amb(List(1,2,3,4,5))
        val mary = amb(List(1,2,3,4,5))
        require(distinct(List(betty,ethel,joan,kitty,mary)))
        require((kitty==2 &amp;amp;&amp;amp; betty!=3) || (kitty!=2 &amp;amp;&amp;amp; betty==3))
        require((ethel==1 &amp;amp;&amp;amp; joan!=2) || (ethel!=1 &amp;amp;&amp;amp; joan==2))
        require((joan==3 &amp;amp;&amp;amp; ethel!=5) || (joan!=3 &amp;amp;&amp;amp; ethel==5))
        require((kitty==2 &amp;amp;&amp;amp; mary!=4) || (kitty!=2 &amp;amp;&amp;amp; mary==4))
        require((mary==4 &amp;amp;&amp;amp; betty!=1) || (mary!=4 &amp;amp;&amp;amp; betty==1))
        yld(List(
            ("betty",betty),
            ("ethel",ethel),
            ("joan",joan),
            ("kitty",kitty),
            ("mary",mary)))
    }
}
&lt;/pre&gt;
output from &lt;code&gt;dump(new Liars)&lt;/code&gt;:
&lt;pre name="hlcode" class="scala"
&gt;List((betty,3), (ethel,5), (joan,2), (kitty,1), (mary,4))
&lt;/pre&gt;

&lt;h5&gt;RosettaExample&lt;/h5&gt;
This problem is from the rosettacode.org wiki page for
&lt;a href="http://rosettacode.org/wiki/Amb"&gt;Amb&lt;/a&gt;:
&lt;blockquote&gt;
The example is using amb to choose four words from the following strings:
&lt;br/&gt;&lt;br/&gt;
set 1: "the" "that" "a"
&lt;br/&gt;&lt;br/&gt;
set 2: "frog" "elephant" "thing"
&lt;br/&gt;&lt;br/&gt;
set 3: "walked" "treaded" "grows"
&lt;br/&gt;&lt;br/&gt;
set 4: "slowly" "quickly"
&lt;br/&gt;&lt;br/&gt;
It is a failure if the last character of word 1 is not equal to the
first character of word 2, and similarly with word 2 and word 3, as
well as word 3 and word 4. (the only successful sentence is "that thing
grows slowly").
&lt;/blockquote&gt;
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/amb/RosettaExample.scala"&gt;
RosettaExample.scala&lt;/a&gt;
&lt;pre name="hlcode" class="scala"
&gt;class RosettaExample extends AmbEval[List[String]] {
    generate {
        def joins(s1:String, s2:String) = s1.endsWith(s2.substring(0,1))
        val w1 = amb(List("the","that","a"))
        val w2 = amb(List("frog","elephant","thing"))
        val w3 = amb(List("walked","treaded","grows"))
        val w4 = amb(List("slowly","quickly"))
        require(joins(w1,w2))
        require(joins(w2,w3))
        require(joins(w3,w4))
        yld(List(w1,w2,w3,w4))
    }
}
&lt;/pre&gt;
output from &lt;code&gt;dump(new RosettaExample)&lt;/code&gt;:
&lt;pre name="hlcode" class="scala"
&gt;List(that, thing, grows, slowly)
&lt;/pre&gt;

&lt;a name="other-implementations"&gt;&lt;/a&gt;
&lt;h3&gt;Other Implementations&lt;/h3&gt;

If you poke around on the net you can find implementations of
nondeterministic evaluators such as the amb evaluator.
Some of these implementations suffer from one or more of three problems:
&lt;ul&gt;
&lt;li&gt;They use a for-comprehension or direct sequence iteration
    rather than an external amb evaluator.
    To see the difference, consider how the code would have to be modified
    in order to choose alternatives from each set of choices in random order
    rather than left-to-right.  In an amb evaluator, this change can be
    made in one place.
&lt;li&gt;They don't separate the application requirements
    from the mechanism that makes choices among alternatives.
    This can be seen by considering how a second example problem would
    be implemented in the same framework.
    It should be possible to implement the second problem
    by sharing code used in the implementation of the first problem but
    without either
    modifying or duplicating any code from the first problem.
&lt;li&gt;They use a global variable to store a stack of continuations.
    This makes it impossible to run two independent nondeterministic
    evaluations at the same time.
&lt;/ul&gt;

Implementations of amb in various languages:
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://rosettacode.org/wiki/Amb"&gt;RosettaCode&lt;/a&gt;,
    implementations in many languages -
    but many of the implementations suffer from the problems listed above.
    In particular, the Scala implementation
    uses direct sequence iteration (in the form of a recursive tail operation
    on a list)
    and does not separate the application requirements
    (of first and last letters of adjacent words
    being the same) from the mechanism that makes choices among alternatives.
&lt;li&gt;&lt;a href="http://mail.python.org/pipermail/python-list/2000-March/028478.html"&gt;
    Nondeterministic evaluation in under 300 bytes of Python&lt;/a&gt; -
    Uses a global variable to store a stack of continuations.
&lt;li&gt;&lt;a href="http://www.randomhacks.net/articles/2005/10/11/amb-operator"&gt;
    Ruby&lt;/a&gt; -
    Uses a global variable to store a stack of continuations.
&lt;li&gt;&lt;a href="http://apisuckage.wordpress.com/2009/05/28/challenging-sicp/"&gt;
    C#, Linq&lt;/a&gt; - Uses direct sequence iteration.
&lt;li&gt;&lt;a href="http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-16.html#node_sec_14.2"&gt;
    Scheme&lt;/a&gt;
&lt;/ul&gt;

Some interesting terms:
&lt;ul&gt;
&lt;li&gt;Angelic choice - always avoids choices that lead to nonterminating branches.
&lt;li&gt;Demonic choice - always makes choices that lead to nonterminating branches.
&lt;li&gt;Erratic choice - choices may or may not lead to nonterminating branches.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4064199900176256620?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4064199900176256620/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4064199900176256620' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4064199900176256620'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4064199900176256620'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/11/nondeterministic-evaluation-in-scala.html' title='Nondeterministic Evaluation in Scala'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-3822589431724043957</id><published>2010-09-08T20:33:00.000-07:00</published><updated>2010-09-08T20:33:03.024-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Standalone Generic Scala Generator</title><content type='html'>A generic Scala generator class built directly on Scala's shift and reset operators.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-generators.html"&gt;
previous post&lt;/a&gt;
I showed a
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-generators.html#generators"&gt;
generic generator&lt;/a&gt; class built on top of my
&lt;a href="http://www.github.com/jimmc/scoroutine"&gt;coroutines&lt;/a&gt; library.
I commented in that post that I was taking the expedient approach of
using my existing library, but that it would be possible to package
all of the functionality into the &lt;code&gt;Generator&lt;/code&gt; class.

&lt;br/&gt;&lt;br/&gt;
I soon realized that it would in fact be relatively easy to do that packaging,
and that the resulting relatively simple class
would probably make a good vehicle for demonstrating
the usefulness of Scala's
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
delimited continuations&lt;/a&gt;.
Thus I give you the
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/StandaloneGenerator.scala"&gt;
&lt;code&gt;StandaloneGenerator&lt;/code&gt;&lt;/a&gt; class:

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.collection.Iterator
import scala.util.continuations._

class StandaloneGenerator[T] extends Iterator[T] {
    private var nextValue:Option[T] = None
    private var nextStep:Option[Unit=&gt;Unit] = None

    /** Subclass calls this method to generate values.
     * @param body The code for your generator.
     */
    protected def generate(body: =&gt; Unit @suspendable) {
        reset {
            suspend
            body
        }
    }

    /** Yield the next generated value.
     * Call this code from your generator to deliver the next value.
     */
    protected def yld(x:T):Unit @suspendable = {
        nextValue = Some(x)
        suspend
    }

    /** Retrieve the next generated value.
     * Call this from your main code.
     */
    def next:T = {
        step
        nextValue match {
            case None =&gt; throw new NoSuchElementException("next on empty generator")
                    //make it similar to the equivalent Iterator exception
            case Some(x) =&gt; nextValue = None; x
        }
    }

    /** True if there is another value to retrieve.
     * Call this from your main code.
     */
    def hasNext:Boolean = {
        step
        nextValue.isDefined
    }

    /** Save our continuation to resume later. */
    private def suspend:Unit @suspendable = {
        shift { k:(Unit=&gt;Unit) =&gt;
            nextStep = Some(k)
        }
    }

    /** If we have a next step but we don't have a value queued up,
     * run the generator until it calls yld or completes. */
    private def step = {
        if (nextValue.isEmpty) nextStep foreach { nextStep = None; _() }
    }
}
&lt;/pre&gt;

The &lt;code&gt;StandaloneGenerator&lt;/code&gt; class is a plug-compatible replacement
for the &lt;code&gt;Generator&lt;/code&gt; class described in my previous post,
as long as the derived generator does not use fancy scheduling as in the
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-generators.html#primegen"&gt;
Primes Generator&lt;/a&gt; example.
So, for example, you could take the
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-generators.html#intgen"&gt;
Integers Generator&lt;/a&gt; example from that post,
replace the two occurences of &lt;code&gt;Generator&lt;/code&gt; with
&lt;code&gt;StandaloneGenerator&lt;/code&gt;, and everything would work the same way.

&lt;br/&gt;&lt;br/&gt;
We have two variables:
&lt;code&gt;nextValue&lt;/code&gt; is our one-element queue where we store the
next value to be returned by the generator,
and &lt;code&gt;nextStep&lt;/code&gt; is our "scheduler queue"
where we store our continuation each
time we suspend the generator to return a value.
Both of these variables are of type &lt;code&gt;Option&lt;/code&gt; so that we can
tell when they hold a value.

&lt;br/&gt;&lt;br/&gt;
Control is managed by the two functions &lt;code&gt;suspend&lt;/code&gt;
and &lt;code&gt;step&lt;/code&gt;.
The &lt;code&gt;suspend&lt;/code&gt; method has a &lt;code&gt;shift&lt;/code&gt; block that
could not be much simpler: all it does is store the passed-in
continuation in the &lt;code&gt;nextStep&lt;/code&gt; variable.
Since the body of a &lt;code&gt;shift&lt;/code&gt; block is always the last thing
executed within the scope of the CPS code (delimited either by the
enclosing &lt;code&gt;reset&lt;/code&gt; block or an explicitly invoked continuation),
the fact that &lt;code&gt;suspend&lt;/code&gt; does not execute its continuation means
that after &lt;code&gt;suspend&lt;/code&gt; does its thing,
control returns to the point just past the enclosing &lt;code&gt;reset&lt;/code&gt;,
or to the next statement after the explicit continuation call.

&lt;br/&gt;&lt;br/&gt;
I considered calling the &lt;code&gt;step&lt;/code&gt; method &lt;code&gt;resume&lt;/code&gt;
instead, to make it clear that it was the complement to &lt;code&gt;suspend&lt;/code&gt;,
but from the point of view of the code calling &lt;code&gt;step&lt;/code&gt;,
by the time control returns to the point after the call to &lt;code&gt;step&lt;/code&gt;,
the generator code has already been suspended again:
it has completed running one step, which is from one &lt;code&gt;yld&lt;/code&gt;
call to the next.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;step&lt;/code&gt; function executes our continuation
if we have one, but only
if we don't already have a value in &lt;code&gt;nextValue&lt;/code&gt;.
Using &lt;code&gt;foreach&lt;/code&gt; on an &lt;code&gt;Option&lt;/code&gt; is a neat way
to execute a block of code only if the &lt;code&gt;Option&lt;/code&gt; has a value
(i.e. is not &lt;code&gt;None&lt;/code&gt;).
In this case, since the contents of &lt;code&gt;nextStep&lt;/code&gt; (if it has any)
is a function, the placeholder variable &lt;code&gt;_&lt;/code&gt; gets set to
the continuation and
the code fragment &lt;code&gt;_()&lt;/code&gt; is what actually
executes the continuation.
Here are two other ways this function could be implemented that
do the same thing:

&lt;pre name="hlcode" class="scala"
&gt;    private def step1 = {
        if (nextValue.isEmpty) nextStep match {
            case None =&gt; //do nothing
            case Some(p) =&gt; nextStep = None; p()
        }
    }

    private def step2 = {
        if (nextValue.isEmpty &amp;&amp; nextStep.isDefined) {
            val p = nextStep.get; nextStep = None; p()
        }
    }
&lt;/pre&gt;

Let's walk through this and see how it works
(assuming the generator generates at least one value):
&lt;ol&gt;
&lt;li&gt;The main code instantiates a generator, which passes its generator body
    to the &lt;code&gt;generate&lt;/code&gt; function,
    which calls &lt;code&gt;suspend&lt;/code&gt;,
    which saves that body in
    &lt;code&gt;nextStep&lt;/code&gt; without executing it.
&lt;li&gt;The main code calls &lt;code&gt;hasNext&lt;/code&gt;, which calls &lt;code&gt;step&lt;/code&gt;,
    which runs the continuation.
    This starts the generator body code running, which runs until it
    calls &lt;code&gt;yld&lt;/code&gt; with the first value.
    That first value is stored in &lt;code&gt;nextValue&lt;/code&gt; and
    the generator code is suspended, with the continuation stored
    in &lt;code&gt;nextStep&lt;/code&gt;.
    Since we now have a value stored in &lt;code&gt;nextValue&lt;/code&gt;,
    &lt;code&gt;hasNext&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;.
&lt;li&gt;The main code calls &lt;code&gt;next&lt;/code&gt;.
    Since we have a value in &lt;code&gt;nextValue&lt;/code&gt;, the call to
    &lt;code&gt;step&lt;/code&gt; does nothing (it is only there in case the caller
    calls &lt;code&gt;next&lt;/code&gt; without first calling &lt;code&gt;hasNext&lt;/code&gt;).
    The value in &lt;code&gt;nextValue&lt;/code&gt; is returned, and that variable
    is cleared (set to &lt;code&gt;None&lt;/code&gt;).
&lt;li&gt;Each time the main code calls &lt;code&gt;hasNext&lt;/code&gt;
    and it calls &lt;code&gt;step&lt;/code&gt;, the continuation
    stored in &lt;code&gt;nextStep&lt;/code&gt; is executed,
    the generator code calls &lt;code&gt;yld&lt;/code&gt; with the next value,
    that value is stored into &lt;code&gt;nextValue&lt;/code&gt;,
    the continuation is stored in &lt;code&gt;nextStep&lt;/code&gt;,
    and the generator is suspended,
    and &lt;code&gt;hasNext&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;.
&lt;li&gt;Eventually (for a finite generator) there is a call to &lt;code&gt;step&lt;/code&gt;
    where the generator finishes execution without calling
    &lt;code&gt;yld&lt;/code&gt;.
    When this happens, no values are stored in either
    &lt;code&gt;nextValue&lt;/code&gt; or &lt;code&gt;nextStep&lt;/code&gt;.
    Since there is no value in &lt;code&gt;nextValue&lt;/code&gt;,
    &lt;code&gt;hasNext&lt;/code&gt; returns false.
    Since there is also no value in &lt;code&gt;nextStep&lt;/code&gt;,
    further calls to &lt;code&gt;step&lt;/code&gt; do nothing, and
    &lt;code&gt;hasNext&lt;/code&gt; will continue to return &lt;code&gt;false&lt;/code&gt;.
&lt;/ol&gt;

I dare say this might be one of the simplest realistic examples of
the use of Scala's &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; operators
that you will find.
Two variables, six functions each not more than four lines of body code,
16 lines of function body code in total,
35 lines of code altogether.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-3822589431724043957?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/3822589431724043957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=3822589431724043957' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/3822589431724043957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/3822589431724043957'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/09/standalone-generic-scala-generator.html' title='Standalone Generic Scala Generator'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-8294051801503348978</id><published>2010-09-07T20:53:00.000-07:00</published><updated>2010-09-07T20:57:38.498-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala Generators</title><content type='html'>An implementation of generators on top of
Scala's delimited continuations.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html"&gt;
previous post&lt;/a&gt;
I described a library that supports coroutines on top of Scala's
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
delimited continuations&lt;/a&gt;
capability.
In this post I show how you can easily create generators on top of
that
&lt;a href="http://www.github.com/jimmc/scoroutine"&gt;
coroutine library&lt;/a&gt; (net.jimmc.scoroutine).
This is a second example of the kind of interesting construct
that can be built on top of Scala's delimited continuations.

&lt;br/&gt;&lt;br/&gt;
As with my previous post on coroutines,
you don't need to understand &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;
if you just want to use the &lt;code&gt;Generator&lt;/code&gt; class shown here
to write and use your own generators.
But, as with coroutines,
you should have a basic understanding of CPS code and its
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#control_construct_restrictions"&gt;restrictions&lt;/a&gt;
when writing generators.

&lt;h3&gt;Contents&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#generators"&gt;Generators&lt;/a&gt;
&lt;li&gt;&lt;a href="#intgen"&gt;Integers Generator&lt;/a&gt;
&lt;li&gt;&lt;a href="#primegen"&gt;Primes Generator&lt;/a&gt;
&lt;li&gt;&lt;a href="#samefringe"&gt;Same Fringe&lt;/a&gt;
&lt;li&gt;&lt;a href="#morepossibilities"&gt;More Possibilities&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="generators"&gt;&lt;/a&gt;
&lt;h3&gt;Generators&lt;/h3&gt;

A &lt;a href="http://en.wikipedia.org/wiki/Generator_%28computer_science%29"&gt;
generator&lt;/a&gt;
is a routine that produces values like an iterator but is structured
as a function.
The generated values are returned by calling a special function,
typically called &lt;code&gt;yield&lt;/code&gt;, with each value that is generated.
In our case, since &lt;code&gt;yield&lt;/code&gt; is a reserved word in Scala,
we will use &lt;code&gt;yld&lt;/code&gt; instead.

&lt;br/&gt;&lt;br/&gt;
Generators and coroutines are
&lt;a href="http://en.wikipedia.org/wiki/Coroutine#Coroutines_and_generators"&gt;
closely related&lt;/a&gt;.
Depending on the implementation, generators and coroutines may be
almost the same thing or fairly different,
but in any case,
if you have either one, you can implement the other one on top of it.
Since we already have coroutines
in the &lt;code&gt;net.jimmc.scoroutine&lt;/code&gt; library
described in my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html"&gt;
previous post&lt;/a&gt;,
we will implement generators on top of coroutines using that library.

&lt;br/&gt;&lt;br/&gt;
You can think of this approach as using the Producer-Consumer pattern,
where we set up a generator as the producer and we allow the main
code to act as the consumer.
We create a generic &lt;code&gt;Generator&lt;/code&gt; class that does the following:
&lt;ul&gt;
&lt;li&gt;Creates a &lt;code&gt;CoScheduler&lt;/code&gt; that we use to control
    the generator.
&lt;li&gt;Creates a &lt;code&gt;CoQueue&lt;/code&gt; buffer into which we will place
    the generated values.
&lt;li&gt;Provides convenience functions
    &lt;code&gt;yld&lt;/code&gt; (in place of the reserved word &lt;code&gt;yield&lt;/code&gt;)
    and &lt;code&gt;generate&lt;/code&gt;.
&lt;li&gt;Provides &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;hasNext&lt;/code&gt; functions for
    the consuming code to call from a non-CPS context,
    and so that a &lt;code&gt;Generator&lt;/code&gt; can be used as an
    &lt;code&gt;Iterator&lt;/code&gt;.
&lt;/ul&gt;

This is all simple and straightforward.
Here is the code for
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/Generator.scala"&gt;&lt;code&gt;Generator&lt;/code&gt;&lt;/a&gt;:

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;
&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.collection.Iterator
import scala.util.continuations._

/** Generic generator class.
 */
class Generator[T] extends Iterator[T] {
    val sched = new DefaultCoScheduler
    val buf = new CoQueue[T](sched,1)

    /** Subclass calls this method to generate values.
     * @param body The code for your generator.
     */
    def generate(body: =&amp;gt; Unit @suspendable) {
        sched.addRoutine("gen") { body }
        sched.run
    }

    /** Yield the next generated value.
     * Call this code from your generator to deliver the next value.
     */
    protected def yld(x:T):Unit @suspendable = {
        buf.blockingEnqueue(x)
    }

    /** Retrieve the next generated value.
     * Call this from your main code.
     */
    def next:T = {
        sched.run
        buf.dequeue
    }

    /** True if there is another value to retrieve.
     * Call this from your main code.
     */
    def hasNext:Boolean = {
        sched.run
        !buf.dequeueBlocker.isBlocked
    }
}
&lt;/pre&gt;

We are not concerning ourselves with performance here, so we are simply
using the available &lt;code&gt;DefaultCoScheduler&lt;/code&gt; as our scheduler.
As a future optimization, we could develop a scheduler optimized for
a single coroutine and use that as our scheduler for simple generators
that fit that criterion.
We could go further and use neither a scheduler nor &lt;code&gt;CoQueue&lt;/code&gt;,
packaging all of the functionality directly into the &lt;code&gt;Generator&lt;/code&gt;
class; but we are using the more expedient approach of using those two
pieces, since we already have them and are familiar with their use from
our experience with coroutines.

&lt;a name="intgen"&gt;&lt;/a&gt;
&lt;h3&gt;Integers Generator&lt;/h3&gt;

Here is how we would use our generic &lt;code&gt;Generator&lt;/code&gt; class to create
a generator that will generate integers up to a specified maximum value:

&lt;pre name="hlcode" class="scala"
&gt;import net.jimmc.scoroutine.Generator

class IntGen(max:Int) extends Generator[Int] {
    generate {
        var x = 1
        while (x&amp;lt;=max) {
            yld(x)
            x = x + 1
        }
    }
}
&lt;/pre&gt;

The one catch to remember here is that the body of the &lt;code&gt;generate&lt;/code&gt;
call is CPS code, so as with the body of a coroutine,
there are some &lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#control_construct_restrictions"&gt;restrictions&lt;/a&gt;
on what control constructs we can use.
Thus we use a &lt;code&gt;while&lt;/code&gt; loop with a &lt;code&gt;var&lt;/code&gt;
rather than a &lt;code&gt;for&lt;/code&gt; loop, since the latter does not
yet work with the continuations compiler plugin.

&lt;br/&gt;&lt;br/&gt;
Given the above generator class, here is a simple
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/GenInts.scala"&gt;&lt;code&gt;GenInts&lt;/code&gt;&lt;/a&gt;
object with a &lt;code&gt;main&lt;/code&gt; function
that creates an instance of that generator,
then calls it to print out its values:

&lt;pre name="hlcode" class="scala"
&gt;object GenInts {
    def main(args:Array[String]) = {
        val gen = new IntGen(4)
        for (i &amp;lt;- gen)
            println(i)
    }
}
&lt;/pre&gt;

Alternatively, we could replace the &lt;code&gt;for&lt;/code&gt; loop with direct
calls to &lt;code&gt;hasNext&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;object GenInts {
    def main(args:Array[String]) = {
        val gen = new IntGen(4)
        while (gen.hasNext)
            println(gen.next)
    }
}
&lt;/pre&gt;

&lt;a name="primegen"&gt;&lt;/a&gt;
&lt;h3&gt;Primes Generator&lt;/h3&gt;

It is
&lt;a href="http://stackoverflow.com/questions/2201882/implementing-yield-yield-return-using-scala-continuations/2215182"&gt;
possible&lt;/a&gt; to
&lt;a href="http://stackoverflow.com/questions/2137619/scala-equivalent-to-python-generators"&gt;
use&lt;/a&gt; &lt;code&gt;shift&lt;/code&gt; and &lt;code&gt;reset&lt;/code&gt; directly
to code up a generator,
but because our coroutine library implements a scheduler
to which new coroutines can be added at any time,
this gives you the ability to create generators that include
dynamic filter pipelines.

&lt;br/&gt;&lt;br/&gt;
The example I use for this is the
&lt;a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"&gt;
Sieve of Eratosthenes&lt;/a&gt;,
a method of calculating primes in which,
each time a prime is found, it is added to a list of prime divisors
that are used for testing each new candidate.
In this
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/GenPrimes.scala"&gt;&lt;code&gt;GenPrimes&lt;/code&gt;&lt;/a&gt;
example, I create a new filter for each prime and add it to the pipeline.
You can do this much more efficiently in Scala
&lt;a href="http://en.literateprograms.org/Sieve_of_Eratosthenes_%28Scala%29"&gt;
using a Stream&lt;/a&gt;,
but this example illustrates the technique of dynamically building a pipeline
within a generator.

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.continuations._

import net.jimmc.scoroutine.CoQueue
import net.jimmc.scoroutine.Generator

object GenPrimes {
    def main(args:Array[String]) = {
        val gen = new PrimeGen()
        for (i &amp;lt;- gen) {
            println("Prime: "+i)
        }
    }
}

class PrimeGen extends Generator[Int] {
    val bufSize = 1
    val out1 = new CoQueue[Int](sched,bufSize)

    sched.addRoutine("prime2")(nextPrime(2,out1))
    generate {
        def gen(n:Int):Unit @suspendable = {
            out1.blockingEnqueue(n)
            gen(n+1)
        }
        gen(2)
    }

    def nextPrime(p:Int, in:CoQueue[Int]):Unit @suspendable = {
        var out:Option[CoQueue[Int]] = None
        yld(p)
        def sieve():Unit @suspendable = {
            val n = in.blockingDequeue()
            if ((n%p)!=0) {
                if (!out.isDefined) {
                    out = Some(new CoQueue[Int](sched,bufSize))
                    val rName = "prime"+n
                    sched.addRoutine(rName)(nextPrime(n,out.get))
                }
                out.get.blockingEnqueue(n)
            } else {
                in.dequeueBlocker.waitUntilNotBlocked
            }
            sieve()
        }
        sieve()
    }
}
&lt;/pre&gt;

This example starts by setting up two coroutines:
the &lt;code&gt;addRoutine&lt;/code&gt; call sets up the first filter in the pipeline,
which reads values from the &lt;code&gt;out1&lt;/code&gt; queue and
filters our all numbers divisible by 2.
The &lt;code&gt;generator&lt;/code&gt; call sets up the other initial coroutine,
which generates every integer and feeds it into the first filter in
the pipeline.
We start off this counting generator with the first prime number, 2.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;nextPrime&lt;/code&gt; function is called each time we see a new prime.
It starts by outputting its prime parameter value &lt;code&gt;p&lt;/code&gt;
as a value of the GenPrimes generator.
It then goes into a loop reading its input buffer and looking for values
which are not divisible by its prime number.
The first time it finds one (when &lt;code&gt;out&lt;/code&gt; is not yet defined)
it registers (with a call to &lt;code&gt;addRoutine&lt;/code&gt;)  a new coroutine
based on a new instance of &lt;code&gt;nextPrime&lt;/code&gt; that uses our output
as its input.
It then passes each candidate prime along to that next filter in
the sieve pipeline.

&lt;br/&gt;&lt;br/&gt;
You can tell this is CPS code because of the &lt;code&gt;suspendable&lt;/code&gt;
annotations, which is a cue to realizing
that the code might not behave quite as you think.
For example, the &lt;code&gt;gen&lt;/code&gt; function within the body of the
&lt;code&gt;generate&lt;/code&gt; call is recursive, so you might think it would
cause a stack overflow.
But since we are in a CPS function and the call to &lt;code&gt;blockingEnqueue&lt;/code&gt;
is a call to a CPS function, the recursive call to &lt;code&gt;gen&lt;/code&gt;
is turned into a continuation and executed later from the scheduler,
so it is in fact not recursive.
Likewise the recursive call to &lt;code&gt;sieve&lt;/code&gt; is not really
recursive for the same reason.

&lt;br/&gt;&lt;br/&gt;
Another CPS detail is the call to &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;.
It would seem to be functionally unnecessary, since the first thing
in the &lt;code&gt;sieve&lt;/code&gt; function is a call to &lt;code&gt;blockingDequeue&lt;/code&gt;.
However, this is the same attempt to avoid blocking as discussed in my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html#attempt-to-avoid-blocking"&gt;previous post&lt;/a&gt;;
without this call our code will not work.

&lt;a name="samefringe"&gt;&lt;/a&gt;
&lt;h3&gt;Same Fringe&lt;/h3&gt;

The
&lt;a href="http://c2.com/cgi/wiki?SameFringeProblem"&gt;SameFringe&lt;/a&gt;
problem has
&lt;a href="http://www.crystalclearsoftware.com/soc/coroutine/coroutine/stackful.html#id2607744"&gt;
been called&lt;/a&gt; the "killer application" for coroutines.
Given two trees, they have the same fringe if the leaves of the two
trees, read from left to right, are the same.

&lt;br/&gt;&lt;br/&gt;
With coroutines, or in this case generators,
 the simple solution to this problem
is to create a generator that takes a tree and returns the sequence
of leaves of that tree,
then compare the outputs of two of those generators on the two trees
to be compared.

&lt;br/&gt;&lt;br/&gt;
We start with a simple tree definition:

&lt;pre name="hlcode" class="scala"
&gt;sealed abstract class Tree[T]
case class Branch[T](left:Tree[T], right:Tree[T]) extends Tree[T]
case class Leaf[T](x:T) extends Tree[T]
&lt;/pre&gt;

Given this tree definition, we write a generator that walks a tree
and yields all of the leaves:

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.continuations._
import net.jimmc.scoroutine.Generator

class TreeFringe[T](tree:Tree[T]) extends Generator[T] {
    generate {
        def walk(t:Tree[T]):Unit @suspendable = {
            t match {
                case Leaf(x) =&gt; yld(x)
                case Branch(left,right) =&gt; walk(left); walk(right)
            }
        }
        walk(tree)
    }
}
&lt;/pre&gt;

Since our generators implement the
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/Iterator.html"&gt;
&lt;code&gt;Iterator&lt;/code&gt;&lt;/a&gt; trait, we can compare
two generators as two iterators with this little piece of code,
making the assumption that the tree leaf values are never null:

&lt;pre name="hlcode" class="scala"
&gt;    def sameFringe[T](tree1:Tree[T], tree2:Tree[T]):Boolean = {
        !((new TreeFringe(tree1)).zipAll(new TreeFringe(tree2),null,null).
            exists(p=&gt;p._1!=p._2))
    }
&lt;/pre&gt;

Alternatively, we could use this more verbose version:

&lt;pre name="hlcode" class="scala"
&gt;    def sameFringe[T](tree1:Tree[T], tree2:Tree[T]):Boolean = {
        val fringe1 = new TreeFringe(tree1)
        val fringe2 = new TreeFringe(tree2)
        while(fringe1.hasNext &amp;&amp; fringe2.hasNext) {
            if (fringe1.next != fringe2.next)
                return false;
        }
        !(fringe1.hasNext || fringe2.hasNext)
    }
&lt;/pre&gt;

We add a
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/SameFringe.scala"&gt;&lt;code&gt;SameFringe&lt;/code&gt;&lt;/a&gt; object with a
main method that creates some test trees, prints out
the leaves of each tree using our generator, then calls our
&lt;code&gt;sameFringe&lt;/code&gt; method to check for equality.

&lt;pre name="hlcode" class="scala"
&gt;
object SameFringe {
    def main(args:Array[String]) = {
        val t1 = Branch(Branch(Leaf(1),Leaf(2)),Leaf(3))
        val t2 = Branch(Leaf(1),Branch(Leaf(2),Leaf(3)))
        val t3 = Branch(Leaf(1),Branch(Leaf(2),Leaf(4)))
        println("t1:"); for (x &lt;- (new TreeFringe(t1))) println(x)
        println("t2:"); for (x &lt;- (new TreeFringe(t2))) println(x)
        println("t3:"); for (x &lt;- (new TreeFringe(t3))) println(x)
        println("sameFringe(t1,t2)="+sameFringe(t1,t2))
        println("sameFringe(t1,t3)="+sameFringe(t1,t3))
    }
    //include the sameFringe method in this object
}
&lt;/pre&gt;

&lt;a name="morepossibilities"&gt;&lt;/a&gt;
&lt;h3&gt;More Possibilities&lt;/h3&gt;

Some other possible uses for generators or coroutines:

&lt;ul&gt;
&lt;li&gt;Pipelines: A sequence of tasks can operate on a stream of data,
    with each task reading data from an input queue and writing to an
    output queue which is the input queue of the next task in the sequence.
&lt;li&gt;Fan-out: A single producer with multiple consumers can be implemented
    by using a fan-out coroutine that reads from its input queue and writes
    the same data to multiple output queues, each of which is the input
    queue to one of the multiple consumers.
&lt;li&gt;Fan-in: Multiple producers can use a single shared output queue so that the
    coroutine using that queue as its input queue receives data from
    multiple sources.  If you stick with a single-thread scheduler,
    you don't have to worry about synchronization or other concurrent
    access issues on the shared queue.
    By combining Pipelines, Fan-out and Fan-in, we can create arbitrary
    networks of communicating coroutines.
&lt;li&gt;State machines: For any situation in which a task has to maintain state
    based on one or more inputs, a coroutine or generator can be used to
    allow some of that state to be stored as the location of current
    execution in the code, which often makes the code simpler to write
    and maintain.
&lt;li&gt;Parsers: A parser is a typical example of a producer that reads an
    input stream and maintains state. As the parser collects input characters
    (which could be provided by another coroutine in a pipeline)
    and resolves them into tokens, it writes them to its output queue
    where the tokens are available to the routine handling the next level of
    analysis.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-8294051801503348978?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/8294051801503348978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=8294051801503348978' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8294051801503348978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8294051801503348978'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/09/scala-generators.html' title='Scala Generators'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-8501641801714740144</id><published>2010-09-06T14:38:00.000-07:00</published><updated>2010-09-06T14:38:08.232-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala Coroutines</title><content type='html'>An implementation of coroutines on top of
Scala's delimited continuations.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
previous post&lt;/a&gt;
I said that delimited continuations could be used to create
interesting control constructs.
In this post I give examples of one such construct: coroutines.

&lt;br/&gt;&lt;br/&gt;
I describe the implementation of a library to make it easier to
write coroutines, and I give an example that is built on that library.
If you want to go straight to the example, see the section
&lt;a href="#producer-consumer"&gt;Producer-Consumer&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
Like my previous post on
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
delimited continuations&lt;/a&gt;,
this is a long post.
However, though long, it should be much easier going than that post.
You don't need to read and understand that post in order to understand
this post, but it will help in two places:
&lt;ol&gt;
&lt;li&gt;In the discussion of the
&lt;a href="#coscheduler-api"&gt;CoScheduler API&lt;/a&gt; and
&lt;a href="#coscheduler-impl"&gt;implementation&lt;/a&gt;,
an understanding of &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; is required.
&lt;li&gt;You need to understand at some level about CPS code and its
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#control_construct_restrictions"&gt;restrictions&lt;/a&gt;
when writing coroutines or using the CPS-only methods in
&lt;code&gt;CoQueue&lt;/code&gt; and &lt;code&gt;Blocker&lt;/code&gt;.
&lt;/ol&gt;

&lt;h3&gt;Contents&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="#coroutines"&gt;Coroutines&lt;/a&gt;
&lt;li&gt;&lt;a href="#why-coroutines"&gt;Why Use Coroutines&lt;/a&gt;
&lt;li&gt;&lt;a href="#library"&gt;Building a Coroutine Library&lt;/a&gt;
&lt;li&gt;&lt;a href="#blocker"&gt;Blocker&lt;/a&gt;
&lt;li&gt;&lt;a href="#coqueue"&gt;CoQueue&lt;/a&gt;
&lt;li&gt;&lt;a href="#attempt-to-avoid-blocking"&gt;An Attempt to Avoid Blocking&lt;/a&gt;
&lt;li&gt;&lt;a href="#coscheduler-api"&gt;CoScheduler API&lt;/a&gt;
&lt;li&gt;&lt;a href="#coscheduler-impl"&gt;CoScheduler Implementation&lt;/a&gt;
&lt;li&gt;&lt;a href="#defaultcoscheduler"&gt;DefaultCoScheduler&lt;/a&gt;
&lt;li&gt;&lt;a href="#other-schedulers"&gt;Other Schedulers&lt;/a&gt;
&lt;li&gt;&lt;a href="#producer-consumer"&gt;Producer-Consumer&lt;/a&gt;
&lt;li&gt;&lt;a href="#resources"&gt;Resources&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="coroutines"&gt;&lt;/a&gt;
&lt;h3&gt;Coroutines&lt;/h3&gt;

The &lt;a href="http://en.wikipedia.org/wiki/Coroutine"&gt;
Wikipdeia coroutines&lt;/a&gt; page
says coroutines are a generalization of subroutines:
whereas a subroutine starts at the beginning every time it is called,
a coroutine starts at the beginning the first time it is called,
but can start at any other point on successive calls.
Generally this means that on successive calls, it continues running
from the point where it most recently returned.

&lt;br/&gt;&lt;br/&gt;
Instead of using a &lt;code&gt;return&lt;/code&gt; statement,
coroutines typically use a &lt;code&gt;yield&lt;/code&gt; statement.
A &lt;code&gt;yield&lt;/code&gt; statement
indicates that the routine is done executing for now,
and will resume execution following the &lt;code&gt;yield&lt;/code&gt; statement
the next time it is called.

&lt;br/&gt;&lt;br/&gt;
In the classic definition of coroutines,
the &lt;code&gt;yield&lt;/code&gt; statement indicates which coroutine is to be run next.
With two coroutines, each always yields to the other;
with more than two, a coroutine might have code to determine which
other coroutine to yield to.

&lt;br/&gt;&lt;br/&gt;
Unfortunately for coroutines, &lt;code&gt;yield&lt;/code&gt;
is already a keyword in Scala, so we can't use it for our purposes.
We can either pick a slightly different word such as
&lt;code&gt;yld&lt;/code&gt; or &lt;code&gt;yieldTo&lt;/code&gt;,
or we can just use a different term altogether.

&lt;br/&gt;&lt;br/&gt;
The classic example of the use of coroutines is a pair of routines,
one of which (the producer) generates data and
one of which (the consumer) consumes data.
These two routines are connected by a queue; the producer puts data
into the queue and the consumer takes data out of the queue.

&lt;br/&gt;&lt;br/&gt;
This same producer-consumer pair is also a typical example
of multi-thread code.
Ted Neward uses this producer-consumer example in his
&lt;a href="http://www.ibm.com/developerworks/java/library/j-scala02049.html"&gt;
blog post&lt;/a&gt; describing the concurrency benefits of using Scala.

&lt;br/&gt;&lt;br/&gt;
In the multi-thread producer-consumer example,
the producer thread runs and places data into the queue
until the queue is full,
at which point that thread stops running until the queue empties out enough
for it to add more data.
Meanwhile the consumer thread runs and takes data out of the queue
until the queue is empty,
at which point that thread stop running until the queue contains more data.
If the host on which the multi-thread application is running contains
more than one processor, both of these threads might be running
at the same time.

&lt;br/&gt;&lt;br/&gt;
I like to think of coroutines as being like multi-thread code,
only without multiple threads.
The coroutine version of the producer-consumer example
works essentially the same as the multi-thread version,
except that only one of the two is ever running at one point in time.
The producer runs and places data into the queue until it is full,
at which point it pauses and the consumer starts running.
The consumer takes data out of the queue until it is empty,
at which point it pauses and the producer starts running again.
&lt;br/&gt;&lt;br/&gt;
In both the multi-thread and the coroutine version of this example,
there is some state that is saved while each routine is paused
waiting for the queue to fill or empty.
In the multi-thread example, that state is saved in the thread.
In our coroutine example, we use a different mechanism to save
that state: a delimited continuation.

&lt;a name="why-coroutines"&gt;&lt;/a&gt;
&lt;h3&gt;Why Use Coroutines&lt;/h3&gt;

If coroutines are like multi-thread code, why use them and have to
deal with continuations rather than just using threads?
Here are some possible reasons:
&lt;ul&gt;
&lt;li&gt;With the
    &lt;a href="#defaultcoscheduler"&gt;default scheduler&lt;/a&gt;
    that runs everything in one thread,
    you don't need to worry about concurrency issues such as
    multiple concurrent access to shared state.
&lt;li&gt;You can create your own scheduler to control when to run each of your
    coroutines.  If you want that scheduler to use a thread pool and
    run coroutines concurrently, you can do that (assuming you then
    deal with concurrency issues in your coroutines).
&lt;li&gt;An application can handle more suspended continuations than it can
    suspended threads (for example, see slide 19 of Phillip Haller's
    &lt;a href="http://lamp.epfl.ch/~phaller/doc/ScalaActors.pdf"&gt;presentation&lt;/a&gt;
    on Actors, where he says an app can handle up to 5,000
    threads, but up to 1,200,000 actors).
&lt;/ul&gt;

&lt;a name="library"&gt;&lt;/a&gt;
&lt;h3&gt;Building a Coroutine Library&lt;/h3&gt;

It is &lt;a href="http://scala-programming-language.1934581.n4.nabble.com/Delimited-continuations-and-Scala-td1947779.html"&gt;
possible&lt;/a&gt; to write coroutines directly in Scala code using
&lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;, but
dealing with delimited continuations can be tricky,
so I wanted to isolate all of that code into a reusable library
that would make it easier to write coroutines
as well as allow encapsulating more sophisticated
capabilities within the library.
The package name I selected for this library is
&lt;code&gt;net.jimmc.scoroutine&lt;/code&gt;.
The source code is
&lt;a href="http://github.com/jimmc/scoroutine"&gt;
available&lt;/a&gt; on github.

&lt;br/&gt;&lt;br/&gt;
I started with a change that makes these coroutines look less like
coroutines and more like the multi-thread model:
rather than have a coroutine specify what other coroutine is to be run,
I wanted to be able to specify only that the coroutine is ready to give
up control.
Essentially, rather than yielding to another coroutine,
I always yield to a scheduler,
and the scheduler selects and then yields to the next coroutine.
Given such a scheduler (described &lt;a href="#coscheduler-api"&gt;below&lt;/a&gt;),
we can create a few simple constructs on which to build our
coroutine API.

&lt;a name="blocker"&gt;&lt;/a&gt;
&lt;h3&gt;Blocker&lt;/h3&gt;

In the typical producer-consumer example,
there is an explicit check to see if the routine is blocked,
and if so, then a call to &lt;code&gt;yield&lt;/code&gt; is made.
I wanted something more generic, so I created an abstract trait
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/Blocker.scala"&gt;&lt;code&gt;Blocker&lt;/code&gt;&lt;/a&gt;
to represent the condition that a routine could be blocked by something:

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;
&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

trait Blocker {
    def isBlocked: Boolean
}
&lt;/pre&gt;

The implementations of this for the producer and consumer are straightforward:
for the producer, &lt;code&gt;isBlocked&lt;/code&gt; returns true when the
queue is full;
for the consumer, &lt;code&gt;isBlocked&lt;/code&gt; returns true when
the queue is empty.

&lt;br/&gt;&lt;br/&gt;
Given the &lt;code&gt;isBlocked&lt;/code&gt; method, the typical coroutine always
includes a code fragment that looks something like this:

&lt;pre name="hlcode" class="scala"
&gt;while (isBlocked)
    yield control
&lt;/pre&gt;

Since we will always be yielding control to the scheduler, we can encapsulate
this into a more convenient method, which I have called
&lt;code&gt;waitUntilNotBlocked&lt;/code&gt;.
I added this function to my &lt;code&gt;Blocker&lt;/code&gt; trait, and delegated it to
a scheduler of type
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/CoScheduler.scala"&gt;&lt;code&gt;CoScheduler&lt;/code&gt;&lt;/a&gt;:

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

trait Blocker {
    val scheduler: CoScheduler  //class must override to provide instance
    def isBlocked: Boolean

    def waitUntilNotBlocked:Unit = {
        scheduler.waitUntilNotBlocked(this)
    }
}
&lt;/pre&gt;

We pass &lt;code&gt;this&lt;/code&gt; to the scheduler so that it can call our
&lt;code&gt;isBlocked&lt;/code&gt; method and continue our execution only
when &lt;code&gt;isBlocked&lt;/code&gt; returns false.

&lt;br/&gt;&lt;br/&gt;
There is one more detail to be added to &lt;code&gt;Blocker&lt;/code&gt;,
but it is an important one.
I stated above that this implementation is built on
top of delimited continuations.
When we call &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;
and we are blocked, we want the coroutine library to wait until we
are no longer blocked and then continue execution of our routine.
The coroutine library will be using delimited continuations to do this,
and since our routine might be suspended, the signature
for the &lt;code&gt;waitUntilNotBlocked&lt;/code&gt; method must
include the
&lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#annotations"&gt;
&lt;code&gt;suspendable&lt;/code&gt;&lt;/a&gt; annotation.
We add that annotation along with a suitable &lt;code&gt;import&lt;/code&gt; statement
to get our final version of &lt;code&gt;Blocker&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.util.continuations._

trait Blocker {
    val scheduler: CoScheduler  //class must override to provide instance
    def isBlocked: Boolean

    def waitUntilNotBlocked:Unit @suspendable = {
        scheduler.waitUntilNotBlocked(this)
    }
}
&lt;/pre&gt;

&lt;a name="coqueue"&gt;&lt;/a&gt;
&lt;h3&gt;CoQueue&lt;/h3&gt;

Once we have &lt;code&gt;Blocker&lt;/code&gt;, it is easy to compose a blocking
version of the standard
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html"&gt;&lt;code&gt;Queue&lt;/code&gt;&lt;/a&gt; class
(where by "blocking" I mean that the coroutine will be suspended until
the specified &lt;code&gt;Blocker&lt;/code&gt; is no longer blocked).
We want a version of the
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html#enqueue(A*):Unit"&gt;
&lt;code&gt;Queue.enqueue&lt;/code&gt;&lt;/a&gt; function
that blocks when the queue is full,
and a version of the
&lt;a href="http://www.scala-lang.org/api/current/scala/collection/mutable/Queue.html#dequeue():A"&gt;
&lt;code&gt;Queue.dequeue&lt;/code&gt;&lt;/a&gt; function
that blocks when the queue is empty.
We create a thin wrapper around the &lt;code&gt;Queue&lt;/code&gt; class,
which we call &lt;code&gt;CoQueue&lt;/code&gt;,
to implement our blocking methods for use with our coroutines.
For each of our two blocking conditions,
we create an instance of &lt;code&gt;Blocker&lt;/code&gt;
with those conditions as each of their &lt;code&gt;isBlocked&lt;/code&gt; functions,
and we use those &lt;code&gt;Blocker&lt;/code&gt; instances to create our
&lt;code&gt;blockingEnqueue&lt;/code&gt; and &lt;code&gt;blockingDequeue&lt;/code&gt; methods.

&lt;br/&gt;&lt;br/&gt;
Because the &lt;code&gt;blockingEnqueue&lt;/code&gt;
and &lt;code&gt;blockingDequeue&lt;/code&gt; methods might block,
they must be annotated as &lt;code&gt;suspendable&lt;/code&gt;,
which means they can only be called from within CPS code.
Here is our entire
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/CoQueue.scala"&gt;&lt;code&gt;CoQueue&lt;/code&gt;&lt;/a&gt;
class:

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.util.continuations._
import scala.collection.mutable.Queue

class CoQueue[A](val scheduler:CoScheduler, val waitSize:Int)
        extends Queue[A] { coqueue =&amp;gt;

    val enqueueBlocker = new Blocker() {
        val scheduler = coqueue.scheduler
        def isBlocked() = length &amp;gt;= waitSize
    }

    val dequeueBlocker = new Blocker() {
        val scheduler = coqueue.scheduler
        def isBlocked() = isEmpty
    }

    def blockingEnqueue(x:A):Unit @suspendable = {
        enqueueBlocker.waitUntilNotBlocked
        enqueue(x)
    }

    def blockingDequeue():A @suspendable = {
        dequeueBlocker.waitUntilNotBlocked
        dequeue
    }
}
&lt;/pre&gt;

&lt;a name="attempt-to-avoid-blocking"&gt;&lt;/a&gt;
&lt;h3&gt;An Attempt to Avoid Blocking&lt;/h3&gt;

You might wonder if we could implement &lt;code&gt;blockingEnqueue&lt;/code&gt;
as follows so as to avoid the call to the scheduler's
&lt;code&gt;waitUntilNotBlocked&lt;/code&gt; method:

&lt;pre name="hlcode" class="scala"
&gt;//this won't compile
    def blockingEnqueue(x:A):Unit @suspendable = {
        if (enqueueBlocker.isBlocked)
            enqueueBlocker.waitUntilNotBlocked
        enqueue(x)
    }
&lt;/pre&gt;

However, the compiler gives this error message:

&lt;pre name="hlcode" class="scala"
&gt;
CoQueue.scala:22: error: type mismatch;
 found   : Unit
 required: Unit @scala.util.continuations.cpsParam[Unit,Unit]
        if (enqueueBlocker.isBlocked)
        ^
&lt;/pre&gt;

Syntactically, the problem is that the above one-sided
&lt;code&gt;if&lt;/code&gt; statement is equivalent to

&lt;pre name="hlcode" class="scala"
&gt;        if (enqueueBlocker.isBlocked)
            enqueueBlocker.waitUntilNotBlocked
        else
            ()
&lt;/pre&gt;

The type of &lt;code&gt;()&lt;/code&gt; is &lt;code&gt;Unit&lt;/code&gt;, but the type of
&lt;code&gt;waitUntilNotBlocked&lt;/code&gt; is &lt;code&gt;Unit @suspendable&lt;/code&gt;,
so there is a type mismatch.

&lt;br/&gt;&lt;br/&gt;
You might think we could use some trickery such as this:

&lt;pre name="hlcode" class="scala"
&gt;//compiles, but won't run properly
    def unitSuspendable:Unit @suspendable = ()

    def blockingEnqueue(x:A):Unit @suspendable = {
        if (enqueueBlocker.isBlocked)
            enqueueBlocker.waitUntilNotBlocked
        else
            unitSuspendable
        enqueue(x)
    }
&lt;/pre&gt;

This will compile without errors, but will not work properly.
The problem is that we are trying to define one code path that
is CPS (through &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;) and one path that
is not (through &lt;code&gt;unitSuspendable&lt;/code&gt;).
The CPS compiler plugin transforms the code to use continuations,
which, as you might recall from the discussion in my previous post,
packages the rest of the function up in a continuation and passes it
along to the called function.
But the &lt;code&gt;unitSuspendable&lt;/code&gt; expression does nothing with that
continuation; it neither executes it nor saves it for later execution.
Thus as soon as this code path is taken, the continuation -
which represents
the remainder of the execution of the entire delimited continuation,
including the caller of this function -
is dropped, and the whole delimited continuation is done.

&lt;a name="coscheduler-api"&gt;&lt;/a&gt;
&lt;h3&gt;CoScheduler API&lt;/h3&gt;

The scheduler is the piece of the coroutine library that saves the
continuations for all of the participating coroutines and determines
when to execute those continuations.
By design, all of the tricky stuff is encapsulated in this class.
I have divided the discussion of the scheduler into two section:
this first section discusses at a high level the tasks for which the
schedule must take responsibility, and defines an API to perform
those tasks.
The following section describes the implementation of that API.
If you don't want to get too far into the delimited continuation stuff,
you can read just this section and skip the implementation section.

&lt;br/&gt;&lt;br/&gt;
I started with the requirements that it should be possible to
write a scheduler that:
&lt;ol&gt;
&lt;li&gt;uses one thread for all participating coroutines, or uses
    a thread pool so that multiple coroutines can run at once;
&lt;li&gt;uses a simple algorithm
    to select which coroutine to run next, such as round-robin,
    or a more complex algorithm, such as a priority-based approach;
&lt;li&gt;can be instantiated multiple times
    so that different collections of coroutines can be managed with
    different schedulers.
&lt;/ol&gt;

In order to allow all of the above,
the scheduler API is defined in a trait,
which I call &lt;code&gt;CoScheduler&lt;/code&gt;.
&lt;code&gt;CoScheduler&lt;/code&gt; needs to define three basic functions:
&lt;ol&gt;
&lt;li&gt;Register a coroutine.
    I chose to do this with a method that accepts a block of code which
    is the coroutine body.
    Since the coroutine body might be suspended, the signature for that
    argument must include the &lt;code&gt;suspendable&lt;/code&gt; annotation.
    I call this method &lt;code&gt;addRoutine&lt;/code&gt;.
    To improve tracing, I also pass in a name argument that can be
    used to identify that coroutine.
&lt;li&gt;Wait until a coroutine is no longer blocked.
    This is the method to which &lt;code&gt;Blocker.waitUntilNotBlocked&lt;/code&gt;
    will delegate.
    It is also called &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;, and takes an
    argument which is the &lt;code&gt;Blocker&lt;/code&gt; whose
    &lt;code&gt;isBlocked&lt;/code&gt; method will be used to make that determination.
    Since this method will be called from the coroutine body or a method
    called from that body, it must be marked as &lt;code&gt;suspendable&lt;/code&gt;.
&lt;li&gt;Run a coroutine.
    There are two flavors of this: run a single step, returning as soon as
    one coroutine has run and has returned control to the scheduler,
    or run as many steps as possible,
    until there are no more unblocked coroutines to run.
    I call these two methods
    &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; and &lt;code&gt;runUntilBlockedOrDone&lt;/code&gt;,
    respectively.
    Since these methods are meant to be called from the main application,
    not from within a coroutine, they are not marked as
    &lt;code&gt;suspendable&lt;/code&gt;.
    &lt;br/&gt;&lt;br/&gt;
    When one of the &lt;code&gt;run&lt;/code&gt; methods returns,
    we would like to know whether
    there are some blocked coroutines,
    all of our coroutines have completed,
    or we don't know which because we only ran one routine.
    In order to return this indication, we define a sealed class
    &lt;code&gt;RunStatus&lt;/code&gt; with three case objects representing
    these three possibilities.
&lt;/ol&gt;

The API of our
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/CoScheduler.scala"&gt;&lt;code&gt;CoScheduler&lt;/code&gt;&lt;/a&gt;
trait thus looks like this:

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

sealed abstract class RunStatus
case object RanOneRoutine extends RunStatus
case object SomeRoutinesBlocked extends RunStatus
case object AllRoutinesDone extends RunStatus

trait CoScheduler {
    def addRoutine(name:String)(body: =&amp;gt; Unit @suspendable)

    def waitUntilNotBlocked(b:Blocker):Unit @suspendable

    def runNextUnblockedRoutine():RunStatus
    def runUntilBlockedOrDone():RunStatus
}
&lt;/pre&gt;

If you are interested in using the &lt;code&gt;scoroutine&lt;/code&gt; library
to write coroutines, but you don't care how it works internally,
then you can skip the next few sections and pick up again at
&lt;a href="#producer-consumer"&gt;Producer-Consumer&lt;/a&gt;.

&lt;a name="coscheduler-impl"&gt;&lt;/a&gt;
&lt;h3&gt;CoScheduler Implementation&lt;/h3&gt;

The reason for implementing &lt;code&gt;CoScheduler&lt;/code&gt; as a trait rather
than a class is to make it easier to create multiple implementations.
On the other hand, there is some functionality which is likely to be
the same in all implementations, and since Scala allows us to include
code in traits, we will add the implementation of those methods to the trait.

&lt;br/&gt;&lt;br/&gt;
For example, given the &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; method,
the &lt;code&gt;runUntilBlockedOrDone&lt;/code&gt; method is a simple loop that
calls &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; until it does not run something.

&lt;br/&gt;&lt;br/&gt;
Likewise, if we internally create an instance of a &lt;code&gt;Blocker&lt;/code&gt;
in &lt;code&gt;addRoutine&lt;/code&gt;,
we can implement both that method and &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;
on top of a single method that stores a continuation,
which we will call &lt;code&gt;setRoutineContinuation&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
This means we can create a concrete scheduler class that extends the
&lt;code&gt;CoScheduler&lt;/code&gt; trait by implementing just two functions:
&lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; and
&lt;code&gt;setRoutineContinuation&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Below is what our implementation of &lt;code&gt;CoScheduler&lt;/code&gt; looks like
after making the above changes.

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.util.continuations._

sealed class RunStatus
case object RanOneRoutine extends RunStatus
case object SomeRoutinesBlocked extends RunStatus
case object AllRoutinesDone extends RunStatus

trait CoScheduler { cosched =&amp;gt;
    private[scoroutine] def setRoutineContinuation(
            b:Blocker, cont:Option[Unit=&amp;gt;Unit]):Unit
    def runNextUnblockedRoutine():RunStatus

    /* We use a class rather than an object because we are using the
     * instance as a key to find more info about the associated routine. */
    class BlockerNever() extends Blocker {
        val scheduler = cosched
        val isBlocked = false
    }

    def addRoutine(name:String)(body: =&amp;gt; Unit @suspendable) {
        reset {
            val blocker = new BlockerNever()
            waitUntilNotBlocked(blocker)
            body
        }
    }

    def runUntilBlockedOrDone():RunStatus = {
        var status:RunStatus = RanOneRoutine
        while (status==RanOneRoutine) {
            status = runNextUnblockedRoutine()
        }
        status
    }

    def waitUntilNotBlocked(b:Blocker):Unit @suspendable = {
        shift( (cont: Unit=&amp;gt;Unit) =&amp;gt; {
            setRoutineContinuation(b,Some(cont))
        })
    }
}
&lt;/pre&gt;

As we already knew based on the existence of the
&lt;code&gt;suspendable&lt;/code&gt; annotation, there are two methods that deal
with CPS code: &lt;code&gt;addRoutine&lt;/code&gt; and &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Before examining these functions, it is worth reviewing one point about
how &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; work.
When there is a &lt;code&gt;shift&lt;/code&gt; call within a &lt;code&gt;reset&lt;/code&gt; block,
the CPS compiler plugin transforms the code within the &lt;code&gt;reset&lt;/code&gt;
block such that the code after the &lt;code&gt;shift&lt;/code&gt; block is passed
as a continuation argument to the &lt;code&gt;shift&lt;/code&gt; block.
The code within the &lt;code&gt;shift&lt;/code&gt; block is thus the last code
to be executed within the &lt;code&gt;reset&lt;/code&gt; block.
The code that is within the &lt;code&gt;reset&lt;/code&gt; but outside of the
&lt;code&gt;shift&lt;/code&gt; is CPS code, but the code within the &lt;code&gt;shift&lt;/code&gt;
block is not CPS code.
In other words, once within a &lt;code&gt;reset&lt;/code&gt; block, we are executing
CPS code until we get into the &lt;code&gt;shift&lt;/code&gt; block, at which point
we are no longer executing CPS code.
The continuation contains CPS code; if we call the continuation from
within our &lt;code&gt;shift&lt;/code&gt; code, we transition from non-CPS code
into CPS code.
But if we happen to save the continuation,
we can directly execute it later from any non-CPS code, and that will likewise
transition us into the CPS code of the continuation.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;waitUntilNotBlocked&lt;/code&gt; function takes as an argument the
&lt;code&gt;Blocker&lt;/code&gt; that will tell us when the calling coroutine is
allowed to run again, and, along with the continuation passed in to
the &lt;code&gt;shift&lt;/code&gt;, passes it to
&lt;code&gt;setRoutineContinuation&lt;/code&gt;.
That function saves the continuation and its associated &lt;code&gt;Blocker&lt;/code&gt;
and returns without executing the continuation.
Because we are returning without executing the continuation,
control returns to the first statement past the end of the
enclosing &lt;code&gt;reset&lt;/code&gt; block,
or, if the current code/continuation (i.e. the CPS code containing the call to
&lt;code&gt;waitUntilNotBlocked&lt;/code&gt;) was directly executed from non-CPS code,
to the statement after
the point at which that continuation was executed.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;addRoutine&lt;/code&gt; function creates a &lt;code&gt;Blocker&lt;/code&gt; that
never blocks, then calls &lt;code&gt;waitUntilNotBlocked&lt;/code&gt; with that
blocker.  Since &lt;code&gt;waitUntilNotBlocked&lt;/code&gt; is a CPS function
(marked with the &lt;code&gt;suspendable&lt;/code&gt; annotation),
the remainder of the code in the &lt;code&gt;reset&lt;/code&gt; block is turned
into a continuation and passed along to &lt;code&gt;waitUntilNotBlocked&lt;/code&gt;.
When that method calls &lt;code&gt;shift&lt;/code&gt;, the continuation we passed to
&lt;code&gt;waitUntilNotBlocked&lt;/code&gt; - i.e. our call to &lt;code&gt;body&lt;/code&gt; -
is part of the continuation passed to the &lt;code&gt;shift&lt;/code&gt;.
Thus when that method saves the continuation, that saved continuation
includes the call to the coroutine body.
Since the continuation is not immediately executed, control returns
to the end of the &lt;code&gt;reset&lt;/code&gt; block, and we return from
&lt;code&gt;addRoutine&lt;/code&gt; with our coroutine sitting in the scheduler
ready to start running.

&lt;a name="defaultcoscheduler"&gt;&lt;/a&gt;
&lt;h3&gt;DefaultCoScheduler&lt;/h3&gt;

Given the &lt;code&gt;CoScheduler&lt;/code&gt; trait described above, the only
functionality that remains for our concrete class is
to implement a mechanism for storing continuations
and selecting the next one to run.
The
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/main/scala/net/jimmc/scoroutine/DefaultCoScheduler.scala"&gt;&lt;code&gt;DefaultCoScheduler&lt;/code&gt;&lt;/a&gt;
implements a simple round-robin
scheduling mechanism, selecting the next runnable continuation each
time it is invoked.
Note that this implementation has been designed to be simple, but
is not very efficient.
In particular, it will not exhibit good performance when there are a large
number of coroutines of which only a few are runnable at any time.

&lt;br/&gt;&lt;br/&gt;
We define a case class &lt;code&gt;BlockerInfo&lt;/code&gt; to tie together a
&lt;code&gt;Blocker&lt;/code&gt; and its associated continuation,
an &lt;code&gt;ArrayBuffer&lt;/code&gt; to store an ordered set of those,
and a map to find one given a &lt;code&gt;Blocker&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;setRoutineContinuation&lt;/code&gt; function adds a new
&lt;code&gt;BlockerInfo&lt;/code&gt; to our array if we don't already have one
for the given &lt;code&gt;Blocker&lt;/code&gt;, or updates the existing one if we do.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; function does a simple
linear scan through the array of items, starting just past where we left off
the last time, looking for the first unblocked continuation and
running it.
If there were no runnable continuations, we return a status code
without running anything.

&lt;pre name="hlcode" class="scala"
&gt;package net.jimmc.scoroutine

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.HashMap

class DefaultCoScheduler extends CoScheduler {
    val blockerIndexMap = new HashMap[Blocker,Int]
    case class BlockerInfo(val blocker:Blocker, index:Int,
            var cont:Option[Unit=&amp;gt;Unit])
    val blockerList = new ArrayBuffer[BlockerInfo]
    var nextIndex = 0

    private[scoroutine] def setRoutineContinuation(
            b:Blocker,cont:Option[Unit=&amp;gt;Unit]) {
        if (blockerIndexMap.get(b).isEmpty) {
            val nextIndex = blockerIndexMap.size
            blockerIndexMap.put(b,nextIndex)
            blockerList += BlockerInfo(b, nextIndex, cont)
        } else {
            val n = blockerIndexMap(b)
            blockerList(n).cont = cont
        }
    }

    def runNextUnblockedRoutine():RunStatus = {
        var blockedCount = 0
        for (i &amp;lt;- 0 until blockerList.size) {
            val index = (nextIndex + i) % blockerList.size
            val bInfo = blockerList(index)
            if (bInfo.cont.isDefined &amp;&amp; bInfo.blocker.isBlocked) {
                blockedCount += 1
            }
            if (bInfo.cont.isDefined &amp;&amp; !bInfo.blocker.isBlocked) {
                nextIndex = index + 1
                val nextCont = bInfo.cont
                bInfo.cont = None
                nextCont.get()          //run the continuation
                return RanOneRoutine
            }
        }
        if (blockedCount &amp;gt; 0) {
            SomeRoutinesBlocked
        } else {
            AllRoutinesDone
        }
    }
}
&lt;/pre&gt;

Note that &lt;code&gt;DefaultCoScheduler&lt;/code&gt; does not import
the continuations package.
It does not use &lt;code&gt;reset&lt;/code&gt;, &lt;code&gt;shift&lt;/code&gt;,
or any of the CPS annotations such as &lt;code&gt;suspendable&lt;/code&gt;.
This is because none of this code is CPS code.
&lt;code&gt;runNextUnblockedRoutine&lt;/code&gt; is called from non-CPS code,
and although &lt;code&gt;setRoutineContinuation&lt;/code&gt; is called from
CPS code, it does not itself call any CPS functions nor does it use
&lt;code&gt;shift&lt;/code&gt;, so it does not need to be declared as CPS code.

&lt;a name="other-schedulers"&gt;&lt;/a&gt;
&lt;h3&gt;Other Schedulers&lt;/h3&gt;

&lt;code&gt;DefaultCoScheduler&lt;/code&gt; implements a basic scheduling algorithm.
It is intended for use with small numbers of coroutines and has not
been optimized.
Other schedulers could be written that are optimized for other
situations, such as large numbers of coroutines,
coroutines with different priorities,
or "stickiness" so that a running coroutine continues to run
until it is blocked before the next coroutine runs.
Since the code that creates the coroutines starts by creating
the scheduler that controls those coroutines,
it would be simple to create a scheduler other than
&lt;code&gt;DefaultCoScheduler&lt;/code&gt; for use with a particular
set of coroutines.

&lt;a name="producer-consumer"&gt;&lt;/a&gt;
&lt;h3&gt;Producer-Consumer&lt;/h3&gt;

Let's see how the Producer-Consumer example
(&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/ProdConTest.scala"&gt;&lt;code&gt;ProdConTest&lt;/code&gt;&lt;/a&gt;)
looks using the
&lt;code&gt;scoroutine&lt;/code&gt; library:

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.continuations._

import net.jimmc.scoroutine.DefaultCoScheduler
import net.jimmc.scoroutine.CoQueue

object ProdConTest {
    def main(args:Array[String]) = {
        val prodcon = new ProduceAndConsume()
        prodcon.run
    }
}

class ProdCon() {
    val sched = new DefaultCoScheduler
    val buf = new CoQueue[Int](sched,2)

    def run() {
        sched.addRoutine("producer"){
            var i = 0
            while (i &amp;lt; 4) {
                buf.blockingEnqueue(i)
            }
        }
        sched.addRoutine("consumer"){
            val total = buf.blockingDequeue +
                    buf.blockingDequeue + buf.blockingDequeue
            println("consume total is "+total)
        }
        sched.runUntilBlockedOrDone
    }
}
&lt;/pre&gt;

After the imports and a simple &lt;code&gt;main&lt;/code&gt; method
for testing, we have the &lt;code&gt;ProdCon&lt;/code&gt; class with the
actual producer-consumer definition.

&lt;br/&gt;&lt;br/&gt;
We start by setting up two objects:
the scheduler that will control our two coroutines,
and a queue for communication between them.
We then register two coroutines with our scheduler,
one for the producer and one for the consumer, and we
call &lt;code&gt;sched.runUntilBlockedOrDone&lt;/code&gt;
to run the coroutines until
there is nothing runnable left on that scheduler.

&lt;br/&gt;&lt;br/&gt;
You can't tell in this example, but the code blocks being passed
to &lt;code&gt;addRoutine&lt;/code&gt; are CPS code, the same as the body of
a &lt;code&gt;reset&lt;/code&gt; block.
If you decide to refactor this code and push the
&lt;code&gt;blockingEnqueue&lt;/code&gt; or &lt;code&gt;blockingDequeue&lt;/code&gt; calls down into a
subroutine, that subroutine will have to be marked with the
&lt;code&gt;suspendable&lt;/code&gt; annotation.
Also, because coroutine bodies are CPS code, there are
there are some &lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#control_construct_restrictions"&gt;restrictions&lt;/a&gt;
on what control constructs can be used.
You can see what this looks like in the
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala/ProdConTestWithSubs.scala"&gt;
&lt;code&gt;ProdConTestWithSubs&lt;/code&gt;&lt;/a&gt; source code in the
&lt;a href="http://github.com/jimmc/scoroutine/tree/master/src/example/scala"&gt;
scoroutine examples&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
I will give some more examples in my next post,
a followup about Generators.

&lt;a name="resources"&gt;&lt;/a&gt;
&lt;h3&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;My original blog post on
    &lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html"&gt;
    Delimited Continuations&lt;/a&gt;,
    which includes its own
    &lt;a href="http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html#resources"&gt;
    Resources&lt;/a&gt; section.
&lt;li&gt;The &lt;a href="http://github.com/jimmc/scoroutine"&gt;net.jimmc.scoroutine&lt;/a&gt; library on github.
&lt;li&gt;&lt;a href="http://code.google.com/p/python-multitask/"&gt;python-multitask&lt;/a&gt;,
    a similar framework for Python.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-8501641801714740144?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/8501641801714740144/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=8501641801714740144' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8501641801714740144'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8501641801714740144'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/09/scala-coroutines.html' title='Scala Coroutines'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-1492763927689974370</id><published>2010-08-08T14:08:00.000-07:00</published><updated>2010-09-26T19:50:40.625-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Delimited Continuations</title><content type='html'>Scala's delimited continuations, introduced in version 2.8,
can be used to implement all sorts of interesting control constructs.

&lt;br/&gt;&lt;br/&gt;
This is a very long blog post.
It took me quite a while to get my head around Scala's &lt;code&gt;reset&lt;/code&gt;
and &lt;code&gt;shift&lt;/code&gt; operators.
To help others hopefully avoid the stumbling blocks I encountered,
I have tried here to start with the basics and build up from there
in some detail.
If you want a shorter explanation, see the
&lt;a href="#resources"&gt;Resources&lt;/a&gt; section at the end of this post
for pointers to some other blog entries that are more succinct.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#mechanics"&gt;Mechanics&lt;/a&gt;
&lt;li&gt;&lt;a href="#cps"&gt;Continuation Passing Style (CPS)&lt;/a&gt;
&lt;li&gt;&lt;a href="#nested_cps"&gt;Nested CPS&lt;/a&gt;
&lt;li&gt;&lt;a href="#full_vs_delimited"&gt;Full versus Delimited Continuations&lt;/a&gt;
&lt;li&gt;&lt;a href="#uses"&gt;Uses&lt;/a&gt;
&lt;li&gt;&lt;a href="#cps_with_return"&gt;CPS With Return&lt;/a&gt;
&lt;li&gt;&lt;a href="#reset_and_shift"&gt;Reset and Shift&lt;/a&gt;
&lt;li&gt;&lt;a href="#annotations"&gt;Annotations&lt;/a&gt;
&lt;li&gt;&lt;a href="#nested_shift"&gt;Nested Shift&lt;/a&gt;
&lt;li&gt;&lt;a href="#control_construct_restrictions"&gt;Control Construct Restrictions&lt;/a&gt;
&lt;li&gt;&lt;a href="#advice"&gt;Advice&lt;/a&gt;
&lt;li&gt;&lt;a href="#resources"&gt;Resources&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="mechanics"&gt;&lt;/a&gt;
&lt;h3&gt;Mechanics&lt;/h3&gt;

In order to use Scala's delimited continuations,
you must use version 2.8,
and you must use the continuations (or CPS) compiler plugin.
You do this by specifying a command line option when running
both the compiler and the runtime:

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax"
    onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="bash"
&gt;$ scalac -P:continuations:enable ${sourcefiles}
$ scala -P:continuations:enable ${classname}
&lt;/pre&gt;

In your source code, you must import the appropriate continuations elements,
which you can do most simply by using a wildcard to import everything:

&lt;pre name="hlcode" class="scala"
&gt;import scala.util.continuations._
&lt;/pre&gt;

If you forget to do the import you will get an error message similar to this:

&lt;pre name="hlcode" class="scala"
&gt;&amp;lt;console&amp;gt;:6: error: not found: value reset
       reset {
       ^
&lt;/pre&gt;


&lt;a name="cps"&gt;&lt;/a&gt;
&lt;h3&gt;Continuation Passing Style (CPS)&lt;/h3&gt;

In order to understand how Scala's delimited continuations work,
you have to understand the "continuation passing style", or
&lt;a href="http://en.wikipedia.org/wiki/Continuation-passing_style"&gt;CPS&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
Consider this code in which a method makes a subroutine call:

&lt;br/&gt;
&lt;pre name="hlcode" class="scala"
&gt;def main {
    pre
    sub()
    post
}
def sub() {
    substuff
}
&lt;/pre&gt;
where &lt;code&gt;pre&lt;/code&gt; and &lt;code&gt;post&lt;/code&gt; represent all of the code
in &lt;code&gt;main&lt;/code&gt; respectively
before and after the call to &lt;code&gt;sub&lt;/code&gt;,
and &lt;code&gt;substuff&lt;/code&gt; represents all of the code in &lt;code&gt;sub&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
When the &lt;code&gt;sub&lt;/code&gt; method gets called, the system, in effect,
instructs the processor to execute the &lt;code&gt;sub&lt;/code&gt; code, then to continue
execution within &lt;code&gt;main&lt;/code&gt; immediately after the call to
&lt;code&gt;sub&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
We can conceptually refactor the code in &lt;code&gt;main&lt;/code&gt; so that
all of the stuff in &lt;code&gt;pre&lt;/code&gt; is in a separate method, and
all of the &lt;code&gt;post&lt;/code&gt; stuff is in a separate method.
We can further refactor the code so that each section (pre, sub, post)
takes in all of its input data as arguments and
passes all of its data changes out as an aggregate return value
(such as a &lt;code&gt;Map&lt;/code&gt; or &lt;code&gt;Tuple&lt;/code&gt;)
of the method for that section.
Adding arguments and return value to &lt;code&gt;main&lt;/code&gt;,
we have something that looks like this:

&lt;pre name="hlcode" class="scala"
&gt;def main(m:M):Z = {
    val x:X = pre(m)
    val y:Y = sub(m,x)
    val z:Z = post(m,x,y)
    return z
}
def sub(m:M,x:X):Y {
    val y:Y = substuff(m,x)
    return y
}
&lt;/pre&gt;

Now, instead of the system automatically continuing execution at
&lt;code&gt;post&lt;/code&gt; after finishing &lt;code&gt;sub&lt;/code&gt;, let's make
that explicit in our code by passing the chunk of code that calls
&lt;code&gt;post&lt;/code&gt; as an extra argument to &lt;code&gt;sub&lt;/code&gt;.
We will then modify &lt;code&gt;sub&lt;/code&gt; so that, after doing all of its
calculations and generating the values it would have returned to
&lt;code&gt;main&lt;/code&gt; as &lt;code&gt;y&lt;/code&gt;, it instead calls &lt;code&gt;post&lt;/code&gt;
with its arguments as specified, and returns as its own value the
return value of &lt;code&gt;post&lt;/code&gt;, which is &lt;code&gt;z&lt;/code&gt;
in &lt;code&gt;main&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;def main(m:M) {
    val x:X = pre(m)
    val z:Z = sub(m,x, { post(m,x,_) } )
    return z
}
def sub(m:M,x:X, subCont: (Y) =&amp;gt; Z) {
    val y:Y = substuff(m,x)
    val z:Z = subCont(y)
    return z
}
&lt;/pre&gt;

When we pass the code fragment containing &lt;code&gt;post&lt;/code&gt; to &lt;code&gt;sub&lt;/code&gt;,
Scala generates a closure
that captures the values available to &lt;code&gt;post&lt;/code&gt; at that point,
including &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;x&lt;/code&gt;, so that when that closure
is evaluated later it can get those values.

&lt;br/&gt;&lt;br/&gt;
Note that the &lt;code&gt;main&lt;/code&gt; method no longer sees &lt;code&gt;y&lt;/code&gt;,
the original return value from &lt;code&gt;sub&lt;/code&gt;, so it can't be explicitly
passed to &lt;code&gt;post&lt;/code&gt;; instead, we use a placeholder, which is
filled in by the code in &lt;code&gt;sub&lt;/code&gt; that calls
&lt;code&gt;post&lt;/code&gt;.
We can rewrite that line to use the more explicit function syntax
(where, for convenience, we use &lt;code&gt;y&lt;/code&gt; as our parameter name):

&lt;pre name="hlcode" class="scala"
&gt;    val z:Z = sub(m,x, { (y:Y) =&amp;gt; post(m,x,y) } )
&lt;/pre&gt;

The gist of CPS is that we don't use &lt;code&gt;return&lt;/code&gt;.
Rather than calling a subroutine and having it return to us,
as is the case in the normal Direct Style,
we pass a continuation to the subroutine for it to execute when it is done.

&lt;a name="nested_cps"&gt;&lt;/a&gt;
&lt;h3&gt;Nested CPS&lt;/h3&gt;

In the above example we have only taken the first step in converting to CPS.
To be able to take advantage of CPS, we need to complete the transformation.

&lt;br/&gt;&lt;br/&gt;
At the top, our &lt;code&gt;main&lt;/code&gt; method is still returning a value.
Since we have no &lt;code&gt;return&lt;/code&gt; in CPS, how do we handle this?
The answer is that the topmost level can not return a value.
Let's add a top-level wrapper like this:

&lt;pre name="hlcode" class="scala"
&gt;def prog(m:M) {
    val z:Z = main(m)
    println(z)
    System.exit(z.exitValue)
}
&lt;/pre&gt;

Now we can make the same CPS transformation on
&lt;code&gt;prog&lt;/code&gt; and &lt;code&gt;main&lt;/code&gt; as we did before on
&lt;code&gt;main&lt;/code&gt; and &lt;code&gt;sub&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;def prog(m:M) {
    main(m, { (z:Z) =&amp;gt;
        println(z)
        System.exit(z.exitValue)
    })
}

def main(m:M, mainCont:(Z)=&amp;gt;Unit):Unit = {
    val x:X = pre(m)
    val z:Z = sub(m,x, { (y:Y) =&amp;gt; post(m,x,y) } )
    mainCont(z)
}
&lt;/pre&gt;

We are still using a &lt;code&gt;return&lt;/code&gt; statement in &lt;code&gt;sub&lt;/code&gt;,
with code in &lt;code&gt;main&lt;/code&gt; following the return from &lt;code&gt;sub&lt;/code&gt;.
To fix this, we need to push the &lt;code&gt;mainCont&lt;/code&gt;
in &lt;code&gt;main&lt;/code&gt; into the
continuation we pass to &lt;code&gt;sub&lt;/code&gt;.
We modify both &lt;code&gt;main&lt;/code&gt; and &lt;code&gt;sub&lt;/code&gt; to do this:

&lt;pre name="hlcode" class="scala"
&gt;def main(m:M, mainCont:(Z)=&amp;gt;Unit):Unit = {
    val x:X = pre(m)
    sub(m,x, { (y:Y) =&amp;gt; {
        val z:Z = post(m,x,y) } )
        mainCont(z)
    })
}

def sub(m:M,x:X, subCont: (Y) =&amp;gt; Unit) {
    val y:Y = substuff(m,x)
    subCont(y)
}
&lt;/pre&gt;

We have now threaded our top-level continuation - the one that includes
the call to &lt;code&gt;System.exit&lt;/code&gt; - all the way down to &lt;code&gt;sub&lt;/code&gt;,
so when we execute the &lt;code&gt;subCont&lt;/code&gt; in &lt;code&gt;sub&lt;/code&gt;,
it will first execute the &lt;code&gt;post&lt;/code&gt; method with the code in
&lt;code&gt;main&lt;/code&gt; that originally appeared after &lt;code&gt;sub&lt;/code&gt;,
then it will execute the code in &lt;code&gt;prog&lt;/code&gt; that originally
appeared after the call to &lt;code&gt;main&lt;/code&gt;, which will call
&lt;code&gt;println&lt;/code&gt; and then exit the program by calling
&lt;code&gt;System.exit&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
If we wanted to convert &lt;code&gt;substuff&lt;/code&gt; to CPS, we would apply the
same transformation to it and &lt;code&gt;sub&lt;/code&gt;, after which the call
from &lt;code&gt;sub&lt;/code&gt; to &lt;code&gt;substuff&lt;/code&gt; would pass an additional
argument which was the continuation of the rest of &lt;code&gt;sub&lt;/code&gt;,
which includes the continuation passed from &lt;code&gt;main&lt;/code&gt; to
&lt;code&gt;sub&lt;/code&gt;, which in turn includes the continuation passed 
from &lt;code&gt;prog&lt;/code&gt; into &lt;code&gt;main&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
As you can see, each continuation that we pass down to another subroutine
always includes the continuations for all of the callers.
In other words, every continuation includes all of the rest of the
program to be executed after the called subroutine is done.
The other important point is that in every method where we call a
subroutine using CPS,
that call is always the very last thing in the method.

&lt;a name="full_vs_delimited"&gt;&lt;/a&gt;
&lt;h3&gt;Full versus Delimited Continuations&lt;/h3&gt;

In the discussion above we have assumed that the entire program is
converted over to CPS.
This is the classical definition of continuations,
which can be referred to as full continuations.
However, using CPS in languages (such as Scala) that were not
specifically designed for it
can be awkward, so it would be nicer if we could restrict the use of
CPS to the specific areas in our code where we want to use it.

&lt;br/&gt;&lt;br/&gt;
This is exactly the intent of a delimited continuation.
Rather than attempting to capture the entire remainder of the
program execution in a continuation, we only capture the remaining execution
of the program up to a specified point.

&lt;br/&gt;&lt;br/&gt;
If we reexamine the start of our sample program, the &lt;code&gt;prog&lt;/code&gt; method,
we see that the only difference between it and any arbitrary method
is that we can't return a Direct Style value from it.
If we remove the call to &lt;code&gt;System.exit&lt;/code&gt;,
we can call &lt;code&gt;prog&lt;/code&gt; from normal Direct Style code,
with CPS being used within
&lt;code&gt;prog&lt;/code&gt; and all of its converted subroutines.
Program execution within the CPS code  proceeds normally using CPS,
each method ending by passing a continuation along to the next method.
After the last continuation is finally executed, the CPS code is done
and control returns to the caller of &lt;code&gt;prog&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;def prog(m:M) {
    main(m, { (z:Z) =&amp;gt;
        println(z)
    })
}
&lt;/pre&gt;

&lt;a name="uses"&gt;&lt;/a&gt;
&lt;h3&gt;Uses&lt;/h3&gt;

We have gone to a lot of trouble to restructure our code to use CPS
while keeping the functionality the same.
Now we can examine how we can
make changes to the code that are only possible because it uses CPS.

&lt;br/&gt;&lt;br/&gt;
The key ability that CPS gives us is that we have an explicit object
(the continuation) representing the remainder of execution of our
program (or, in the case of a delimited continuation, of a portion
of our program).
In the code sample above, we executed that continuation once we reached
the end of the line in &lt;code&gt;sub&lt;/code&gt;.
But what would happen if, instead of executing the continuation at that point,
we just saved it somewhere, such as into a singleton?

&lt;pre name="hlcode" class="scala"
&gt;object ContinuationSaver {
    var savedContinuation:Option[()=&amp;gt;Unit] = None
    def save(saveCont: =&amp;gt;Unit) = savedContinuation = Some(saveCont _)
}
def sub(m:M,x:X, subCont: (Y) =&amp;gt; Unit) {
    val y:Y = substuff(m,x)
    ContinuationSaver.save { subCont(y) }
}
&lt;/pre&gt;

After &lt;code&gt;sub&lt;/code&gt; saves the continuation, it is done, and in fact
the entire delimited continuation is done; control returns to the
caller of &lt;code&gt;prog&lt;/code&gt;.
But in ContinuationSaver
we still have the continuation that represents execution of the
remainder of that portion of the program, which we can execute later.
In effect, we have placed the execution of that code into suspended animation,
to be revived at some later time of our choosing.

&lt;br/&gt;&lt;br/&gt;
Not only can we call the continuation later, we can call it multiple times.
We can also write a more sophisticated ContinuationSaver that can save
multiple continuations and keep track of which ones we should execute later,
including the order and whether to call them multiple times.
We can even save the continuations to persistent storage
or move them to another computer,
as is done by
&lt;a href="http://www.scala-lang.org/node/3485"&gt;Swarm&lt;/a&gt;.

&lt;a name="cps_with_return"&gt;&lt;/a&gt;
&lt;h3&gt;CPS With Return&lt;/h3&gt;

In pure CPS, there are no returns.
But code in Scala does return, even when we are using CPS.
In the previous section I used the phrase
"control returns to the caller of &lt;code&gt;prog&lt;/code&gt;."
This happens in the normal way,
by having each of the intervening methods return to its caller
until the stack unwinds to the first CPS call.
I have assumed that each CPS method returns no value (&lt;code&gt;Unit&lt;/code&gt;),
but there is nothing preventing us from adding code to each
method in the transformed CPS chain to make it return a value.

&lt;br/&gt;&lt;br/&gt;
The examples above demonstrate a transformation from
Direct Style code to CPS code, and that transformation always results
in code that returns &lt;code&gt;Unit&lt;/code&gt;.
If we add a return value to
the transformed code, this is not something we can get as a result of using
the above transformation technique.

&lt;br/&gt;&lt;br/&gt;
What happens if we add a return value to our CPS code?
In our examples above, the execution of the continuation was always
the last thing in the subroutine.
If we keep this as our default behavior, then when we change the CPS
methods to return a value, the return value from the last CPS method
in a chain of continuations will propagate back up through
the chain of CPS callers all the way out to the topmost CPS method,
and will appear to the Direct Style code as the value of that outermost method.
Of course, one of the intervening CPS method might modify or replace
that value as it is being returned through it.

&lt;br/&gt;&lt;br/&gt;
For example, let's take the most recent version of &lt;code&gt;sub&lt;/code&gt; above
(the one that saves the continuation for later execution)
and make it return an Int value:

&lt;pre name="hlcode" class="scala"
&gt;object ContinuationSaver {
    var numberOfSavedContinuations = 0
    var savedContinuation:Option[()=&amp;gt;Unit] = None
    def save(saveCont: =&amp;gt;Unit):Int = {
        savedContinuation = saveCont _
        numberOfSavedContinuations = numberOfSavedContinuations + 1
        numberOfSavedContinuations
    }
}
def sub(m:M,x:X, subCont: (Y) =&amp;gt; Unit):Int = {
    val y:Y = substuff(m,x)
    ContinuationSaver.save { subCont(y) }
}
&lt;/pre&gt;

We also change the rest of the methods in our calling chain to allow us to
propagate this value all the way out.
Since the call to &lt;code&gt;sub&lt;/code&gt; is the last call in &lt;code&gt;main&lt;/code&gt;,
all we need to do is change the return type on &lt;code&gt;main&lt;/code&gt; to
match the return type of &lt;code&gt;sub&lt;/code&gt;.
Likewise, since the call to &lt;code&gt;main&lt;/code&gt; is the last call in
&lt;code&gt;prog&lt;/code&gt;, we change the return type of &lt;code&gt;prog&lt;/code&gt;
to match the return type of &lt;code&gt;main&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;def prog(m:M):Int = {
    main(m, { (z:Z) =&amp;gt;
        println(z)
    })
}

def main(m:M, mainCont:(Z)=&amp;gt;Unit):Int = {
    val x:X = pre(m)
    sub(m,x, { (y:Y) =&amp;gt; {
        val z:Z = post(m,x,y) } )
        mainCont(z)
    })
}
&lt;/pre&gt;

We could, if we wanted to, modify &lt;code&gt;main&lt;/code&gt; to make a change
to the value returned by &lt;code&gt;sub&lt;/code&gt; before passing it back
as its own return value, or we could make
&lt;code&gt;main&lt;/code&gt; return something else entirely.

&lt;br/&gt;&lt;br/&gt;
If you think about the CPS code as having been created by transforming
some Direct Style code, you can see that the untransformed code had
its original return type, and the now-CPS transformed code has a
(potentially different) transformed return type.

&lt;a name="reset_and_shift"&gt;&lt;/a&gt;
&lt;h3&gt;Reset and Shift&lt;/h3&gt;

Finally, we have enough background to understand Scala's
&lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; keywords.

&lt;br/&gt;&lt;br/&gt;
The Scala implementation of delimited continuations was created by
Tiark Rompf of EPFL, and is described in his explanatory paper on
&lt;a href="http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf"&gt;
Delimited Continuations in Scala&lt;/a&gt;
with co-authors Ingo Maier and Martin Odersky.
There are also some quotes below from some of Tiark's posts.

&lt;br/&gt;&lt;br/&gt;
&lt;code&gt;Reset&lt;/code&gt; is the keyword that demarcates the limits of the delimited
continuation.
Within the body of the &lt;code&gt;reset&lt;/code&gt;, the code is CPS code;
the return value of &lt;code&gt;reset&lt;/code&gt; is not CPS.

&lt;br/&gt;&lt;br/&gt;
&lt;code&gt;Shift&lt;/code&gt; is the keyword that indicates the bottoming out
of the CPS path.
The body of the &lt;code&gt;shift&lt;/code&gt; is not CPS code, but it's
untransformed return value is CPS.
The &lt;code&gt;shift&lt;/code&gt; call gets passed as its argument
the continuation that has been
collected from all of the callers out to the (dynamically)
enclosing &lt;code&gt;reset&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
&lt;code&gt;Reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;
are thus the keywords that take you from Direct Style
to CPS, and from CPS to Direct Style, respectively.
All of the code between &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; is CPS.
Any method that includes &lt;code&gt;shift&lt;/code&gt; must be marked as CPS,
and any method that calls a CPS method
must be marked as CPS, until you reach the enclosing &lt;code&gt;reset&lt;/code&gt; call.

&lt;br/&gt;&lt;br/&gt;
When you use &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; in your code,
the continuations compiler plugin transforms your code in a manner
similar to the CPS transformation I described above.
All of the code from the end of the &lt;code&gt;shift&lt;/code&gt; block
to the end of the enclosing method or &lt;code&gt;reset&lt;/code&gt; block is
packaged up as a closure and passed to the body of the
&lt;code&gt;shift&lt;/code&gt; block as the continuation function.

&lt;br/&gt;&lt;br/&gt;
Let's break down some examples
of &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; in Scala.

&lt;pre name="hlcode" class="scala"
&gt;reset {
  shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(7)
  } + 1
}
&lt;/pre&gt;

The &lt;code&gt;shift&lt;/code&gt; statement tells the compiler plugin to
restructure the code as in our CPS examples,
by converting the code after the &lt;code&gt;shift&lt;/code&gt; call into a
continuation that gets passed as an argument to the &lt;code&gt;shift&lt;/code&gt;.
To make it easier to see what that means in this case, let's do
that code transformation in a few steps.

&lt;br/&gt;&lt;br/&gt;
First, we assign the result of the &lt;code&gt;shift&lt;/code&gt; call to a variable
and use that variable later in the code:

&lt;pre name="hlcode" class="scala"
&gt;reset {
  var r = shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(7)
  }
  r + 1
}
&lt;/pre&gt;

Second, we convert all of the code following the &lt;code&gt;shift&lt;/code&gt;
into a function and call it:

&lt;pre name="hlcode" class="scala"
&gt;reset {
  var r = shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(7)
  }
  def f(x:Int) = x + 1
  f(r)
}
&lt;/pre&gt;

The function &lt;code&gt;f&lt;/code&gt; is our continuation function
that represents all of the code between the end of the &lt;code&gt;shift&lt;/code&gt;
block and the end of the enclosing &lt;code&gt;reset&lt;/code&gt; block.
Finally, we transform the code as is done by the compiler plugin,
binding our continuation function &lt;code&gt;f(x)&lt;/code&gt; to the &lt;code&gt;shift&lt;/code&gt;
parameter &lt;code&gt;k&lt;/code&gt;, and making the return value of the fully
transformed code be the return value of the body of the &lt;code&gt;shift&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;reset {
  def f(x:Int) = x + 1
  f(7)
}
&lt;/pre&gt;

Now we can easily see that the return value is 8.

&lt;br/&gt;&lt;br/&gt;
We can apply the same transformations to

&lt;pre name="hlcode" class="scala"
&gt;reset {
  shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(k(k(7)))
  } + 1
}
&lt;/pre&gt;

to get

&lt;pre name="hlcode" class="scala"
&gt;reset {
  def f(x:Int) = x + 1
  f(f(f(7)))
}
&lt;/pre&gt;

from which we can quickly calculate that this will return a value of 10.

&lt;br/&gt;&lt;br/&gt;
All of our transformations have no effect on anything outside of
the &lt;code&gt;reset&lt;/code&gt;; for example,

&lt;pre name="hlcode" class="scala"
&gt;reset {
  shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(7)
  } + 1
} * 2
&lt;/pre&gt;

just multiplies the return value of the &lt;code&gt;reset&lt;/code&gt; expression by 2,
so the result of this code snippet would be 16.

&lt;br/&gt;&lt;br/&gt;
Tiark's paper gives this interesting example:

&lt;pre name="hlcode" class="scala"
&gt;reset {
  shift { k: (Int=&amp;gt;Int) =&amp;gt;
    k(k(k(7))); "done"
  } + 1
}
&lt;/pre&gt;

and points out that the value of this code snippet is "done".
The continuation function &lt;code&gt;k&lt;/code&gt; is called three times,
but the value of that expression is discarded.
If we apply our code transformations as before, we see that this
transforms into:

&lt;pre name="hlcode" class="scala"
&gt;reset {
  def f(x:Int) = x + 1
  f(f(f(7))); "done"
}
&lt;/pre&gt;

which makes it more obvious why the result of this code snippet is "done".

&lt;br/&gt;&lt;br/&gt;
A key detail to note here is that the value of the evaluated &lt;code&gt;reset&lt;/code&gt;
block is &lt;i&gt;not&lt;/i&gt; the value of the last expression in that block,
as it is in most code.
Instead, the value of the evaluated &lt;code&gt;reset&lt;/code&gt; block is the value
of the last expression in the &lt;code&gt;shift&lt;/code&gt; block that gets
executed within that &lt;code&gt;reset&lt;/code&gt; block.
Execution of the body of the &lt;code&gt;shift&lt;/code&gt; is always the last thing
that happens within the enclosing &lt;code&gt;reset&lt;/code&gt; block.

&lt;br/&gt;&lt;br/&gt;
When you look at a &lt;code&gt;shift&lt;/code&gt; block and see its return value
being used in an expression, as in the "shift + 1" examples above,
remember that, due to code transformation, that "return" from the
&lt;code&gt;shift&lt;/code&gt; block never actually happens as a return.
Instead, once execution reaches the &lt;code&gt;shift&lt;/code&gt; block,
the code after that block gets passed to it as a continuation;
if the code in the &lt;code&gt;shift&lt;/code&gt; block calls the continuation,
the value which is passed as an argument to the continuation
appears as the value being returned from the &lt;code&gt;shift&lt;/code&gt; block.
Thus the type of the argument passed to the
&lt;code&gt;shift&lt;/code&gt; block's continuation function is the same
as the type of the return value of the &lt;code&gt;shift&lt;/code&gt; in the source
code,
and the type of the return value of that continuation function is the
same as the type of the return value of the original last value in
the &lt;code&gt;reset&lt;/code&gt; block that encloses the &lt;code&gt;shift&lt;/code&gt; block.

&lt;a name="reset_shift_types"&gt;&lt;/a&gt;
&lt;br/&gt;&lt;br/&gt;
There are thus three types associated with &lt;code&gt;shift&lt;/code&gt;:
&lt;ul&gt;
&lt;li&gt;The type of the argument to pass to the continuation, which is the
    same as the syntactic return type of the &lt;code&gt;shift&lt;/code&gt;
    in the source code.
&lt;li&gt;The type of the return from the continuation, which is the same
    as the return type of all of the code that follows the &lt;code&gt;shift&lt;/code&gt;
    block in the source code (i.e. the type of the last value in the
    block of code between the &lt;code&gt;shift&lt;/code&gt; block and the end of
    the function or &lt;code&gt;reset&lt;/code&gt; block
    containing the &lt;code&gt;shift&lt;/code&gt; block).
    This is called the untransformed return type.
&lt;li&gt;The type of the last value in the &lt;code&gt;shift&lt;/code&gt; block,
    which becomes the type of the return value of the enclosing function or
    &lt;code&gt;return&lt;/code&gt; block.
    This is called the transformed return type.
&lt;/ul&gt;

In the signature for &lt;code&gt;shift&lt;/code&gt;, the above three types appear as
&lt;code&gt;A&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;, respectively:

&lt;pre name="hlcode" class="scala"
&gt;def shift[A, B, C](fun: ((A) =&amp;gt; B) =&amp;gt; C): A @scala.util.continuations.cpsParam[B,C]
&lt;/pre&gt;

The two types in the &lt;code&gt;cpsParam&lt;/code&gt; annotation always
represent the untransformed and the transformed return types, respectively.
The CPS annotations are described in more detail
&lt;a href="#annotations"&gt;below&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
The signature for &lt;code&gt;reset&lt;/code&gt; only uses two types:
the first type is the untransformed type of the code block passed to
&lt;code&gt;reset&lt;/code&gt;, which matches the B type of &lt;code&gt;shift&lt;/code&gt;,
and the second type is the type of the transformed code block, which
matches the C type of &lt;code&gt;shift&lt;/code&gt;, and is also the real return
type of the &lt;code&gt;reset&lt;/code&gt; block to its caller.
The scaladoc for &lt;code&gt;reset&lt;/code&gt; uses parameter type names A and C,
but I write it here using B and C so that the signature of the
&lt;code&gt;ctx&lt;/code&gt; by-name parameter matches the signature of
the return value of &lt;code&gt;shift&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;def reset[B, C](ctx: =&amp;gt; B @scala.util.continuations.cpsParam[B,C]): C   
&lt;/pre&gt;

Here's where those types appear:
&lt;pre name="hlcode" class="scala"
&gt;C = reset { ...; A = shift { k:(A=&amp;gt;B) =&amp;gt; ...; C } ...; B }  
&lt;/pre&gt;

In the following example, A=Int, B=String and C=Boolean:

&lt;pre name="hlcode" class="scala"
&gt;def is123(n:Int):Boolean = {
  reset {
    shift { k : (Int=&amp;gt;String) =&amp;gt;
      (k(n) == "123")
    }.toString
  }
}
&lt;/pre&gt;

&lt;a name="annotations"&gt;&lt;/a&gt;
&lt;h3&gt;Annotations&lt;/h3&gt;

As you saw above, the signatures for &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;
include the &lt;code&gt;cpsParam&lt;/code&gt; annotation.
The compiler plugin uses this type annotation to select what pieces of code
to transform to CPS;
in Tiark's paper this is referred to as a
"type-directed selective CPS transform."
If you just use &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt; without any
subroutine calls, you may never need to explicitly use a CPS annotation.
But if you put any &lt;code&gt;shift&lt;/code&gt; calls into subroutines,
as described &lt;a href="#nested_shift"&gt;below&lt;/a&gt;,
then you will need to use a CPS annotation.

&lt;br/&gt;&lt;br/&gt;
The base annotation is &lt;code&gt;cpsParam[-B, +C]&lt;/code&gt;.
This annotation tells the compiler that the corresponding block of code
has an untransformed return value of type &lt;code&gt;B&lt;/code&gt; and
a transformed return value of type &lt;code&gt;C&lt;/code&gt;,
as described in the discussion of the types for &lt;code&gt;reset&lt;/code&gt;
and &lt;code&gt;shift&lt;/code&gt; &lt;a href="#reset_shift_types"&gt;above&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
To simplify the annotation for the common case where the transformed
return type is the same as the untransformed type, the continuations
package defines the convenience type &lt;code&gt;cps&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;type cps[A] = cpsParam[A, A]
&lt;/pre&gt;

If you are looking at old posts on the web,
be aware that the &lt;code&gt;cpsParam&lt;/code&gt; annotation used to be called
simply &lt;code&gt;cps&lt;/code&gt;;
the old &lt;code&gt;cps&lt;/code&gt; annotation was renamed to &lt;code&gt;cpsParam&lt;/code&gt;
and the new one-type-parameter &lt;code&gt;cps&lt;/code&gt; type alias was added.

&lt;br/&gt;&lt;br/&gt;
In the &lt;a href="#uses"&gt;Uses&lt;/a&gt; section above we discussed the possibility
of saving away the continuation for later execution,
after which control returns to the caller.
If we do this, we can't return a value from the suspended code to the
original caller, since that code has not been executed yet,
and the eventual executor of the continuation may not know where it
came from, so it too is likely not to care about a return value.

&lt;br/&gt;&lt;br/&gt;
In order to simplify the source code for this typical case,
the Scala continuations library includes a special annotation type,
&lt;code&gt;suspendable&lt;/code&gt;:

&lt;pre name="hlcode" class="scala"
&gt;type suspendable = cpsParam[Unit, Unit]
&lt;/pre&gt;

In addition to being more succinct,
this annotation type can be used to make it clear that this
function may suspend its continuation so that it can finish execution
later.

&lt;a name="nested_shift"&gt;&lt;/a&gt;
&lt;h3&gt;Nested Shift&lt;/h3&gt;

In all of the above examples,
the &lt;code&gt;shift&lt;/code&gt; block appears directly inside the
&lt;code&gt;reset&lt;/code&gt; block,
and the &lt;code&gt;cpsParam&lt;/code&gt; type of the &lt;code&gt;reset&lt;/code&gt; block must match
the &lt;code&gt;cpsParam&lt;/code&gt; type of the &lt;code&gt;shift&lt;/code&gt; block.

&lt;br/&gt;&lt;br/&gt;
What happens if you put the &lt;code&gt;shift&lt;/code&gt; block in a separate
function and call that function from the &lt;code&gt;reset&lt;/code&gt; block?
In this case, the function containing the &lt;code&gt;shift&lt;/code&gt; block
must be marked as a CPS function by using the &lt;code&gt;cpsParam&lt;/code&gt;
annotation on its return type, and that
&lt;code&gt;cpsParam&lt;/code&gt; type must be the same as the
&lt;code&gt;cpsParam&lt;/code&gt; type of the enclosed &lt;code&gt;shift&lt;/code&gt; block.
When this function is invoked from within the &lt;code&gt;reset&lt;/code&gt; block,
the compiler plugin knows how to transform that block such that the
code after the call to the CPS function becomes part of a
continuation which is passed in to the CPS function,
just as in the &lt;a href="#nested_cps"&gt;Nested CPS&lt;/a&gt; examples above.

&lt;pre name="hlcode" class="scala"
&gt;def is123(n:Int):Boolean = {
  reset {
    is123sub(n)
  }
}

def is123sub(n:Int):String @cpsParam[String,Boolean] = {
    shift { k : (Int=&amp;gt;String) =&amp;gt;
      (k(n) == "123")
    }.toString
}
&lt;/pre&gt;

The function containing the &lt;code&gt;shift&lt;/code&gt; block can be refactored
to push that &lt;code&gt;shift&lt;/code&gt; block down into another function,
in which case that new function must also have the same signature as
the original function and the &lt;code&gt;shift&lt;/code&gt; block.
Thus the entire chain of functions between the &lt;code&gt;reset&lt;/code&gt;
and the &lt;code&gt;shift&lt;/code&gt; are all tied together with the same CPS signature.

&lt;br/&gt;&lt;br/&gt;
What if you have an existing CPS function, but you want to call it
and change its return type?
If you were to follow the pattern of regular code, you might start
by trying something like this in order to return floating point
1 or 0 rather than the Boolean true or false returned by a
&lt;code&gt;reset&lt;/code&gt; block that just calls &lt;code&gt;is123sub&lt;/code&gt;.

&lt;pre name="hlcode" class="scala"
&gt;//this won't compile
def is123f(n:Int):Float = {
    reset {
        val x = is123sub(n)
        if (x) 1.0 else 0.0
    }
}
&lt;/pre&gt;

This does not work as expected;
the line of code following the call to &lt;code&gt;is123sub&lt;/code&gt;
is not operating on what will be the
return value of the &lt;code&gt;reset&lt;/code&gt; block, despite it being the last
statement in that block.
Instead, due to the code transformation described above that is being done
by the CPS compiler plugin,
code added after the call to &lt;code&gt;is123sub&lt;/code&gt; gets bundled up as
part of the continuation passed to the &lt;code&gt;shift&lt;/code&gt; block within
&lt;code&gt;is123sub&lt;/code&gt;.
The code that follows the call to the CPS function must end with
a type that matches the first parameter of the &lt;code&gt;cpsParam&lt;/code&gt;
part of the signature of the function; in this case, &lt;code&gt;String&lt;/code&gt;
The untransformed return type of &lt;code&gt;is123sub&lt;/code&gt; is also
&lt;code&gt;String&lt;/code&gt;, so in this case the block of code that follows the call to
&lt;code&gt;is123sub&lt;/code&gt; must take a &lt;code&gt;String&lt;/code&gt; (as the return value
of the call to &lt;code&gt;is123sub&lt;/code&gt;) and must also return a &lt;code&gt;String&lt;/code&gt;
(which becomes the return value of the &lt;code&gt;shift&lt;/code&gt; block
within &lt;code&gt;is123sub&lt;/code&gt;).

&lt;br/&gt;&lt;br/&gt;
If we want to intercept the &lt;code&gt;Boolean&lt;/code&gt; value that is being
calculated in the &lt;code&gt;shift&lt;/code&gt; block within &lt;code&gt;is123sub&lt;/code&gt;,
we must do that from within another &lt;code&gt;shift&lt;/code&gt; block.
The body of a &lt;code&gt;shift&lt;/code&gt; block is written in Direct Style,
and our subroutine &lt;code&gt;is123sub&lt;/code&gt; is CPS, so we can't call it
from within the new &lt;code&gt;shift&lt;/code&gt; block.
What we have to do is to put the new &lt;code&gt;shift&lt;/code&gt; block
&lt;i&gt;before&lt;/i&gt; the call to &lt;code&gt;is123sub&lt;/code&gt;.
The call to &lt;code&gt;is123sub&lt;/code&gt; then becomes part of the continuation
that is passed to the new &lt;code&gt;shift&lt;/code&gt; block,
and we can add code within the new &lt;code&gt;shift&lt;/code&gt; block that
receives the transformed result of the &lt;code&gt;shift&lt;/code&gt; block
in &lt;code&gt;is123sub&lt;/code&gt; and converts it as desired.

&lt;br/&gt;&lt;br/&gt;
To see the control flow a little more clearly, you can execute this
code snippet:

&lt;pre name="hlcode" class="scala"
&gt;reset {
    println("A")
    shift { k1: (Unit=&amp;gt;Unit) =&amp;gt;
        println("B")
        k1()
        println("C")
    }
    println("D")
    shift { k2: (Unit=&amp;gt;Unit) =&amp;gt;
        println("E")
        k2()
        println("F")
    }
    println("G")
}
&lt;/pre&gt;

Here's the output the above code produces:
&lt;pre name="hlcode" class="scala"
&gt;A
B
D
E
G
F
C
&lt;/pre&gt;

You can see from the order of execution that the second &lt;code&gt;shift&lt;/code&gt;
block is being executed as part of the continuation that is passed to the
first &lt;code&gt;shift&lt;/code&gt; block.
Despite the fact that one appears before the other in the source code,
the two &lt;code&gt;shift&lt;/code&gt; blocks are actually nested.
The compiler plugin notices this and handles them slightly differently
to prevent the nested &lt;code&gt;shift&lt;/code&gt; block from escaping from the
enclosing &lt;code&gt;reset&lt;/code&gt; block.

&lt;br/&gt;&lt;br/&gt;
To show how all of the types thread together, here is a little piece
of code with explicit type annotations on the &lt;code&gt;reset&lt;/code&gt; and
&lt;code&gt;shift&lt;/code&gt; blocks
in which you can see sets of places for which the same type needs to be used.
The &lt;code&gt;assert&lt;/code&gt; statements help show how the values are
getting passed around.

&lt;pre name="hlcode" class="scala"
&gt;def nestedShifts[T1,T2,T3,T4,T5](t1:T1,t2:T2,t3:T3,t4:T4,t5:T5):T2 = {
    reset[T1,T2] {
        val s1:T3 = shift[T3,T5,T2] { k1: (T3=&amp;gt;T5) =&amp;gt;
            val r1:T5 = k1(t3)
            assert(r1==t5)
            t2  //this is the return value of nestedShifts
        }
        assert(s1==t3)
        val s2:T4 = shift[T4,T1,T5] { k2: (T4=&amp;gt;T1) =&amp;gt;
            val r2:T1 = k2(t4)
            assert(r2==t1)
            t5
        }
        assert(s2==t4)
        t1
    }
}
&lt;/pre&gt;

If you get a compiler error when nesting CPS functions like this,
try modifying the code to assign the value of the nested CPS function
to a local variable, then end with that variable:

&lt;pre name="hlcode" class="scala"
&gt;def is123f(n:Int):Float = {
    reset {
        val x = shift { k:(Int=&amp;gt;Boolean) =&amp;gt;
            if (k(n)) 1.0f else 0.0f
        }
        val r = is123sub(x)
        r
    }
}
&lt;/pre&gt;

If you leave out the &lt;code&gt;val r&lt;/code&gt; and just end the &lt;code&gt;reset&lt;/code&gt;
block with the call to &lt;code&gt;is123sub&lt;/code&gt;, you will get an error
such as this:

&lt;pre name="hlcode" class="scala"
&gt;&amp;lt;console&amp;gt;:13: error: type mismatch;
 found   : String @scala.util.continuations.cpsParam[String,Boolean]
 required: String @scala.util.continuations.cpsParam[String,Float]
           is123sub(x)
                   ^
&lt;/pre&gt;

&lt;a name="control_construct_restrictions"&gt;&lt;/a&gt;
&lt;h3&gt;Control Construct Restrictions&lt;/h3&gt;

Because of the code transformation performed by
the continuations compiler plugin,
there are some control constructs that can not be used
when calling a CPS function.

&lt;br/&gt;&lt;br/&gt;
Using &lt;code&gt;return&lt;/code&gt; statements in a
CPS function is unlikely to do what you expect, and may cause
&lt;code&gt;type mismatch&lt;/code&gt; compiler errors, so you should not use them.

&lt;br/&gt;&lt;br/&gt;
When using an &lt;code&gt;if&lt;/code&gt; statement,
you may get an error like this:

&lt;pre name="hlcode" class="scala"
&gt;Foo.scala:21: error: then and else parts must both be cps code or neither of them
&lt;/pre&gt;

&lt;a href="http://www.scala-lang.org/node/3177"&gt;Tiark's advice&lt;/a&gt;
is not to use explicit &lt;code&gt;return&lt;/code&gt;, and maybe use
&lt;code&gt;shiftUnit&lt;/code&gt; on the non-CPS value.

&lt;br/&gt;&lt;br/&gt;
The compiler plugin does not handle &lt;code&gt;try&lt;/code&gt; blocks,
so you can't catch exceptions within CPS code.
Those exceptions will be propagated out to the enclosing &lt;code&gt;reset&lt;/code&gt;
block and can be caught there - unless the continuation is suspended
and executed later, in which case any exceptions would be propagated to
the &lt;code&gt;reset&lt;/code&gt; block of the code doing that later execution.

&lt;br/&gt;&lt;br/&gt;
You need to be careful when using looping constructs.
As
&lt;a href="http://scala-programming-language.1934581.n4.nabble.com/scala-Tail-Recursive-Calls-possible-inside-CPS-td2221188.html#a2226758"&gt;
Tiark says&lt;/a&gt;,
&lt;blockquote&gt;Capturing delimited continuations inside a while loop
turns the loop basically into a general recursive
function.&lt;/blockquote&gt;
You can follow the above link for details, but basically each invocation
of &lt;code&gt;shift&lt;/code&gt; within a looping construct allocates another stack
frame, so after "looping" many times you will likely get a
StackOverflowError.

&lt;br/&gt;&lt;br/&gt;
Some looping constructs can not be used with a &lt;code&gt;shift&lt;/code&gt;
inside them.
To
&lt;a href="http://scala-programming-language.1934581.n4.nabble.com/scala-Continuations-multiple-calls-to-shift-within-a-reset-td2003158.html#a2003169"&gt;
quote Tiark&lt;/a&gt; again:
&lt;blockquote&gt;In a reset block you can do anything, but shifts are not
allowed everywhere. The limitation is that everything on the call path
between a shift and its enclosing reset must be "shift-aware". That
rules out the regular foreach, map and filter methods because they
know nothing about continuations, so they can't call closures
containing shift.&lt;/blockquote&gt;

&lt;a name="advice"&gt;&lt;/a&gt;
&lt;h3&gt;Advice&lt;/h3&gt;

As I mentioned at the start of this post, it took me some time to feel
that I had a good understanding of how &lt;code&gt;reset&lt;/code&gt; and
&lt;code&gt;shift&lt;/code&gt; work.
You may not get it in one reading of this post.
As with any new coding concept, the best way to gain a working
understanding is to try using it in some of your own code.
You will need patience; the CPS error messages are not always clear.

&lt;br/&gt;&lt;br/&gt;
If you are interested in playing with control constructs,
such as actors or generators,
then you should definitely take the time to understand
&lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;.
You might also want to take a look at
&lt;a href="http://www.scala-lang.org/node/3485"&gt;Swarm&lt;/a&gt;.

&lt;br/&gt;&lt;br/&gt;
On the other hand, you may never need to deal with
&lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;.
Now that they are available in Scala, I expect some people will
create libraries that build on &lt;code&gt;reset&lt;/code&gt; and &lt;code&gt;shift&lt;/code&gt;
to present APIs for developers that are simpler to understand.
Still, even when using those simpler APIs you may find that
an understanding of the content of this post will be useful.

&lt;a name="resources"&gt;&lt;/a&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Wikipedia article about &lt;a href="http://en.wikipedia.org/wiki/Continuation-passing_style"&gt;CPS&lt;/a&gt;.
&lt;li&gt;Tiark Rompf's
    &lt;a href="http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf"&gt;
    explanatory paper&lt;/a&gt; on Delimited Continuations in Scala.
    Some heavy going, but also includes a number of examples
    of different capabilities that can be built on top of
    delimited continuations.
&lt;li&gt;Rich Dougherty's
    &lt;a href="http://blog.richdougherty.com/2009/02/delimited-continuations-in-scala_24.html"&gt;'Delimited continuations in Scala'&lt;/a&gt;
    blog entry of 2009-02-24.
&lt;li&gt;Rich Dougherty's
    &lt;a href="http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html"&gt;'Tail calls, @tailrec and trampolines'&lt;/a&gt;
    blog entry of 2009-04-05
    discusses stack size management, including a section
    that mentions Scala continuations.
&lt;li&gt;Daniel Sobral's &lt;a href="http://dcsobral.blogspot.com/2009/07/delimited-continuations-explained-in.html"&gt;'Delimited Continuations Explained (in Scala)'&lt;/a&gt;
    blog entry of 2009-07-04.
&lt;li&gt;Josh Suereth's &lt;a href="http://suereth.blogspot.com/2010/03/how-you-should-think-about-delimited.html"&gt;'How you should think about Delimited Continuations in Scala'&lt;/a&gt;
    blog entry of 2010-03-10.
&lt;li&gt;&lt;a href="http://www.scala-lang.org/archives/rc-api/scala/util/continuations/package.html"&gt;Scaladoc&lt;/a&gt;
    for the scala.util.continuations package.
&lt;li&gt;&lt;a href="http://www.scala-lang.org/node/3485"&gt;Swarm&lt;/a&gt;,
    a distributed programming system that captures delimited continuations
    in order to move them around the network for execution on other nodes.
    You can see a &lt;a href="http://code.google.com/p/swarm-dpl/"&gt;video&lt;/a&gt;
    introducing Swarm on their Google Code page.
&lt;/ul&gt;

Updated 2010-08-09 to fix error pointed out by mgm7734.
&lt;br/&gt;
Updated 2010-09-26 to fix error pointed out by Nikolay.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-1492763927689974370?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/1492763927689974370/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=1492763927689974370' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/1492763927689974370'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/1492763927689974370'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/08/delimited-continuations.html' title='Delimited Continuations'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4405639477394559084</id><published>2010-05-28T21:40:00.000-07:00</published><updated>2010-07-10T09:54:02.974-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>My Misperception of Scala's Ordered Trait</title><content type='html'>A brief tale of a little misperception that I had,
and how it was corrected.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;Ordered&lt;/code&gt;
trait in Scala is typically used when defining a class of objects
that know how to order themselves by comparing against
other instances of that class.
The typical class definition looks something like this:

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;case class Thing(val n:Int) extends Ordered[Thing] {
    def compare(that: Thing): Int = { this.n - that.n }  //&lt;a href="#note1"&gt;[1]&lt;/a&gt;
}
&lt;/pre&gt;

When I first started looking at these definitions, they just looked wrong to me:
it seemed like &lt;code&gt;Ordered[Thing]&lt;/code&gt;
depended on the definition of &lt;code&gt;Thing&lt;/code&gt;, and &lt;code&gt;Thing&lt;/code&gt; in
turn was defined in terms of &lt;code&gt;Ordered[Thing]&lt;/code&gt;; a circular definition!

&lt;br/&gt;&lt;br/&gt;
It is of course not a circular definition.
The appropriate interpretation was made obvious to me
recently while reviewing some Scala code with a co-worker, who was using the
&lt;code&gt;Ordered&lt;/code&gt; trait in a non-canonical way.
Instead of the usual use of &lt;code&gt;Ordered&lt;/code&gt;
as in the above class definition,
his class definition looked similar to this:

&lt;pre name="hlcode" class="scala"
&gt;case class Thing(val n:Int) extends Ordered[Any] {
    def compare(that: Any): Int = that match {
        case i:Int =&amp;gt; this.n - i
        case x:Thing =&amp;gt; this.n - x.n
        case _ =&amp;gt; throw new IllegalArgumentException("bad type")
    }
}
&lt;/pre&gt;

The reason he did this was because he wanted to be able to compare
a &lt;code&gt;Thing&lt;/code&gt; - which has an &lt;code&gt;Int&lt;/code&gt;
value as part of its definition -
against either a &lt;code&gt;Thing&lt;/code&gt; or an &lt;code&gt;Int&lt;/code&gt;.
The smallest common superclass of &lt;code&gt;Thing&lt;/code&gt;
and &lt;code&gt;Int&lt;/code&gt; is &lt;code&gt;Any&lt;/code&gt;, so his
compare method had to accept an argument of type &lt;code&gt;Any&lt;/code&gt;,
which in turn meant the &lt;code&gt;Ordered&lt;/code&gt; trait must be
&lt;code&gt;Ordered[Any]&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
This somewhat unusual use of &lt;code&gt;Ordered&lt;/code&gt;
led us to another small problem.
He had an &lt;code&gt;Array[Thing]&lt;/code&gt;, sorted according to
&lt;code&gt;Thing.compare&lt;/code&gt;,
on which he wanted to do a binary search.
We couldn't find a binary search built in to Scala,
but it was simple enough to find one on the web.
We grabbed the Scala implementation of binary search from
&lt;a href="http://rosettacode.org/wiki/Binary_search#Scala"&gt;
RosettaCode.org&lt;/a&gt;
(and changed the type of the argument from
&lt;code&gt;IndexedSeq[A]&lt;/code&gt; to &lt;code&gt;Array[A]&lt;/code&gt;
since we were using Scala 2.7):

&lt;pre name="hlcode" class="scala"
&gt;def binarySearch[A &amp;lt;% Ordered[A]](a: Array[A], v: A) = {
  def recurse(low: Int, high: Int): Option[Int] = (low + high) / 2 match {
    case _ if high &lt; low =&gt; None
    case mid if a(mid) &amp;gt; v =&amp;gt; recurse(low, mid - 1)
    case mid if a(mid) &amp;lt; v =&amp;gt; recurse(mid + 1, high)
    case mid =&amp;gt; Some(mid)
  }
  recurse(0, a.size - 1)
}
&lt;/pre&gt;

Our code to call the &lt;code&gt;binarySearch&lt;/code&gt;
method looked something like this:

&lt;pre name="hlcode" class="scala"
&gt;val a:Array[Thing] = Array(Thing(1), Thing(3), Thing(5), Thing(6))
val x = binarySearch(a,3)
&lt;/pre&gt;

It failed to compile, with an error like this REPL output:

&lt;pre name="hlcode" class="scala"
&gt;&amp;lt;console&amp;gt;:7: error: type mismatch;
 found   : Array[Thing]
 required: Array[Any]
       val x = binarySearch(a,3)
                            ^
&amp;lt;console&amp;gt;:7: error: no implicit argument matching parameter type (Any) =&gt; Ordered[Any] was found.
       val x = binarySearch(a,3)
               ^
&lt;/pre&gt;

The cause of this pair of error messages may be obvious to some people,
but we scratched our heads a while trying to figure out what we had done wrong.
Eventually we realized that the &lt;code&gt;binarySearch&lt;/code&gt;
method was making the assumption
that the element type of the array was same as the type of the sort;
in other words, the &lt;code&gt;binarySearch&lt;/code&gt;
method only worked on arrays of elements
where each element, of type &lt;code&gt;A&lt;/code&gt;, implemented &lt;code&gt;Ordered[A]&lt;/code&gt;.
Since our &lt;code&gt;Thing&lt;/code&gt; class did not do that,
we were getting a type mismatch
when trying to pass it to &lt;code&gt;binarySearch&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
The solution was to modify &lt;code&gt;binarySearch&lt;/code&gt; to explicitly have two type
parameters, one for the element type of the array (A) and one for the
ordering type of those elements (B), i.e. the type of the values
against which we can compare the array elements:

&lt;pre name="hlcode" class="scala"
&gt;def binarySearch[A,B](a: Array[A with Ordered[B]], v: B) = {
  def recurse(low: Int, high: Int): Option[Int] = (low + high) / 2 match {
    case _ if high &amp;lt; low =&amp;gt; None
    case mid if a(mid) &amp;gt; v =&amp;gt; recurse(low, mid - 1)
    case mid if a(mid) &amp;lt; v =&amp;gt; recurse(mid + 1, high)
    case mid =&amp;gt; Some(mid)
  }
  recurse(0, a.size - 1)
}
&lt;/pre&gt;

Going through this exercise helped me clearly see the distinction
between the element type and the ordering type,
and more generally to firmly excise the mistaken perception of
circular dependency I mentioned at the start of this post.
Now when I see &lt;code&gt;class Foo extends Bar[Foo]&lt;/code&gt;,
it's easy for me to remember that just means that class
&lt;code&gt;Foo&lt;/code&gt; implements
methods, as declared in &lt;code&gt;Bar&lt;/code&gt;,
that happen to take arguments of type &lt;code&gt;Foo&lt;/code&gt;
&lt;a href="note2"&gt;[2]&lt;/a&gt;.

&lt;h3&gt;Footnotes&lt;/h3&gt;
&lt;a name="note1"&gt;&lt;/a&gt;
[1] Using a straight subtraction like this for the comparison will fail
in extreme cases, such as if the first value is MAX_VALUE and the second
value is negative;
however, in the interest of keeping the code example simple,
I have used this very short and understandable code snippet.

&lt;br/&gt;&lt;br/&gt;
&lt;a name="note2"&gt;&lt;/a&gt;
[2] Yes, I know this is not a very precise statement:
&lt;code&gt;Bar[Foo]&lt;/code&gt; might only define variables rather than methods, or
the methods might take arguments of type &lt;code&gt;List[Foo]&lt;/code&gt;
rather than &lt;code&gt;Foo&lt;/code&gt;,
or any of a number of other variations.

&lt;br/&gt;&lt;br/&gt;
Updated 2010-07-10 to escape angle brackets as pointed out by
&lt;a href="http://jim-mcbeath.blogspot.com/2008/08/scala-traits.html?showComment=1278779308733#c2379702887319874135"&gt;tolund&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4405639477394559084?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4405639477394559084/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4405639477394559084' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4405639477394559084'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4405639477394559084'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/05/my-misperception-of-scalas-ordered.html' title='My Misperception of Scala&apos;s Ordered Trait'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-6930718009174183960</id><published>2010-01-10T22:05:00.000-08:00</published><updated>2010-01-10T22:05:56.567-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Reload That Config File</title><content type='html'>It is common for applications to load a configuration file
on startup to control various options.
Some applications can also reload their configuration file
while running,
allowing you to modify the application configuration without
having to restart the application.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#goals"&gt;Goals&lt;/a&gt;
&lt;li&gt;&lt;a href="#dependency-injection"&gt;Dependency Injection&lt;/a&gt;
&lt;li&gt;&lt;a href="#config-contents"&gt;Config Contents&lt;/a&gt;
&lt;li&gt;&lt;a href="#config-objects"&gt;Config Objects&lt;/a&gt;
&lt;li&gt;&lt;a href="#steps"&gt;Seven Steps&lt;/a&gt;
&lt;li&gt;&lt;a href="#unit-testing"&gt;Unit Testing&lt;/a&gt;
&lt;li&gt;&lt;a href="#implementation"&gt;Implementation Options&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="goals"&gt;&lt;/a&gt;
&lt;h3&gt;Goals&lt;/h3&gt;

Configuration files (or config files)
are useful because they let us change the behavior of an
application with a mechanism that is much simpler and faster than
modifying the application source and recompiling it.
Being able to reload the configuration of a running application allows
us to take that concept a bit further, as generally we can make
reloading the configuration operationally simpler and faster
than shutting down and restarting the application.

&lt;br/&gt;&lt;br/&gt;
When reloading the configuration, we have the following goals:
&lt;ul&gt;
&lt;li&gt;Reloading a configuration should be a simple operation for the operator
    to trigger.
&lt;li&gt;It should not be possible to load an invalid configuration.
    If the operator tries to do so, the application should continue running
    with the old configuration.
&lt;li&gt;When reloading a configuration, the application should smoothly switch
    from the old configuration to the new configuration,
    ensuring that it is always operating with a consistent configuration.
    More precisely, an operational sequence that requires a consistent
    set of configuration parameters for the entire sequence should
    complete its sequence with the same set of configuration parameters
    as were active when the sequence started.
&lt;li&gt;The application should provide feedback so that the operator
    knows what the application is doing.
    Logging, notification or statistics about configuration reloads
    should be available.
&lt;/ul&gt;

&lt;a name="dependency-injection"&gt;&lt;/a&gt;
&lt;h3&gt;Dependency Injection&lt;/h3&gt;

&lt;a href="http://martinfowler.com/articles/injection.html"&gt;
Dependency injection&lt;/a&gt; (DI)
is a form of structural configuration in which different suppliers of a service
are wired into an application based on the contents of a configuration file.
In the typical case, these configurations are unlikely to change
once an application has started.
Although in principle it is possible to reload a DI configuration,
and thus all of the discussion below could apply,
in practice you might want to separate out the kind of
relatively static structural configuration that is typically done with DI
from the more dynamic parametric configuration that you might want
to change while the application is running,
and use different mechanisms to implement those two sets of configurations.

&lt;br/&gt;&lt;br/&gt;
Alternatively, you can selectively disallow
(as part of your &lt;a href="#validate"&gt;validation&lt;/a&gt; step)
configuration changes
that are too much work to implement,
requiring the user who wants to make such changes
to restart the application.

&lt;a name="config-contents"&gt;&lt;/a&gt;
&lt;h3&gt;Config Contents&lt;/h3&gt;

If you think of a config file as being a set of late-binding
commands for controlling the behavior of an application program,
it should be clear that the most flexible config file
is one that is itself a program.
Applications that already have a built-in interpreter, such as
&lt;a href="http://www.gnu.org/software/emacs/"&gt;emacs&lt;/a&gt;
and applications written in
&lt;a href="http://www.paulgraham.com/lisp.html"&gt;Lisp&lt;/a&gt;,
often simply feed their config files to their interpreter,
giving them the full power of a
&lt;a href="http://en.wikipedia.org/wiki/Turing_completeness"&gt;
Turing-complete&lt;/a&gt; language in
which to express site-specific program behavior.

&lt;br/&gt;&lt;br/&gt;
If you have an
&lt;a href="http://en.wikipedia.org/wiki/Interpreter_(computing)"&gt;
interpreter&lt;/a&gt; available, this can be a reasonable option:
it is simple to implement, takes little work to document (assuming
you already have to provide documentation for the interpreted language
anyway), and provides a great deal of flexibility.
One potential downside is that you might not want all of the power of
the language to be available in a config file;
in particular, if you are using the language internally,
your program may have made available certain functions that you
don't want a user to call from a config file.
If your application already has a security framework built in to it,
this may be easy enough to do, or you may not be concerned about it.
In any case, you should at least be aware of this potential pitfall if
you choose to use a language as your config file syntax.

&lt;br/&gt;&lt;br/&gt;
At the other end of the spectrum, you could choose a standard
&lt;code&gt;name=value&lt;/code&gt; format,
such as &lt;a href="http://en.wikipedia.org/wiki/INI_file"&gt;Windows INI file&lt;/a&gt;
or a
&lt;a href="http://java.sun.com/docs/books/tutorial/essential/environment/properties.html"&gt;
Java Properties file&lt;/a&gt;
If you have a relatively simple application with just a few config
parameters to set, this is probably a reasonable option.

&lt;br/&gt;&lt;br/&gt;
You can treat all of your config data as strings and let the application
deal with each individually, or you can define a set of datatypes that
can be uniformly represented in a config file.
This might include lists of data or other compound types.

&lt;br/&gt;&lt;br/&gt;
One of the typical capabilities implemented in config systems is
the ability to group config parameters into logical groupings.
The standard Windows INI file does this with its &lt;code&gt;[section]&lt;/code&gt;
prefixes.
You can simulate this in a Properties file by selecting a character
to be a name separator (typically a period),
then using that separator character to define names
for your parameters that indicate their grouping.
This can easily be extended to multiple levels to allow
a hierarchy of grouped parameters.

&lt;br/&gt;&lt;br/&gt;
Once you have groups of config parameters, you might want to implement
some kind of inheritance mechanism, whereby you can declare a set of
names and values in group A,
then declare that group B has the same item values as group A,
possibly with some specified exceptions.
Or perhaps you would like to be able to set the value of a parameter to
be the same as the value of some other parameter,
or some combination or transformation of other parameters.

&lt;br/&gt;&lt;br/&gt;
You can continue to add more capabilities to your config file,
but once you start getting too complex, you probably want to adopt an
existing language syntax to avoid creating
&lt;a href="http://en.wikipedia.org/wiki/Greenspun%27s_Tenth_Rule"&gt;
something&lt;/a&gt;
that is complicated
to implement and maintain, tedious to document, and difficult to learn and use.

&lt;br/&gt;&lt;br/&gt;
If you do use a language for your config file,
you may need to modify your approach in order to be able to
implement all of the steps given below.
In particular,
you should not directly modify your operational objects from the config file,
as this violates the separation of config data from the application
and makes it more difficult to validate the entire config before
activating it.
One solution is to make your config file code
only set data into the new Config objects that are being created
for the reload process.
Other solutions are possible, such as setting up a mock execution
environment in which code can be validated before being applied,
but a detailed discussion of such techniques is
outside the scope of this post.

&lt;br/&gt;&lt;br/&gt;
When choosing a format,
you might consider whether you plan on maintaining config files
through a program (either the application being configured or
a separate config maintenance application),
or if editing config files with a text editor is sufficient.
Some applications maintain their config files in
&lt;a href="http://www.w3.org/XML/"&gt;XML&lt;/a&gt; format for this reason,
as there are many packages that can easily read and write XML files,
as well as do basic syntax checking outside of the application being configured.
Properties files can also be easily written,
but there are many other formats that could be used.
This can get tricky if you are trying to use an application to maintain
config files when you are using a general purpose language for those files.

&lt;br/&gt;&lt;br/&gt;
No matter what format you settle on for your config files,
the same concerns discussed below apply regarding reloading the config.

&lt;a name="config-objects"&gt;&lt;/a&gt;
&lt;h3&gt;Config Objects&lt;/h3&gt;

In the approach described here we store in-memory configuration information
in special Config objects
that are separate from the operational objects that they configure.
Defining separate Config objects gives us these benefits:
&lt;ul&gt;
&lt;li&gt;It allows us to represent multiple configurations simultaneously.
    In particular, it allows us to load and operate on
    a configuration that is
    separate from the currently active configuration.
&lt;li&gt;It provides a convenient location to collect the methods that
    manipulate or otherwise access the configuration parameters.
&lt;/ul&gt;

There should be a set of Config objects that correspond to the
different operational objects that can be configured.
Each different class of operational object to be configured should have a
different custom class of Config object associated with it.
An operational class with multiple instances should have a separate
instance of its Config class associated with each operational instance.

&lt;br/&gt;&lt;br/&gt;
The various Config objects should be related to each other in the
same way as the operational objects are related to each other;
for example, if operational object A can have multiple children
of type B, then ConfigA should be able to have multiple children
of type ConfigB.
There should be a single Config object which serves as the root
Config object from which all other Config objects can be reached.

&lt;br/&gt;&lt;br/&gt;
If the application is written such that there is a single application-wide
active configuration, then
the application should have a singleton which is the active root
Config object.
In the discussion below, I assume that such a singleton exists;
if your application has multiple contexts, each with a different
set of config info, you should interpret the word "singleton" to
refer to the single active root config for the context whose config is
being updated.

&lt;br/&gt;&lt;br/&gt;
All of the Config classes can inherit from a standard base Config class
that provides implementations of common useful methods
such as type-safe calls to get integer and date parameters.

&lt;a name="steps"&gt;&lt;/a&gt;
&lt;h3&gt;Seven Steps&lt;/h3&gt;

There are seven steps involved in loading or reloading configuration data:
&lt;a href="#trigger"&gt;Trigger&lt;/a&gt;,
&lt;a href="#locate"&gt;Locate&lt;/a&gt;,
&lt;a href="#load"&gt;Load&lt;/a&gt;,
&lt;a href="#validate"&gt;Validate&lt;/a&gt;,
&lt;a href="#activate"&gt;Activate&lt;/a&gt;,
&lt;a href="#report"&gt;Report&lt;/a&gt;, and
&lt;a href="#use"&gt;Use&lt;/a&gt;.

Each of these steps can be considered independently of the others.
Each step has its own design decisions and implementation choices.
In the approach we are using, the Config objects mentioned above
are the common data shared by all but the first two steps.

&lt;a name="trigger"&gt;&lt;/a&gt;
&lt;h4&gt;Trigger&lt;/h4&gt;

If your application is going to reload its configuration information,
it needs to know when to do that.
There are a number of options:

&lt;ul&gt;
&lt;li&gt;Your app can check for changes on a regular interval and reload
    if the source has changed.
    This is a typical approach used with logging configuration files
    such as for log4j, in which you can specify
    &lt;a href="http://logging.apache.org/log4j/1.2/faq.html#3.6"&gt;
    automatic reloading&lt;/a&gt;
    with a call to the static
    &lt;code&gt;configureAndWatch&lt;/code&gt;
    method of
    &lt;a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html"&gt;
    DOMConfigurator&lt;/a&gt;
    or
    &lt;a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html"&gt;
    PropertyConfigurator&lt;/a&gt;.
&lt;li&gt;If your app has a command line interface
    (&lt;a href="http://en.wikipedia.org/wiki/Command-line_interface"&gt;CLI&lt;/a&gt;),
    you can add a command that reloads the config info.
&lt;li&gt;If your app has a web interface, you can add a web page that
    controls config reloads.
    This can be a full web page with a form and feedback, or a simple
    URL that triggers a reload.
&lt;li&gt;On a Unix system, a standalone app such as a daemon
    can be written such that a reload is triggered on receipt of a
    &lt;a href="http://en.wikipedia.org/wiki/Signal_(computing)"&gt;
    signal&lt;/a&gt;.
    You can then use the &lt;code&gt;kill&lt;/code&gt; command to send the process that
    signal.
    SIGHUP (signal 1) is often used by Unix daemon programs for this purpose,
    some examples being
    acpid, dnsmasq, postgresd, smartd, smbd, winbindd, and ypbind.
&lt;li&gt;For a Java app, you can enable
    &lt;a href="http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/"&gt;JMX&lt;/a&gt;
    and use that to send commands to your application with a JMX console app
    such as
    &lt;a href="http://openjdk.java.net/tools/svc/jconsole/"&gt;jconsole&lt;/a&gt; or
    &lt;a href="http://sourceforge.net/projects/mc4j/"&gt;MC4J&lt;/a&gt;.
    &lt;a href="http://www.jboss.com/"&gt;JBoss&lt;/a&gt;
    uses this technique, allowing you to
    &lt;a href="http://jbossuser.blogspot.com/2009/06/reload-log4j-configuration.html"&gt;
    reload its log4j config&lt;/a&gt;
    using the JBoss jmx-console.
&lt;li&gt;For many apps, you can pretty easily add a web interface,
    such as by using
    &lt;a href="http://www.mortbay.org/jetty/"&gt;Jetty&lt;/a&gt; for Java apps,
    for the purpose of allowing control and status feedback.
&lt;/ul&gt;

You may want to limit how often a reload can be triggered to prevent a
&lt;a href="http://en.wikipedia.org/wiki/Denial-of-service_attack"&gt;DOS&lt;/a&gt;
attack (or the same effect caused by a bug in whatever is
producing the trigger).

&lt;a name="locate"&gt;&lt;/a&gt;
&lt;h4&gt;Locate&lt;/h4&gt;

Once the app has been triggered to reload the config info, it needs to
locate that info.
Some options:

&lt;ul&gt;
&lt;li&gt;Assume the data is in the same location as before and reopen that
    location,
    such as is often done for a log4j config file.
&lt;li&gt;Provide the location of the data along with the trigger.
    This is easy to do if you have a CLI, web form, or web URL,
    not so easy if you are using a timer or a Unix signal.
&lt;/ul&gt;

Some applications (such as one that uses the standard
&lt;a href="http://scala-tools.org/scaladocs/liftweb/1.0/net/liftweb/util/Props$object.html"&gt;
&lt;code&gt;Props&lt;/code&gt;&lt;/a&gt; class in &lt;a href="http://liftweb.net/"&gt;Lift&lt;/a&gt;,
including the way Lift handles its
&lt;a href="http://wiki.github.com/dpp/liftweb/how-to-configure-logging"&gt;
log4j configuration&lt;/a&gt;)
have more sophisticated file lookup
mechanisms that allow configuration information to be split among multiple
files or segregated according to the runtime environment to be used.
If you are using a package that looks for one or more out of a set of
possible files,
and you want to be able to add or remove a config file and then reload,
you should check to make sure the package is able to reload files and
that it will rescan its set of
possible files and not just assume that the same config files should be used
as when they were first loaded.

&lt;a name="load"&gt;&lt;/a&gt;
&lt;h4&gt;Load&lt;/h4&gt;

Once the data has been located, it needs to be loaded into memory where
it can be manipulated.
Note that you should load the data into a new Config object or set of objects
so that you can do the validation checks on it before activating it.

&lt;br/&gt;&lt;br/&gt;
You should not have to write the code that actually loads the data,
as there are a number of usable options available.
As an example, you can store your config data in the standard Java
Properties format, then load that data using
&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)"&gt;
Properties.load&lt;/a&gt;.
After reading the data into a Properties object,
you can create your custom Config objects from the data in the Properties
object.

&lt;a name="validate"&gt;&lt;/a&gt;
&lt;h4&gt;Validate&lt;/h4&gt;

Once the config data is loaded into your Config objects,
you are ready to validate the new configuration.
You should make the following checks:

&lt;ol&gt;
&lt;li&gt;Ensure that the syntax of all configuration values is correct.
    Depending on how you loaded the data and converted it to your
    Config objects, some of these checks may already have been
    done.  If there are any values which have not yet been checked
    for correct syntax, those values should be checked now.
&lt;li&gt;Perform semantic checks on individual parameters.
    This includes things such as checking that numbers are within
    allowable ranges, or that each selection parameter has a value
    that is one of the allowable selections for that parameter.
&lt;li&gt;Perform validity checks on multiple parameters.
    This includes situations in which you have
    two or more parameters that are related and
    which thus must have values consistent with each other.
&lt;li&gt;Compare the new set of Config objects against the current
    set to ensure that all proposed changes are allowed.
    You may decide that some changes are too much work to bother to
    implement; you can disallow those changes in this step.
&lt;/ol&gt;

With a Config class that corresponds to each configurable operational class,
we can put the validation code directly in those classes rather than in
the operational classes.

&lt;br/&gt;&lt;br/&gt;
After completing the above validation steps,
and assuming there were no errors,
you have done all error checking and
know that you will be able to switch to the new config without errors,
but you have not yet done so.

&lt;br/&gt;&lt;br/&gt;
Errors in any of these steps should be collected so that they
are available for &lt;a href="#report"&gt;Reporting&lt;/a&gt;.

&lt;a name="activate"&gt;&lt;/a&gt;
&lt;h4&gt;Activate&lt;/h4&gt;

Assuming that the loaded Config objects pass all of your
validation tests,
it is time to activate the new Config.
While conceptually simple, this is the trickiest step.

&lt;br/&gt;&lt;br/&gt;
The key issue here is ensuring that the application works properly
in the presence of concurrent access to the config data.
You want to make sure that the application cleanly switches from
using the old configuration to using the new one,
without the possibility that some operations will be performed
with part of the old configuration and part of the new one.

&lt;br/&gt;&lt;br/&gt;
There are two basic updates you need to make,
which correspond to the two basic approaches to
&lt;a href="#use"&gt;using&lt;/a&gt; the data:
&lt;ol&gt;
&lt;li&gt;Update the active root Config singleton.
&lt;li&gt;Update all operational objects that contain configuration state.
&lt;/ol&gt;

Handling the first approach is pretty easy:
inside a synchronized block, update the active root Config singleton.
When another thread begins an operational sequence that relies on
any config parameters, it reads the current root Config singleton
(in a synchronized block)
and keeps it in a local variable for the duration of the operational
sequence.
All queries for config parameters during that sequence
are done against the local Config variable,
ensuring that the entire sequence uses a single
Config even if the Config singleton is updated in the middle of
that operational sequence.

&lt;br/&gt;&lt;br/&gt;
If you are using the second approach, updates are a bit trickier.
It would be simple if the activation thread could just
update the state in the operational objects,
but another thread may currently be running and using those
operational objects in an active operation.
You can't just update the state in all of the operational objects
from the activation thread because
the operational thread might then pick up the new state in the middle
of one operational sequence, and we assume that starting an operational
sequence with one state and finishing it with another state
will cause problems.

&lt;br/&gt;&lt;br/&gt;
The key to handling changes when using this second approach is to
build on how we solved changes to the first approach by capturing
the value of the active root Config singleton at the start of the
operational sequence.
That starting point is the point at which we know (by definition)
that it is safe to change over to a new config.
When we start our operational sequence,
we capture the currently active Config into a local variable,
as described above as the solution for changes to the first approach.
We then check to see if the config has changed
since the last time we started the sequence.
We do this by comparing our newly captured Config against the Config
that we used the previous time we executed our sequence,
which means we need a second variable that stores that previous Config.
If the newly captured Config is not the same as the previous Config
we used, then we reconfigure our operational objects according to
the newly captured Config,
then save that as well as the most recently used Config for the
next execution.

&lt;br/&gt;&lt;br/&gt;
When using the above solution,
if the only time you update the operational state is when a thread starts an
operational sequence,
and that thread waits for a long time before
beginning execution of the sequence,
then the switch of the operational state to the new config may not
happen for a long time.
Despite having validated our new config,
it is possible that, due to a bug, the new config will fail when we
attempt to apply it to our operational objects,
and it is generally better to have that happen immediately when the
config is activated rather than much later,
when it might not be obvious that the problem is due to the new config.
In order to avoid this situation,
you should add code to make your threads wake up and apply the
new config immediately after it is activated,
even if there is no other work for them to do.

&lt;br/&gt;&lt;br/&gt;
If you have multiple independent operational sequences you should
separately capture a copy of the active Config at the start of
each sequence.
However, you need to make sure that each sequence is in fact independent
of the others as far as the config parameters that each uses,
since when using the above approach you may end up with two threads
executing different sequences at the same time with one using the old
config and the other using the new config.

&lt;br/&gt;&lt;br/&gt;
If the different threads are related, such that it is not acceptable for
one thread to be using the new config while another is still using the
old config, then you will have to use a different approach.
In this case, you will probably need to write some code
to ensure that no thread starts using a new config until all threads
have stopped using the old config.

&lt;br/&gt;&lt;br/&gt;
You can do this with two flags, properly synchronized:
&lt;ol&gt;
&lt;li&gt;config-in-use
&lt;li&gt;ok-to-use-config
&lt;/ol&gt;

The activation thread turns off ok-to-use-config, then waits until
config-in-use is zero.
At that point it updates the root Config singleton and turns on
ok-to-use-config.

&lt;br/&gt;&lt;br/&gt;
The operational threads check ok-to-use-config before capturing the
current config.
If turned off, they wait until it is turned on.
They then increment config-in-use, use the config,
and decrement config-in-use when done.
Synchronized and try/catch blocks should be used to avoid
race conditions and ensure the config-in-use count doesn't get stuck on.

&lt;a name="report"&gt;&lt;/a&gt;
&lt;h4&gt;Report&lt;/h4&gt;

&lt;a href="http://jim-mcbeath.blogspot.com/2009/05/fun-with-feedback.html"&gt;
Feedback&lt;/a&gt; is important.
Ideally, the user should get the following feedback:
&lt;ul&gt;
&lt;li&gt;When loading of an updated config is triggered, the user should
    get feedback on whether or not the new configuration was activated.
&lt;li&gt;If the new configuration was not activated, the user should
    get feedback on why the new configuration was rejected
    (i.e. he should see a list of config errors).
&lt;li&gt;Ideally, at a later point in time
    it should be possible for the user to determine what configuration
    is currently being used and how long it has been active.
    This is useful in situations where an on-disk config was changed
    at some point in the past but not loaded into the application.
&lt;/ul&gt;

Generally the reporting feedback channel is related to the trigger
mechanism:
&lt;ul&gt;
&lt;li&gt;If you use a CLI command to trigger the reload, that command can
    print out the feedback.
&lt;li&gt;If you use a web page to trigger the reload,
    the web response page can display the feedback.
&lt;li&gt;If you use JMX, the feedback can be returned through that protocol.
&lt;li&gt;If you use a web URL, the HTTP response can include the feedback.
&lt;/ul&gt;

If you application does logging, the feedback can be logged to the
log file.  This can be done in addition to any of the above
feedback mechanisms.

&lt;a name="use"&gt;&lt;/a&gt;
&lt;h4&gt;Use&lt;/h4&gt;

It is important to ensure that the config parameters used
are consistent throughout an operational sequence,
even when a config reload occurs while that sequence is executing,
as discussed above in the
&lt;a href="#activate"&gt;Activate&lt;/a&gt; section above.
Once you have handled that, you can move on to other usage aspects.

&lt;br&gt;&lt;br/&gt;
There are two basic approaches to using the active config parameters:
&lt;ol&gt;
&lt;li&gt;Use the current Config object directly each time a config value is needed.
    This is suitable for simple options that are tested each time
    a specific behavior or feature is desired.
&lt;li&gt;Load data from the current Config object into operational objects.
    This is necessary when some of the config info refers to state
    that is managed by an operational object, such as the
    endpoint for a TCP connection.
&lt;/ol&gt;

The first approach provides for simpler updating of the config data,
but sometimes the second approach is necessary for performance reasons
or due to how state information is stored in other objects.
The timing for when to update config state in operational objects is
discussed in the &lt;a href="#activate"&gt;Activate&lt;/a&gt; section above.

&lt;br/&gt;&lt;br/&gt;
A minimal implementation of the Config object
would provide just a single method to retrieve
any parameter by name, such as is provided by the
&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html#getProperty%28java.lang.String%29"&gt;
Properties.getProperty&lt;/a&gt;
method.
While this is easy to implement,
it does not provide as much protection against programming errors
as other approaches described below.

&lt;br/&gt;&lt;br/&gt;
For type safety, you should implement (or use a package that provides)
a set of methods with specific return types that match the types of
your config parameters.
You can then pass in the name of each parameter and not have to
type-cast the result.

&lt;br/&gt;&lt;br/&gt;
For maximum type safety your Config object should provide methods
specific to each config parameter being retrieved.
This ensures not only that you have the correct return type for
the parameter, but that you have not accidentally mistyped a
parameter name in a call to retrieve its value.
(Of course, your unit tests should also catch this error, but
you will catch it sooner and more surely with compile-time checks.)

&lt;a name="unit-testing"&gt;&lt;/a&gt;
&lt;h3&gt;Unit Testing&lt;/h3&gt;

Keeping the configuration management code in separate
Config objects improves the testability of your code.
You can write unit tests for your Config objects to test that
they properly locate, load (or reload), and validate
config files,
and you can create a set of mock Config objects
that you can use to test how your application responds to
different configurations.

&lt;br/&gt;&lt;br/&gt;
A more complete test suite will include tests that verify proper
functionality when a reload operation is performed by one thread
while one or more other threads are in the middle of processing and
using config data.
However, a detailed discussion of this kind of multi-thread testing
is beyond the scope of this post.

&lt;a name="implementation"&gt;&lt;/a&gt;
&lt;h3&gt;Implementation Options&lt;/h3&gt;

You can write all of your own config code from scratch,
or you can leverage an existing package.
Whatever approach you take, you will want to ensure that your
application handles all seven of the &lt;a href="#steps"&gt;steps&lt;/a&gt;
discussed above.

&lt;br/&gt;&lt;br/&gt;
A few packages are listed below,
with a discussion of the steps for which they provide support.
For bullet items marked &lt;i&gt;no support&lt;/i&gt;
you will have to write your own code.
None of the packages provides support for all of the steps.
Even if a package did provide that support,
you must still provide application-specific code
for validation, activation, and use.

&lt;br/&gt;&lt;br/&gt;
&lt;b&gt;Caveat:&lt;/b&gt; Except for Properties,
I have not used the packages listed below.
My evaluation of their capabilities is based entirely on
reading the documentation and examining the source code,
so it is possible that I have made some mistakes in that evaluation.

&lt;ul&gt;
&lt;li&gt;&lt;a href="#imp-properties"&gt;Properties (Java)&lt;/a&gt;
&lt;li&gt;&lt;a href="#imp-javaconfig"&gt;JavaConfig (Java)&lt;/a&gt;
&lt;li&gt;&lt;a href="#imp-commons-config"&gt;Apache Commons Config (Java)&lt;/a&gt;
&lt;li&gt;&lt;a href="#imp-configgy"&gt;Configgy (Scala)&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="imp-properties"&gt;&lt;/a&gt;
&lt;h4&gt;Properties (Java)&lt;/h4&gt;

The standard Java library includes the
&lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html"&gt;
Properties&lt;/a&gt; class,
which can be used for simple applications that require only
a few parameters.

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Trigger&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Locate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Load&lt;/b&gt;: You can load a properties file with a single call to
    Properties.load, where you pass in the name of the file to load.
&lt;li&gt;&lt;b&gt;Validate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Activate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Report&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Use&lt;/b&gt;: The Properties.get method will return the value of a property
    as a String.
    You can use this directly as a generic call to retrieve
    config parameters by name,
    or you can layer your type-safe methods on top of this.
&lt;/ul&gt;

&lt;a name="imp-javaconfig"&gt;&lt;/a&gt;
&lt;h4&gt;JavaConfig (Java)&lt;/h4&gt;

&lt;a href="http://javaconfig.sourceforge.net/"&gt;
JavaConfig&lt;/a&gt;
(not to be confused with
&lt;a href="http://www.springsource.org/javaconfig"&gt;Spring JavaConfig&lt;/a&gt;,
which is used for Dependency Injection configuration)
reads config files using the standard Properties
file format.
The package provides a generic &lt;code&gt;Config&lt;/code&gt; class,
which you subclass to create your application-specific config class.
It handles a defined set of data types.

&lt;br/&gt;&lt;br/&gt;
JavaConfig specifically does not include any logging,
so that it can be used to read the configuration for another
logging package.

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Trigger&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Locate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Load&lt;/b&gt;: You pass the name of a properties file to the
    Config constructor, which loads the properties file.
&lt;li&gt;&lt;b&gt;Validate&lt;/b&gt;: After instantiating your config object,
    you call the &lt;code&gt;validateConfiguration&lt;/code&gt; method on it,
    which returns a &lt;code&gt;ConfigValidationResult&lt;/code&gt; object that contains
    the validation results.
    This &lt;code&gt;validateConfiguration&lt;/code&gt; method
    calls all of your getter methods.  For each of your methods that
    throws an exception, the message is collected and made available
    through the &lt;code&gt;ConfigValidationResult&lt;/code&gt; object.
&lt;li&gt;&lt;b&gt;Activate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Report&lt;/b&gt;: The &lt;code&gt;ConfigValidationResult&lt;/code&gt; class collects
    the error messages from all of your getter methods that throw exceptions,
    and makes them available
&lt;li&gt;&lt;b&gt;Use&lt;/b&gt;: The base &lt;code&gt;Config&lt;/code&gt; class provides type-safe
    methods such as &lt;code&gt;getInt&lt;/code&gt; and &lt;code&gt;getBoolean&lt;/code&gt;
    that accept a parameter name.
    In your config class that extends that class,
    you define a getter method for each of your config parameters.
    Each of your methods should call one of the underlying
    type-safe methods, passing it a config parameter name,
    and return that result.
    Your method should also perform any validation checks and
    throw an exception if there are any validation errors.
&lt;/ul&gt;

&lt;a name="imp-commons-config"&gt;&lt;/a&gt;
&lt;h4&gt;Apache Commons Config (Java)&lt;/h4&gt;

&lt;a href="http://commons.apache.org/configuration/"&gt;
Apache Commons Config&lt;/a&gt;
provides a mechanism to allow config info to be loaded from a
variety of sources, such as files or databases.
You can mix config info from multiple different sources, such as reading
some info from a database and some from system properties,
and access it all through a single config object.
It supports file includes and value substitution.

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Trigger&lt;/b&gt;: The package
    &lt;code&gt;org.apache.commons.configuration.reloading&lt;/code&gt;
    provides a mechanism for defining a reload strategy when
    using file-based configuration,
    such as reloading on access to a config element
    if the file has changed,
    and some support for using JMX to trigger a reload.
&lt;li&gt;&lt;b&gt;Locate&lt;/b&gt;: You can pass in a relative filename, and the package
    will look in various locations for a config file of that name to load.
&lt;li&gt;&lt;b&gt;Load&lt;/b&gt;: You can create a configuration object for a specific
    data source, such as a file, or you can create a composite configuration
    object from multiple other configuration objects.
&lt;li&gt;&lt;b&gt;Validate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Activate&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Report&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Use&lt;/b&gt;: There are a set of type-safe methods to which you pass an
    item name and receive back its value.
&lt;/ul&gt;

&lt;a name="imp-configgy"&gt;&lt;/a&gt;
&lt;h4&gt;Configgy (Scala)&lt;/h4&gt;

&lt;a href="http://www.lag.net/configgy/"&gt;Configgy&lt;/a&gt;
includes logging as well as configuration,
so it can use its own config files to configure logging.
Its config files look like a cross between an XML file
and a Properties file,
with hierarchy represented by XML syntax,
and individual parameters looking more like Properties.
It handles a defined set of data types
and has the ability to represent lists of values for a single parameter.

&lt;br/&gt;&lt;br/&gt;
Configgy supports a lot of options when defining parameters, including
hierarchy, inheritance, includes,
variable substitution (including system properties),
and conditional assignment.

&lt;br/&gt;&lt;br/&gt;
Note that Configgy allows the application to set values in the config
after it has been loaded into memory.
As with any situation in which one datastructure might be shared among
multiple threads, you should be very cautious with this capability.
In particular, if you have a thread which has read the config and used
that data to set state in its own application objects, setting the value
in the config object alone may not have the desired effect.
Your application can set up a subscriber for changes, which will be
called when there are runtime changes to a config value,
but you need to handle synchronization of these runtime changes
in the same manner as when reloading the entire config.
And your application code that is calling the &lt;code&gt;set&lt;/code&gt;
method must be prepared to handle a thrown exception if a subscriber
decides the change is invalid.

&lt;br/&gt;&lt;br/&gt;
The examples given in the Configgy documentation use a Scala object
(as opposed to class) and does not discuss reloading, but there
is a &lt;code&gt;reload&lt;/code&gt; method available on the main object, which should
work if you use the approach described above (using a pair of flags)
for the case when all threads are related.
Also, there are separate config objects being used under the covers,
so it should be possible to use those directly, rather than the main object,
if you want to be able to switch some threads over to your new config
while some other threads continue to use the old config.

&lt;br/&gt;&lt;br/&gt;
If you are writing a Scala application, Configgy is probably your best
option.

&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Trigger&lt;/b&gt;: There is some JMX support built in;
    reload is not one of the methods available from the JMX interface,
    but it should not be too difficult to add it.
&lt;li&gt;&lt;b&gt;Locate&lt;/b&gt;: Configgy has calls to allow you to set the location of
    the config file to load.
    You can call this before calling &lt;code&gt;reload()&lt;/code&gt;
    to control the source for the reload.
&lt;li&gt;&lt;b&gt;Load&lt;/b&gt;: You call Configgy with a filename and it loads that file
    and any file referenced with an &lt;code&gt;include&lt;/code&gt; statement.
&lt;li&gt;&lt;b&gt;Validate&lt;/b&gt;: Configgy uses a subscription/callback model to let
    the application know when data has been changed.
    Your callback is called with an argument that tells you whether
    Configgy is doing a validation pass or an activation ("commit") pass.
    On the validation pass, your callback can throw an exception to
    indicate that the new value fails validation.
&lt;li&gt;&lt;b&gt;Activate&lt;/b&gt;: The validate/commit subscription model provides hooks
    to allow you to write your own validation and activation, but
    you still need to consider synchronization when using multiple threads.
&lt;li&gt;&lt;b&gt;Report&lt;/b&gt;: &lt;i&gt;no support&lt;/i&gt;.
&lt;li&gt;&lt;b&gt;Use&lt;/b&gt;: There is a set of type-safe methods to which you pass
    a parameter name, which can be hierarchical.
&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-6930718009174183960?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/6930718009174183960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=6930718009174183960' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/6930718009174183960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/6930718009174183960'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2010/01/reload-that-config-file.html' title='Reload That Config File'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-8361572793989543861</id><published>2009-12-03T20:14:00.000-08:00</published><updated>2009-12-03T20:14:19.725-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><title type='text'>Improve Your Releases</title><content type='html'>There is more to a good software release than a good program.

&lt;br/&gt;&lt;br/&gt;
If you are releasing software and want it to be successful,
you have to do more than just write a
&lt;a href="http://jim-mcbeath.blogspot.com/2008/10/software-quality-dimensions.html"&gt;
good program&lt;/a&gt;.
You need to consider all of the things the user will want to do
with your software before and after actually using it.

&lt;br/&gt;&lt;br/&gt;
You can look at the steps below as an interpretation of the phases of the
&lt;a href="http://en.wikipedia.org/wiki/Software_development_process"&gt;
software lifecycle&lt;/a&gt;
from the perspective of the user.
Depending on how well you do your job,
the user will have a better or worse experience with each of these phases.
If, for one of these phases, you do nothing,
the user is likely to have an unpleasant experience when he gets to that phase.

&lt;ul&gt;
&lt;li&gt;&lt;a href="#develop"&gt;Develop&lt;/a&gt;
&lt;li&gt;&lt;a href="#release"&gt;Release&lt;/a&gt;
&lt;li&gt;&lt;a href="#distribute"&gt;Distribute&lt;/a&gt;
&lt;li&gt;&lt;a href="#install"&gt;Install&lt;/a&gt;
&lt;li&gt;&lt;a href="#support"&gt;Support&lt;/a&gt;
&lt;li&gt;&lt;a href="#upgrade"&gt;Upgrade&lt;/a&gt;
&lt;li&gt;&lt;a href="#patch"&gt;Patch&lt;/a&gt;
&lt;li&gt;&lt;a href="#migrate"&gt;Migrate&lt;/a&gt;
&lt;li&gt;&lt;a href="#uninstall"&gt;Uninstall&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="develop"&gt;&lt;/a&gt;
&lt;h3&gt;Develop&lt;/h3&gt;

This is the phase that most open-source developers focus on.
If we were looking at the software life cycle in a little more
detail, we would split this phase into three separate phases:
design, code, and test.
In commercial development these three phases are often
handled by separate groups, but from the user's perspective
they can be lumped together as being the factors that contribute
to the overall quality and usability of the software.

&lt;br/&gt;&lt;br/&gt;
This is the time in which to consider all of the points below
so that you can create your system in a way that makes it easy
to do the right thing for all of the other phases of the software
life cycle.

&lt;a name="release"&gt;&lt;/a&gt;
&lt;h3&gt;Release&lt;/h3&gt;

Once the software comes out of test, it must be packaged up as a Release.
It is useful for a user easily to be able to tell what released artifact
he has acquired and what version of that artifact he has.
The simplest way to handle this is to define a released artifact as being
a single file.
If you think you need to release a collection of files,
they should be packaged up as a single file, such as in
&lt;a href="http://www.pkware.com/support/zip-application-note"&gt;zip&lt;/a&gt;,
&lt;a href="http://en.wikipedia.org/wiki/Tar_(file_format)#Compression_and_naming"&gt;
tgz&lt;/a&gt; (tar-gzip),
&lt;a href="http://en.wikipedia.org/wiki/Apple_Disk_Image"&gt;dmg&lt;/a&gt; or
&lt;a href="http://en.wikipedia.org/wiki/ISO_image"&gt;iso&lt;/a&gt; format.
You can then give each file a name and version number to allow the user
to identify it.

&lt;br/&gt;&lt;br/&gt;
You may have a product that is composed of a number of other
released artifacts.
You can bundle these into one larger artifact that is a collection
of the other artifacts
plus an installer that can invoke the installers of the other artifacts,
or that knows how to install those other artifacts directly.
Operating system installers work in this way.

&lt;br/&gt;&lt;br/&gt;
Once your released artifact is in a single file and appropriately labeled,
it is easy to take the next step:
generate and publish a checksum for that file.
If, for any reason, the user is unsure about what artifact and version he
has, he can then run a checksum on his file and compare it against your
official list of checksums.
Using a
&lt;a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function"&gt;
cryptographic checksum&lt;/a&gt;
provides protection not only against accidental corruption,
but against intentional modification (hacking) of the artifact as well.
Depending on the level of security desired, you can use
&lt;a href="http://en.wikipedia.org/wiki/MD5"&gt;md5&lt;/a&gt;,
&lt;a href="http://en.wikipedia.org/wiki/SHA_hash_functions"&gt;sha1&lt;/a&gt;, or
&lt;a href="http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf"&gt;
sha256&lt;/a&gt; for your checksum.
Operating system distributions such as
&lt;a href="http://fedoraproject.org/get-fedora"&gt;Fedora&lt;/a&gt; do
&lt;a href="https://fedoraproject.org/static/checksums/Fedora-12-i386-CHECKSUM"&gt;
this&lt;/a&gt;, including the additional security step that the published list
of checksum values is
&lt;a href="http://en.wikipedia.org/wiki/Digital_signature"&gt;
digitally signed&lt;/a&gt;.

&lt;a name="distribute"&gt;&lt;/a&gt;
&lt;h3&gt;Distribute&lt;/h3&gt;

Your user needs to get your released artifacts.
Long ago this used to be done by distributing physical media such
as DVDs, CDs, floppies, or tapes.
Today most distribution is done over the internet,
which makes this step far simpler than it used to be.

&lt;br/&gt;&lt;br/&gt;
Open source projects have easy solutions available through such services as
&lt;a href="http://sourceforge.net/"&gt;sourceforge&lt;/a&gt; and
&lt;a href="http://github.com/"&gt;github&lt;/a&gt;.
Many commercial providers also distribute their software from
download pages on their web sites,
often with additional security such as restricting web access to
customers with accounts,
and using license files to enable specific
functionality in the installed software.

&lt;br/&gt;&lt;br/&gt;
Given how widespread and well-understood this model is,
it makes sense to use it for internal software as well:
set up a web site where your users can find all of your released artifacts.
If the list of artifacts is small,
you can just set up a few directories with files in them and serve up
those files with your web server.
When the number of artifacts gets large enough so that browsing listings
gets cumbersome, you can add a search form.
If you need to restrict which of your internal users have access to
your downloads, you can do that in the same way as commercial vendors do,
with password access or license-file control of the installed application.

&lt;a name="install"&gt;&lt;/a&gt;
&lt;h3&gt;Install&lt;/h3&gt;

The user should be able to install the complete application
from the downloaded artifact
with a single command, or at most two commands
(an unpack command followed by execution of a setup script).
Installing a Windows application is generally done by downloading an
&lt;a href="http://support.microsoft.com/kb/65122"&gt;exe&lt;/a&gt;
file and then executing it;
a Mac install is generally
&lt;a href="http://www.ofzenandcomputing.com/zanswers/779"&gt;done&lt;/a&gt;
by downloading a dmg file,
double clicking on it, and dragging the app into another folder;
a Java install is often done by downloading a
&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html"&gt;jar&lt;/a&gt; file
and then executing it (such as by running &lt;code&gt;java -jar&lt;/code&gt; on it).
These are all examples of simple installation mechanisms.
Once running, an installer can direct the user to select values for
options and installation paths.

&lt;br/&gt;&lt;br/&gt;
In particular, you should not require the user to unpack the software
and then manually execute a number of other steps such as moving files
around or editing config files.
These are steps that should be handled by an installer.

&lt;br/&gt;&lt;br/&gt;
When the files are installed on the user's system,
there should be an easy way to determine what version is installed and
in use.
This information should be easily available in the application,
such as in an &lt;code&gt;About&lt;/code&gt; menu in a GUI application.
If a user might install multiple artifacts from your collection,
there should be a simple way to get a list of all artifacts installed
and in use along with their version numbers so that you can
unambiguously tell what versions of your software are being used at that site.

&lt;a name="support"&gt;&lt;/a&gt;
&lt;h3&gt;Support&lt;/h3&gt;

Finally, the user is using your software.
If your software is well designed, well written and well tested,
the user should have no problems using it and all will be well.
In reality, it is unlikely that no user will ever have any problems
with your software.
When a user does have problems, what will he do
(other than grumble or swear at your software, that is)?
Assuming the user is motivated to solve the problem rather than just giving up,
he will seek out resources that can provide him with the information he needs
to solve his problem.
You can make his life easier in this step by providing some or all
of the following:
&lt;ul&gt;
&lt;li&gt;A Users Guide or set of guides (tutorial, reference).
&lt;li&gt;In-application help (context-specific, page-specific, links to the manual,
    search, how-to).
&lt;li&gt;On-line forums where users can share their problems and solutions.
&lt;li&gt;Direct support, via telephone, email, or chat.
&lt;/ul&gt;

If a user experiences a crash or runs into a bug,
it might be nice if he can easily submit a crash report or a bug report
so that you can more effectively fix the problem.
If so, you will want that report to automatically include the list of installed
artifacts and versions, as discussed above in the
&lt;a href="#install"&gt;Install&lt;/a&gt; section.

&lt;a name="upgrade"&gt;&lt;/a&gt;
&lt;h3&gt;Upgrade&lt;/h3&gt;

If your software is successful
you will probably release new versions of it.
A user who is already using your software should be able to start using
the new version of your software with minimal hassle.
As with the initial install, installing an upgrade should be done
with at most one or two commands,
as it could be with an upgrader that guides the user through whatever
questions need to be answered for the upgrade.

&lt;br/&gt;&lt;br/&gt;
You can add an option to your application to check for upgrades
and ask the user if he wants to download and install them,
saving the user the hassle of separately doing those steps.
If you choose to implement this, you should allow the user to disable it.
There should also still be a way that the user can download an upgrade
(as a single file, just as with an initial install),
copy it to another machine, and install it there,
in case he is running on a machine that is not connected to the network
or is behind a firewall that prevents your automated download from working.

&lt;br/&gt;&lt;br/&gt;
There are two ways in which an upgrade is different from an install,
leading to two additional goals for the upgrader:
&lt;ol&gt;
&lt;li&gt;If the user has any configuration or customization,
    that should be carried over to the new version.
&lt;li&gt;If the user starts running the new version and soon discovers
    that it is unusable for him,
    he should quickly be able to roll back to the previous version.
&lt;/ol&gt;

An approach to handle the first goal is to keep the configuration
and customization in a separate directory,
such as in the user's home directory,
or (for Unix systems) in /etc
or (for Windows systems) in the
&lt;a href="http://support.microsoft.com/kb/256986"&gt;Registry&lt;/a&gt;.
There can still be problems when upgrading if the format of the config
and customization files changes, or if the items being configured
and customized have changed between versions.
Your upgrader should take care of this.

&lt;br/&gt;&lt;br/&gt;
One relatively easy way to satisfy the second goal is to install each
version of the application in a separate directory that contains the
version number in the name, then providing a &lt;code&gt;current&lt;/code&gt;
directory that is a link to the version to be used.
Rolling back to a previous version might then be as simple as
deleting the &lt;code&gt;current&lt;/code&gt; link and recreating it to point
to the previous version.
Ideally, however, this rollback is also done by a program you provide,
in case a rollback also requires any other changes such as to the
configuration and customization files.

&lt;br/&gt;&lt;br/&gt;
The upgrade and rollback should of course update the list of
installed artifacts and current versions.

&lt;a name="patch"&gt;&lt;/a&gt;
&lt;h3&gt;Patch&lt;/h3&gt;

Occasionally you might want to deliver a minor update or bug fix to
your software.
You might send out one modified file and ask the user to install it
in a specific location to fix a bug.

&lt;br/&gt;&lt;br/&gt;
While this sounds like an easy mechanism for quick fixes,
in the long run you will be better off
ensuring that your upgrade process is streamlined enough that you can
package up that one file in an upgrade and use your upgrade process.

&lt;br/&gt;&lt;br/&gt;
The problem with sending out patch files and doing ad-hoc installs like this
is that it makes it very difficult
to keep track of what is installed at a customer site.
If you send out four or five patches and then the customer starts reporting
unique bugs, will you know what software is running at that site so
you can track down those bugs?
You could work on setting up a system to keep track of those patches,
but you might as well invest that effort into making your upgrade
process easier to use.

&lt;br/&gt;&lt;br/&gt;
Perhaps you think that each customer will have a different set of patches,
and you don't want to send the same patches to all of your customers,
so you don't want to make them all standard upgrades.
If it is really the case that you want to deliver different things to
different customers,
then you are not really delivering one artifact,
you are delivering separate artifacts to each customer.
In this case, you should just call them different artifacts,
give them their own version numbers, and send out upgrades for
those separate artifacts.
In that way you can continue to use your standard upgrade process,
and you can always know exactly what your customer has by collecting
the list of artifacts and their version numbers for all of the
artifacts installed at a customer site.

&lt;br/&gt;&lt;br/&gt;
If you really think you need to send out patches,
consider the following goals:
&lt;ul&gt;
&lt;li&gt;It should be easy for the user to install the patch with a single command.
&lt;li&gt;It should be difficult for the user to make a mistake when installing
    the patch, such as could happen if he has to manually install files
    into specific directories or manually edit any files.
&lt;li&gt;It should be easy for the user to rollback the patch if it doesn't work.
&lt;li&gt;It should be possible for both you and the user
    to know exactly what version of software is installed at the site,
    including what patches have been applied,
    even if there is a patch of a patch.
&lt;/ul&gt;

If it seems to you that implementing a patch mechanism that does all of
this is easier than adding some improvements to your upgrade process
and perhaps dividing up a couple of your artifacts
to more accurately reflect how you are actually installing them,
then go for it.

&lt;a name="migrate"&gt;&lt;/a&gt;
&lt;h3&gt;Migrate&lt;/h3&gt;

At some point one of your users might decide that he wants to stop
using your software and move to some other package.
If you are a commercial software provider you might think this is not
something that should be in your list of goals - why should you help
out a competitor? - but if you are interested in doing what is best for
your user, you should at least recognize this phase of the software lifecycle
and make a conscious decision about it.
The better you treat a leaving customer,
the more likely it is that he will some day be a returning customer.

&lt;br/&gt;&lt;br/&gt;
To support your users in this step, you should provide export tools
that allow the user to export all of his data from your application
in a standard format.
Depending on the application, this might mean exporting a
&lt;a href="http://tools.ietf.org/rfc/rfc4180.txt"&gt;CSV&lt;/a&gt; file,
an &lt;a href="http://www.w3.org/XML/"&gt;XML&lt;/a&gt; file,
an &lt;a href="http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=office"&gt;
Open Document&lt;/a&gt; file,
or something else.

&lt;br&gt;&lt;br&gt;
If you also implement an import capability that reads the same standard
file format as your export produces,
this could help you in the future
if you ever change your internal storage representation
from one version to the next:
just export from the old version into a file using the standard format,
upgrade to the new version, and import that file.

&lt;a name="uninstall"&gt;&lt;/a&gt;
&lt;h3&gt;Uninstall&lt;/h3&gt;

Whether or not a user chooses to move to a different product,
he may eventually decide he is done using your software and
he would like to remove it from his system.
As with the install,
it should be possible for the user to uninstall your software
with a single command.
If an application was installed simply by unpacking it,
that single command might be to remove that unpacked directory.
With a more complicated installation, uninstallation is likely
also to be more complicated, making an uninstaller program more important.

&lt;br/&gt;&lt;br/&gt;
If you have set up your application such that the user-customized portions
are separate from the standard install,
your uninstaller can give the user the option of keeping those portions.
Similarly, if the application maintains user data in its own directories,
you should get confirmation from the user before deleting those files
and give the user the option of keeping them.

&lt;br/&gt;&lt;br/&gt;
You might also want to consider how you want your installer to behave if
the user runs the uninstaller, keeps his customizations and data,
then runs the installer.
A user might want to do this to downgrade to a previous version if you
do not otherwise provide a simple solution for that.
Or perhaps you treated a departing customer well enough that he
is now returning to your product,
in which case he might be pleased to find that his old preferences and
customizations are still available.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-8361572793989543861?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/8361572793989543861/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=8361572793989543861' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8361572793989543861'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/8361572793989543861'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/12/improve-your-releases.html' title='Improve Your Releases'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-997826125817827716</id><published>2009-11-04T21:41:00.000-08:00</published><updated>2009-11-04T21:41:08.515-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Overriding vals as Optional Parameters</title><content type='html'>For simple cases you can use Scala vals, selectively
overridden, as a way of implementing optional parameters.
Overriding can also be used for other interesting tricks.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#optional-class-parameters"&gt;Optional Class Parameters&lt;/a&gt;
&lt;li&gt;&lt;a href="#optional-trait-parameters"&gt;Optional Trait Parameters&lt;/a&gt;
&lt;li&gt;&lt;a href="#early-definition"&gt;Early Definition&lt;/a&gt;
&lt;li&gt;&lt;a href="#required-trait-parameters"&gt;Required Trait Parameters&lt;/a&gt;
&lt;li&gt;&lt;a href="#abstract-class-parameters"&gt;Abstract Class Parameters&lt;/a&gt;
&lt;li&gt;&lt;a href="#type-parameters"&gt;Type Parameters&lt;/a&gt;
&lt;li&gt;&lt;a href="#caveats"&gt;Caveats&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="optional-class-parameters"&gt;&lt;/a&gt;
&lt;h3&gt;Optional Class Parameters&lt;/h3&gt;

In Java, a typical idiom for initializing an object that
has a large number of optional parameters, of which only a
few usually get set, is to construct the object and then
call setter functions to customize each of the optional
parameters.
While this technique can be convenient, it leaves open the
possibility
that the setter might get called later on in the objects
lifecycle at a time when changing that value could cause
problems.

&lt;br/&gt;&lt;br/&gt;
One solution to this problem is to use the builder pattern.
This solution is available in Scala as well, and can be taken
a step farther than in Java by using the
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-4.html"&gt;
type-safe builder&lt;/a&gt; pattern.

&lt;br/&gt;&lt;br/&gt;
The type-safe builder can be overly complicated for many situations.
Sometimes it would be nice to have something simpler than even
the simplest of builders.

&lt;br/&gt;&lt;br/&gt;
Scala 2.8 will have named parameters with default values,
which will make it pretty easy to create classes that have
optional parameters,
although you might not want to do this if you have 30 optional parameters.
Meanwhile, there is another approach you can use:
overriding &lt;code&gt;val&lt;/code&gt;s.

&lt;br/&gt;&lt;br/&gt;
The approach is pretty simple:
you define a base class with a constructor that includes all of the
required parameters, and you then add a &lt;code&gt;val&lt;/code&gt; for each
of the optional parameters.
When you want to create an instance of that class that sets some
of the optional parameters, you create an anonymous subclass by adding
a set of braces after the &lt;code&gt;new&lt;/code&gt; statement
that creates the instance, and inside
the braces you override each &lt;code&gt;val&lt;/code&gt; that you want to set.

&lt;br/&gt;&lt;br/&gt;
In this example we define a &lt;code&gt;Car&lt;/code&gt; class that represents
a few pieces of information about a car.
&lt;code&gt;model&lt;/code&gt; and &lt;code&gt;color&lt;/code&gt; are required parameters
and appear in our constructor.
Our optional parameters are &lt;code&gt;hasRadio&lt;/code&gt; and &lt;code&gt;hasSunRoof&lt;/code&gt;,
so we make those
&lt;code&gt;val&lt;/code&gt;s rather than constructor parameters, and we assign them their
default values.
We include a &lt;code&gt;toString&lt;/code&gt; method so we can easily see
the results.

&lt;br/&gt;&lt;br/&gt;
&lt;input id="hlButton" type="submit" value="Highlight Syntax" onclick="highlightSyntaxFlipButton()"/&gt;

&lt;pre name="hlcode" class="scala"
&gt;class Car(model:String, color:String) {
    val hasRadio = false
    val hasSunRoof = false

    override def toString() = {
        "Car{"+
            "model="+model+ 
            ",color="+color+
            (if (hasRadio) ",hasRadio" else "")+
            (if (hasSunRoof) ",hasSunRoof" else "")+
        "}"
    }
}
&lt;/pre&gt;

The normal use would be to call the constructor with no additional arguments:
&lt;pre name="hlcode" class="scala"
&gt;val c1 = new Car("Ford", "red")
println(c1)

//Car{model=Ford,color=red}
&lt;/pre&gt;

To specify one of our optional arguments, we add a code block to the
&lt;code&gt;new&lt;/code&gt; call, which creates an anonymous subclass in which
our &lt;code&gt;val&lt;/code&gt; overrides the default:
&lt;pre name="hlcode" class="scala"
&gt;val c2 = new Car("Chevy", "blue") { 
    override val hasRadio = true 
}   
println(c2)

//Car{model=Chevy,color=blue,hasRadio}
&lt;/pre&gt;

We can pass in values from the caller's context rather than constants:
&lt;pre name="hlcode" class="scala"
&gt;val myHasSunRoof = true
val c3 = new Car("Honda", "white") {
    override val hasSunRoof = myHasSunRoof
}
println(c3)

//Car{model=Honda,color=white,hasSunRoof}
&lt;/pre&gt;

&lt;a name="optional-trait-parameters"&gt;&lt;/a&gt;
&lt;h3&gt;Optional Trait Parameters&lt;/h3&gt;

You can use this same approach to pass in values for
instance variables in traits, which don't have constructor parameters.
For example, say we define a trait for an optional &lt;code&gt;Touring&lt;/code&gt;
package for our car:
&lt;pre name="hlcode" class="scala"
&gt;trait Touring {
    val hasNavSystem = false
    val hasExtraSuspension = false
    val hasTowHitch = false
    val hasRunningBoards = false

    override def toString() = {
        super.toString()+
            "+Touring{"+
            (if (hasNavSystem) "navSystem," else "") +
            (if (hasExtraSuspension) "extraSuspension," else "") +
            (if (hasTowHitch) "towHitch," else "") +
            (if (hasRunningBoards) "runningBoards," else "") +
        "}"
    }
}
&lt;/pre&gt;

Now we can create an instance of a &lt;code&gt;Car with Touring&lt;/code&gt;
and pass in values for some of those "optional constructor parameters"
defined in the &lt;code&gt;Touring&lt;/code&gt; trait:

&lt;pre name="hlcode" class="scala"
&gt;val c4 = new Car("Honda","white") with Touring {
    override val hasSunRoof = true      //from Car
    override val hasNavSystem = true    //from Touring
    override val hasRunningBoards = true  //from Touring
}

println(c4)

//Car{model=Honda,color=white,hasSunRoof}+Touring(hasNavSystem,hasRunningBoards,}
&lt;/pre&gt;

&lt;b&gt;NOTE:&lt;/b&gt;
Due to a bug in older versions of Scala, at least through 2.7.6,
overriding a &lt;code&gt;val&lt;/code&gt; on a trait as in the above example does not work.
This does work properly in Scala 2.8.0 (at least it does in the
20091006 nightly build).

&lt;a name="early-definition"&gt;&lt;/a&gt;
&lt;h3&gt;Early Definition&lt;/h3&gt;

You may have a situation in which some of the &lt;code&gt;val&lt;/code&gt;s that you are
initializing in a trait or class depend on other &lt;code&gt;val&lt;/code&gt;s.
In this case, overriding a &lt;code&gt;val&lt;/code&gt; as we did above may not give you
the result you want:
the initializer of the superclass runs to completion before the
initializer of the subclass,
which means all of the vals in the superclass get set before any
of the overriding vals are evaluated.

&lt;br/&gt;&lt;br/&gt;
For example, say we modify our Touring trait by adding a
&lt;code&gt;maxTowWeight&lt;/code&gt; value, as shown in bold below:
&lt;pre name="hlcode" class="scala"
&gt;trait Touring {
    val hasNavSystem = false
    val hasExtraSuspension = false
    val hasTowHitch = false
    val hasRunningBoards = false
    &lt;b&gt;val maxTowWeight = if (!hasTowHitch) 0 else
        { if (hasExtraSuspension) 1500 else 1000 }&lt;/b&gt;

    override def toString() = {
        super.toString()+
            "+Touring{"+
            (if (hasNavSystem) "navSystem," else "") +
            (if (hasExtraSuspension) "extraSuspension," else "") +
            (if (hasTowHitch) "towHitch," else "") +
            (if (hasRunningBoards) "runningBoards," else "") +
            &lt;b&gt;"maxTowWeight="+maxTowWeight +&lt;/b&gt;
        "}"
    }
}
&lt;/pre&gt;
When we instantiate a &lt;code&gt;Car with Touring&lt;/code&gt; the constructor
code for &lt;code&gt;Touring&lt;/code&gt; executes before the constructor code
for the new class.
In particular, &lt;code&gt;val maxTowWeight&lt;/code&gt; gets evaluated before
the overriding values are evaluated, so it always ends up with a
value of zero:
&lt;pre name="hlcode" class="scala"
&gt;val c5 = new Car("Honda","white") with Touring { override val hasTowHitch = true }

println(c5)

//Car with Touring = Car{model=Honda,color=white,hasRadio=false,hasSunRoof=false}+Touring{towHitch,maxTowWeight=0}
&lt;/pre&gt;

Scala provides a mechanism to address this issue:
Early Definition
(&lt;a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"
&gt;Scala Language Specification&lt;/a&gt;, section 5.1.6).
The &lt;code&gt;val&lt;/code&gt;s
that you specify in the Early Definition block are evaluated
in the context of the calling class,
then that set of values is placed into the context of the new class
being instantiated such that all of those values are available at
the beginning of the process of instantiation,
even before the initializer for &lt;code&gt;Object&lt;/code&gt; is executed.
In this way, any expression which uses one of those vals will have
access to the value provided in the Early Definition.

&lt;br/&gt;&lt;br/&gt;
It could be used with our &lt;code&gt;Car&lt;/code&gt; example like this:
&lt;pre name="hlcode" class="scala"
&gt;val c6 = new { override val hasTowHitch = true } with Car("Honda","white") with Touring

println(c6)

//Car with Touring = Car{model=Honda,color=white,hasRadio=false,hasSunRoof=false}+Touring{towHitch,maxTowWeight=1000}
&lt;/pre&gt;

A class definition for the above example could look like this:
&lt;pre name="hlcode" class="scala"
&gt;class TouringCarWithHitch(name:String, color:String) extends {
            override val hasTowHitch = true
        } with Car(name,color) with Touring {
    //normal class overrides and additional elements here
}

val c7 = new TouringCarWithHitch("Honda","white")
//c7 is the same as c6 (but we have not implemented ==)
&lt;/pre&gt;

&lt;a name="required-trait-parameters"&gt;&lt;/a&gt;
&lt;h3&gt;Required Trait Parameters&lt;/h3&gt;

If you want to define a trait that has required parameters rather than
optional parameters, you can omit the value from the declarations
and instead specify only the type,
which causes the &lt;code&gt;val&lt;/code&gt; to be abstract.
For example, if we want to make the &lt;code&gt;hasTowHitch&lt;/code&gt;
and &lt;code&gt;hasNavSystem&lt;/code&gt;
parameters to our modified Touring trait be required,
that would look like this:
&lt;pre name="hlcode" class="scala"
&gt;trait Touring {
    val hasNavSystem&lt;b&gt;:Boolean   //abstract (no value)&lt;/b&gt;
    val hasExtraSuspension = false
    val hasTowHitch&lt;b&gt;:Boolean    //abstract (no value)&lt;/b&gt;
    val hasRunningBoards = false
    val maxTowWeight = if (!hasTowHitch) 0 else
        { if (hasExtraSuspension) 1500 else 1000 }

    override def toString() = {
        super.toString()+
            "+Touring{"+
            (if (hasNavSystem) "navSystem," else "") +
            (if (hasExtraSuspension) "extraSuspension," else "") +
            (if (hasTowHitch) "towHitch," else "") +
            (if (hasRunningBoards) "runningBoards," else "") +
            "maxTowWeight="+maxTowWeight +
        "}"
    }
}
&lt;/pre&gt;

Now when we declare a concrete instance of this class, we are required to
define values for those two variables else we will get a compiler error.
Since the base declaration is now abstract, we omit the
&lt;code&gt;override&lt;/code&gt; keyword on those &lt;code&gt;val&lt;/code&gt;s:

&lt;pre name="hlcode" class="scala"
&gt;val c8 = new Car("Honda","white") with Touring {
    override val hasSunRoof = true      //from Car
    val hasNavSystem = true             //from Touring; required
    override val hasRunningBoards = true  //from Touring; optional
    val hasTowHitch = false             //from Touring; required
}

println(c8)

//Car{model=Honda,color=white,hasSunRoof}+Touring(hasNavSystem,hasRunningBoards,maxTowWeight=0}
&lt;/pre&gt;

&lt;a name="abstract-class-parameters"&gt;&lt;/a&gt;
&lt;h3&gt;Abstract Class Parameters&lt;/h3&gt;

Sometimes it is convenient to use an abstract &lt;code&gt;val&lt;/code&gt; rather than a
constructor parameter for abstract classes.
For example, say you have a Service and you want to define a set of
case classes for service messages.
The base class should have a reference to the Service object so that it
can easily be processed by generic service methods,
but each case class should also have the same reference as a case
value for easy matching.
For consistency, since these are the same value, the name should be the same.
You could do this by defining the base class with one parameter
declared as a &lt;code&gt;val&lt;/code&gt; to make it accessible, then
define the case classes to override that value,
like this:
&lt;pre name="hlcode" class="scala"
&gt;abstract class Service
abstract class ServiceMessage(val service:Service)
case class ServiceStart(override service:Service) extends ServiceMessage(service)
case class ServiceStop(override service:Service) extends ServiceMessage(service)
&lt;/pre&gt;

The case class automatically adds a &lt;code&gt;val&lt;/code&gt; keyword to each
of our parameters,
so we need to specify the &lt;code&gt;override&lt;/code&gt; keyword,
but can omit the &lt;code&gt;val&lt;/code&gt; keyword.

&lt;br/&gt;&lt;br/&gt;
We can simplify our case classes a bit by changing the base class
&lt;code&gt;val&lt;/code&gt; from a constructor parameter to an abstract
&lt;code&gt;val&lt;/code&gt;, like this:

&lt;pre name="hlcode" class="scala"
&gt;abstract class Service
abstract class ServiceMessage { val service:Service }
case class ServiceStart(service:Service) extends ServiceMessage
case class ServiceStop(service:Service) extends ServiceMessage
&lt;/pre&gt;

Not only have we dropped the &lt;code&gt;override&lt;/code&gt; keyword,
but we are also not passing the &lt;code&gt;service&lt;/code&gt; parameter to
the superclass.
The implied &lt;code&gt;val&lt;/code&gt; keyword on the case class parameters
creates a concrete instance of the &lt;code&gt;service&lt;/code&gt; parameter
that overrides the abstract value defined in the base class.

&lt;a name="type-parameters"&gt;&lt;/a&gt;
&lt;h3&gt;Type Parameters&lt;/h3&gt;

Just as scala has value parameters, concrete value members and
abstract value members,
it likewise has type parameters, concrete type members and
abstract type members.
The approach used above on values can generally by applied to types as well:
rather than defining a class with a type parameter,
you can often define that class with a type member.
If the type is a required type that must be overridden by the
extending class, make the type member abstract;
if you want the subclass to be able to default to the type used in
the superclass, use a concrete type and let the subclass use the
&lt;code&gt;override&lt;/code&gt; keyword if it wants to override that type.

&lt;br/&gt;&lt;br/&gt;

Bill Venners has a nice
&lt;a href="http://www.artima.com/weblogs/viewpost.jsp?thread=270195"&gt;
blog post&lt;/a&gt; where he discusses the question of when to use a
type parameter and when to use an abstract type member,
with a reference to an
&lt;a href="http://www.artima.com/scalazine/articles/scalas_type_system.html#abstracttypes"&gt;
interview with Martin Odersky&lt;/a&gt;
where he talks about
abstract type members in comparison to instance variables.

&lt;a name="caveats"&gt;&lt;/a&gt;
&lt;h3&gt;Caveats&lt;/h3&gt;

Although in many ways you are free to choose between using a
constructor parameter versus a class member, they are not entirely equivalent.
In particular, once you start building up class hierarchies using
abstract and concrete members with overrides,
you have to be careful that the initialization order is what you expect.
In the Early Definition &lt;a href="#early-definition"&gt;section&lt;/a&gt; above
I gave one example of how values can fail to initialize correctly
due to ordering issues.
That one is pretty easy to understand, but they can sometimes
be far more subtle and hard to spot.

&lt;br/&gt;&lt;br/&gt;
One thing you can do that will sometimes fix such problems is to use
the &lt;code&gt;lazy&lt;/code&gt; keyword on your value members in order to
get lazy initialization.
This causes initialization of the value to be delayed until
the first time it is used,
rather than being eagerly initialized when the class is initialized.
Note that if you declare a concrete variable as &lt;code&gt;lazy&lt;/code&gt;,
then an overriding
instance of that variable must also be declared as &lt;code&gt;lazy&lt;/code&gt;;
if the original concrete variable is not &lt;code&gt;lazy&lt;/code&gt;,
the overriding variable
can not be &lt;code&gt;lazy&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
Note that overriding a &lt;code&gt;val&lt;/code&gt; in Scala is not the same as
declaring a variable of the same name in a subclass in Java.
Consider this Java test program &lt;code&gt;Test.java&lt;/code&gt;:
&lt;pre name="hlcode" class="java"
&gt;public class Test {
    public static void main(String[] args) {
        (new Test1()).test1();
        (new Test2()).test1();
        (new Test2()).test2();
    }
}

class Test1 {
    public int t = 1;

    public void test1() {
        System.out.println("t="+t);
    }
    public void test2() {
        System.out.println("t="+t);
    }
}

class Test2 extends Test1 {
    public int t = 2;

    public void test2() {
        System.out.println("t="+t);
    }
}
&lt;/pre&gt;

and the apparently equivalent Scala test program
&lt;code&gt;Test.scala&lt;/code&gt;
(where I have used
Java-like syntax where possible so that you can run "diff" on the
two files):
&lt;pre name="hlcode" class="scala"
&gt;object Test {
    def main(args: Array[String]) {
        (new Test1()).test1();
        (new Test2()).test1();
        (new Test2()).test2();
    }
}

class Test1 {
    val t = 1

    def test1() {
        System.out.println("t="+t);
    }
    def test2() {
        System.out.println("t="+t);
    }
}

class Test2 extends Test1 {
    override val t = 2

    override def test2() {
        System.out.println("t="+t);
    }
}
&lt;/pre&gt;

Copy these out to &lt;code&gt;Test.java&lt;/code&gt; and &lt;code&gt;Test.scala&lt;/code&gt;,
then compile and run each one
(don't try to compile both and then run both in the same
directory, as the class files will collide).
The Java test prints this out:
&lt;pre name="hlcode" class="scala"
&gt;t=1
t=1
t=2
&lt;/pre&gt;

The Scala test prints this out:
&lt;pre name="hlcode" class="scala"
&gt;t=1
t=2
t=2
&lt;/pre&gt;

Note the difference in the middle line, where we have called
&lt;code&gt;Test2.test1()&lt;/code&gt;.
The Java program prints 1, but the Scala program prints 2.
This is because the declaration of &lt;code&gt;t&lt;/code&gt;
in &lt;code&gt;Test2&lt;/code&gt; in Java does not
override the value in &lt;code&gt;Test1&lt;/code&gt;, it
&lt;a href="http://en.wikipedia.org/wiki/Variable_shadowing"&gt;
shadows&lt;/a&gt; it.
The &lt;code&gt;Test1&lt;/code&gt; value of &lt;code&gt;t&lt;/code&gt; is still
there, and it used by any method in &lt;code&gt;Test1&lt;/code&gt;
that refers to that variable.

&lt;br/&gt;&lt;br/&gt;
In Scala, by contrast, references to &lt;code&gt;t&lt;/code&gt;
in &lt;code&gt;Test1&lt;/code&gt; refer to
the overridden value provided by &lt;code&gt;Test2&lt;/code&gt;.
Scala can do this because, consistent with the
&lt;a href="http://programming-scala.labs.oreilly.com/ch06.html#UniformAccessPrinciple"&gt;
Uniform Access Principle&lt;/a&gt;,
a variable in Scala is accessed by a pair of functions to
get and set its value.
When a value is overridden, that creates new access functions
in the subclass that override the access functions in the base class.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-997826125817827716?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/997826125817827716/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=997826125817827716' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/997826125817827716'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/997826125817827716'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/11/overriding-vals-as-optional-parameters.html' title='Overriding vals as Optional Parameters'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-5982949285635786618</id><published>2009-10-11T17:53:00.000-07:00</published><updated>2009-10-11T17:53:12.812-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala Case Statements As Partial Functions</title><content type='html'>A Scala case statement can be either a Function1 or a PartialFunction
depending on the context.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html"&gt;
previous post&lt;/a&gt; I presented a simple &lt;code&gt;Publisher&lt;/code&gt; that I used to
decouple my Swing actors from their targets.
Reader nairb774
&lt;a href="http://jim-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html#comments"&gt;
pointed out&lt;/a&gt; that the standard Scala library includes
a &lt;code&gt;Publisher&lt;/code&gt; class.
In fact, there are two &lt;code&gt;Publisher&lt;/code&gt; classes in Scala,
&lt;a href="http://www.scala-lang.org/docu/files/api/scala/collection/mutable/Publisher.html"&gt;
&lt;code&gt;scala.collection.mutable.Publisher&lt;/code&gt;&lt;/a&gt; and
&lt;a href="http://www.scala-lang.org/docu/files/api/scala/swing/Publisher.html"&gt;
&lt;code&gt;scala.swing.Publisher&lt;/code&gt;&lt;/a&gt;.
Although I like my publisher class better,
the swing publisher did have one feature that I thought was useful:
it accepted as a callback a &lt;code&gt;PartialFunction&lt;/code&gt; rather than, as mine
did, a &lt;code&gt;Function1&lt;/code&gt;.
That would mean, I thought, that I could pass in a case
statement as a callback.

&lt;br/&gt;&lt;br/&gt;
For example, continuing the
&lt;a href="http://alumnus.caltech.edu/~jimmc/mimprint/"&gt;
Mimprint&lt;/a&gt; example from my previous post,
if I were only interested in &lt;code&gt;Enabled&lt;/code&gt;
events published by a particular
publisher, rather than explicitly checking this in my callback with
an &lt;code&gt;isInstanceOf&lt;/code&gt; or a &lt;code&gt;match&lt;/code&gt; statement
that includes a &lt;code&gt;case _ =&amp;gt;&lt;/code&gt; clause,
I could just use a one-line case statement:
&lt;pre&gt;&lt;div class="code"
&gt;    showSingleViewerPublisher.subscribe { case e:Enabled =&gt; doSomething() }
&lt;/div&gt;&lt;/pre&gt;

My calling code in &lt;code&gt;Publisher&lt;/code&gt; would call &lt;code&gt;apply&lt;/code&gt;
on the &lt;code&gt;PartialFunction&lt;/code&gt; callback only
if a call to its &lt;code&gt;isDefinedAt&lt;/code&gt;
method returned true, thus avoiding the
&lt;code&gt;MatchError&lt;/code&gt; that would occur
if I treated it like a &lt;code&gt;Function1&lt;/code&gt;
and called its &lt;code&gt;apply&lt;/code&gt;
method when the value was not &lt;code&gt;Enabled&lt;/code&gt;.
This seemed like useful functionality, so I decided to add it.
I thought it would be easy, but unfortunately it was not.

&lt;br/&gt;&lt;br/&gt;
Consider the following three definitions that assign a case statement
to a partial function, full function, or no explicit
function type, respectively:

&lt;pre&gt;&lt;div class="code"
&gt;val pfv:PartialFunction[String,Unit] = { case "x" =&gt; println("Got x") }
val ffv:Function1[String,Unit] = { case "x" =&gt; println("Got x") }
val nfv = { case "x" =&gt; println("Got x") }
&lt;/div&gt;&lt;/pre&gt;

For the first line, the variable &lt;code&gt;pfv&lt;/code&gt;
gets assigned a value which is a
&lt;code&gt;PartialFunction&lt;/code&gt; representing the case statement.
For the second line,
you might think that, since &lt;code&gt;PartialFunction&lt;/code&gt;
extends &lt;code&gt;Function1&lt;/code&gt; and we
are assigning the same value to &lt;code&gt;ffv&lt;/code&gt; as we did to &lt;code&gt;pfv&lt;/code&gt;,
that the variable &lt;code&gt;ffv&lt;/code&gt;
would be assigned a value which is a &lt;code&gt;PartialFunction&lt;/code&gt;,
just as for the value &lt;code&gt;pfv&lt;/code&gt;.
This is not the case.

&lt;br/&gt;&lt;br/&gt;
The
&lt;a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf"&gt;
Scala Language Specification&lt;/a&gt; (SLS) explicitly states,
in section 8.5, that the type of an anonymous function comprised of
one or more case statements must be specified as either a
&lt;code&gt; Function&lt;i&gt;K&lt;/i&gt;&lt;/code&gt;
or a &lt;code&gt;PartialFunction&lt;/code&gt;,
and that the value generated by the compiler is different depending
on that specified target type.
So the value that gets assigned to &lt;code&gt;ffv&lt;/code&gt;
is a &lt;code&gt;Function1&lt;/code&gt;, and
&lt;code&gt;ffv.isInstanceOf[PartialFunction[_,_]]&lt;/code&gt; evaluates to false.
Note that we could assign the value &lt;code&gt;pfv&lt;/code&gt; to the variable
&lt;code&gt;ffv&lt;/code&gt;, in which case &lt;code&gt;ffv&lt;/code&gt; would have a value
which is a &lt;code&gt;PartialFunction&lt;/code&gt; and
&lt;code&gt;ffv.isInstanceOf[PartialFunction[_,_]]&lt;/code&gt; would evaluate to true.

&lt;br/&gt;&lt;br/&gt;
What happens if you don't specify the type, as in the third line
above where we assign the same value to &lt;code&gt;nfv&lt;/code&gt;?
You might think the compiler could infer the type of the resulting
value, but since, as specified in the SLS, the type must be explicitly
specified as either a &lt;code&gt;Function&lt;i&gt;K&lt;/i&gt;&lt;/code&gt;
or a &lt;code&gt;PartialFunction&lt;/code&gt;,
our assignment to &lt;code&gt;nfv&lt;/code&gt; is actually not a valid statement,
and it fails to compile.
It would be nice if the error message said something like
"You must explicitly specify either a Function&lt;i&gt;K&lt;/i&gt; or a
PartialFunction for a case statement", but instead it gives
this relatively unhelpful message:

&lt;pre&gt;&lt;div class="code"
&gt;&amp;lt;console&amp;gt;:4: error: missing parameter type for expanded function ((x0$1) =&amp;gt; x0$1 match {
  case "x" =&amp;gt; println("Got x")
})
       val nfv = { case "x" =&amp;gt; println("Got x") }
                ^
&lt;/div&gt;&lt;/pre&gt;

In my case, the situation in which I encountered this message was
a little different.
Here is an example showing the problem I ran into:

&lt;pre&gt;&lt;div class="code"
&gt;class PF[T] {          //partial function type
    def sub(x:PartialFunction[T,Unit]) = x
}
class FF[T] {           //full function type
    def sub(x:Function[T,Unit]) = x
}
class NF[T] {           //no unique function type
    def sub(x:PartialFunction[T,Unit]) = x
    def sub(x:Function[T,Unit]) = x
}

val pf = new PF[String]
val ff = new FF[String]
val nf = new NF[String]

pf.sub{ case "x" =&gt; println("x") }   //works, result is PartialFunction
ff.sub{ case "x" =&gt; println("x") }   //works, result is Function1
nf.sub{ case "x" =&gt; println("x") }   //fails with compiler error msg
&lt;/div&gt;&lt;/pre&gt;

Calling the above method &lt;code&gt;sub&lt;/code&gt; with a case statement
works when there is only one method of that name,
whether it takes a &lt;code&gt;Function1&lt;/code&gt; or a
&lt;code&gt;PartialFunction&lt;/code&gt;, but although the
compiler has no problem compiling the overloaded pair of functions,
once they both exist the compiler can no longer unambiguously determine
the target type for the case statement,
so it delivers that same error message
"missing parameter type for expanded function".

&lt;br/&gt;&lt;br/&gt;
In my case I was trying to modify the &lt;code&gt;subscribe&lt;/code&gt; method
in my &lt;code&gt;Publisher&lt;/code&gt;
class so that I could
pass in either a regular function, such as &lt;code&gt;println(_)&lt;/code&gt;,
or a &lt;code&gt;PartialFunction&lt;/code&gt;, in particular an in-line case statement.
The three options I tried are essentially classes PF, FF and NF listed above.
When I used approach NF I was unable to directly pass in a case statement,
but instead would get the compiler error mentioned above.
When I used approach PF I could pass in a case statement as a
&lt;code&gt;PartialFunction&lt;/code&gt;,
but I could not pass in a regular function.
When I used approach FF I could pass in a regular function, and
could pass in and properly deal with a &lt;code&gt;PartialFunction&lt;/code&gt;,
since it extends &lt;code&gt;Function1&lt;/code&gt;, but
when I used an in-line case statement it would get
compiled as a &lt;code&gt;Function1&lt;/code&gt;
rather than a &lt;code&gt;PartialFunction&lt;/code&gt;, which would cause
execution to fail when a value was passed to that case statement that
it did not cover (since it was not a &lt;code&gt;PartialFunction&lt;/code&gt; and
thus did not have an &lt;code&gt;isDefinedAt&lt;/code&gt; method to call first).

&lt;br/&gt;&lt;br/&gt;
I don't like option FF because it would allow code
(specifically, an in-line case statement)
to compile but then not execute as expected.
Options PF and NF are not very useful as is, since neither directly
supports both case statements and full functions.

&lt;br/&gt;&lt;br/&gt;
In a
&lt;a href="http://www.nabble.com/Re%3A--scala--Matching-expansion-and-type-inference-p23375171.html"&gt;
mailing list response&lt;/a&gt; to someone who was attempting to
use option NF in his application,
Paul Phillips suggested using option FF with
a helper function &lt;code&gt;pf&lt;/code&gt; that accepts a &lt;code&gt;PartialFunction&lt;/code&gt;
and returns the same value, then wrapping any case statements inside a
call to that helper function;
or, alternatively, assigning the case statement to a
&lt;code&gt;val&lt;/code&gt; declared as
a &lt;code&gt;PartialFunction&lt;/code&gt; before passing it to method &lt;code&gt;sub&lt;/code&gt;.
Unfortunately, if the user forgets to use either of these techniques on
a case statement and just passes it directly to method
&lt;code&gt;sub&lt;/code&gt; in option FF,
it will be handled as a &lt;code&gt;Function1&lt;/code&gt; rather than a
&lt;code&gt;PartialFunction&lt;/code&gt;, so
it will compile but not behave as expected.

&lt;br/&gt;&lt;br/&gt;
Paul's suggestion would also work in option NF (and in option PF,
although in that case it would be redundant),
which would behave much the same as option FF from the user's perspective
except that passing a bare case statement
to the overloaded method &lt;code&gt;sub&lt;/code&gt;
would not compile, so we would no longer have the undesirable situation
of something that compiles but behaves unexpectedly.

&lt;br/&gt;&lt;br/&gt;
As an alternative to Paul's &lt;code&gt;pf&lt;/code&gt; helper function, I could
write a helper function &lt;code&gt;ff&lt;/code&gt;
that takes a &lt;code&gt;Function1&lt;/code&gt; and turns it into a
&lt;code&gt;PartialFunction&lt;/code&gt; with an &lt;code&gt;isDefinedAt&lt;/code&gt; method that always
returns true.
I would then use this with option PF.
This would allow me to directly pass in case statements, but I would
have to wrap all regular functions in a call to &lt;code&gt;ff&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
I have not yet made any changes to my &lt;code&gt;Publisher&lt;/code&gt; class,
since I don't particularly like either of the options
and I don't currently really need the ability to use in-line case statements.
Meanwhile, if
I get the compiler error "missing parameter type for expanded function"
while trying to use an in-line case statement,
at least I now know one more thing to check for.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-5982949285635786618?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/5982949285635786618/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=5982949285635786618' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/5982949285635786618'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/5982949285635786618'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/10/scala-case-statements-as-partial.html' title='Scala Case Statements As Partial Functions'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-906076456470111236</id><published>2009-10-07T07:27:00.000-07:00</published><updated>2009-10-08T08:23:09.594-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>A Simple Publish/Subscribe Example in Scala</title><content type='html'>Here is an example where using a simple publish/subscribe mechanism
allowed me to clean up some of my early Scala code.

&lt;br/&gt;&lt;br/&gt;
My
&lt;a href="http://alumnus.caltech.edu/~jimmc/mimprint/"&gt;
Mimprint&lt;/a&gt;
program
(now also on &lt;a href="http://github.com/jimmc/mimprint"&gt;github&lt;/a&gt;)
was originally written in Java, then ported to Scala
soon after I first started learning that language.
As such, much of that original ported code was "Java written in Scala".
As I have continued to internalize the Scala approach I have gone
back and modified various parts of the program to make it cleaner.

&lt;br/&gt;&lt;br/&gt;
In one part of the program I set up a collection of menu checkboxes
to allow the user to enable or disable various features.
As those features are enabled or disabled, the states of other screen
components change; sometimes a component is enabled or disabled, sometimes
a component is hidden or made visible.

&lt;br/&gt;&lt;br/&gt;
My original Java-ish Scala code to do this looked something like this
(with irrelevant parts omitted):

&lt;pre&gt;&lt;div class="code"
&gt;class ViewListGroup ... {
    ...
    private var singleComp:Component = _
    private var mShowFileInfo:SCheckBoxMenuItem = _
    private var mShowFileIcons:SCheckBoxMenuItem = _
    private var mShowDirDates:SCheckBoxMenuItem = _
    private var mShowSingleViewer:SCheckBoxMenuItem = _

    def getComponent():Component = {
        ...
        singleComp = playViewSingle.getComponent()
        ...
        //Add our menu items
        mShowFileInfo = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowFileInfo")(
                    showFileInfo(mShowFileInfo.getState))
        mShowFileInfo.setState(true)
        m.add(mShowFileInfo)

        mShowFileIcons = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowFileIcons")(
                    showFileIcons(mShowFileIcons.getState))
        mShowFileIcons.setState(false)
        m.add(mShowFileIcons)

        mShowDirDates = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowDirDates")(
                    showDirDates(mShowDirDates.getState))
        mShowDirDates.setState(playViewList.includeDirectoryDates)
        ...
        m.add(mShowDirDates)

        mShowSingleViewer = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowSingleViewer")(
                    showSingleViewer(mShowSingleViewer.getState))
        mShowSingleViewer.setState(true)
        m.add(mShowSingleViewer)
        showSingleViewer(mShowSingleViewer.getState)
                //make sure window state is in sync with menu item state
        ...
    }
    ...

    def showFileInfo(b:Boolean) {
        playViewList.showFileInfo(b)
        mShowFileInfo.setState(b)
        mShowFileIcons.setEnabled(b)
        mShowDirDates.setEnabled(b)
    }

    def showFileIcons(b:Boolean) {
        playViewList.showFileIcons(b)
        playViewList.redisplayList()
    }

    def showDirDates(b:Boolean) {
        playViewList.includeDirectoryDates = b
        playViewList.redisplayList()
    }

    def showSingleViewer(b:Boolean) {
        singleComp.setVisible(b)
        singleComp.getParent.asInstanceOf[JSplitPane].resetToPreferredSizes()
        mShowSingleViewer.setState(b)
        playViewList.requestSelect
    }
    ...
}
&lt;/div&gt;&lt;/pre&gt;

There were two things about this code that I didn't like:
&lt;ol&gt;
&lt;li&gt;Mutable instance variables using &lt;code&gt;var&lt;/code&gt;,
    particularly since they were not really variable.
    These values were being assigned once, not at construction time,
    but had to be available to other methods.
&lt;li&gt;The close binding between the different UI components, since the
    action method called by one component directly modified attributes
    of possibly a number of other components.
&lt;/ol&gt;

After a recent conversation with a friend I realized that I could probably
improve this code by using a publish/subscribe mechanism to loosen
the coupling between the components.
Mimprint already had an
&lt;a href="http://jim-mcbeath.blogspot.com/2008/07/refactoring-scala-actors.html"&gt;
&lt;code&gt;ActorPublisher&lt;/code&gt;&lt;/a&gt;
class, where each
subscriber is an Actor that accepts messages of the published object type,
but in this case I wanted a lighter weight implementation, since I knew
the subscriber actions would be quick.
Also, this being
&lt;a href="http://java.sun.com/docs/books/tutorial/ui/index.html"&gt;
Swing&lt;/a&gt;, the subscriber actions that update screen state
should run in the Swing
&lt;a href="http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html"&gt;
event thread&lt;/a&gt;, and the events being published are
also coming from the event thread, so the simple thing to do is to run
the subscriber actions directly from the publish method.

&lt;br/&gt;&lt;br/&gt;
Writing a publish/subscribe handler in Scala is pretty easy, and for
me it was even simpler, as I already had one.
I grabbed my
&lt;a href="http://jim-mcbeath.blogspot.com/2009/04/scala-listener-manager.html"&gt;
&lt;code&gt;ListenerManager&lt;/code&gt;&lt;/a&gt;
and modified it to use the publish/subscribe terminology.
I also added synchronization to make it multi-thread safe, although
for this app I don't really need it.
It now looks like this:

&lt;pre&gt;&lt;div class="code"
&gt;package net.jimmc.util

/** Manage a subscriber list.
 * There are no guarantees on the order of subscribers in the list.
 * This code is a slightly modified version of ListenerManager
 * as published to my blog in April 2009.
 */
trait Publisher[E] {
    type S = (E) =&amp;gt; Unit
    private var subscribers: List[S] = Nil
    private object lock
        //By using lock.synchronized rather than this.synchronized we reduce
        //the scope of our lock from the extending object (which might be
        //mixing us in with other classes) to just this trait.

    /** True if the subscriber is already in our list. */
    def isSubscribed(subscriber:S) = {
        val subs = lock.synchronized { subscribers }
        subs.exists(_==subscriber)
    }

    /** Add a subscriber to our list if it is not already there. */
    def subscribe(subscriber:S) = lock.synchronized {
        if (!isSubscribed(subscriber))
            subscribers = subscriber :: subscribers
    }

    /** Remove a subscriber from our list.  If not in the list, ignored. */
    def unsubscribe(subscriber:S):Unit = lock.synchronized {
        subscribers = subscribers.filter(_!=subscriber)
    }

    /** Publish an event to all subscribers on the list. */
    def publish(event:E) = {
        val subs = lock.synchronized { subscribers }
        subs.foreach(_.apply(event))
    }
}
&lt;/div&gt;&lt;/pre&gt;

For each menu checkbox I would like to set up a publisher.
In every case, I just need to publish whether that checkbox has
just been enabled or disabled.
I defined a simple case class hierarchy to
represent the &lt;code&gt;Enabled&lt;/code&gt; and &lt;code&gt;Disabled&lt;/code&gt; messages:

&lt;pre&gt;&lt;div class="code"
&gt;sealed abstract class Abled
case object Enabled extends Abled
case object Disabled extends Abled
&lt;/div&gt;&lt;/pre&gt;

I then created a publisher class that uses that event type:
&lt;pre&gt;&lt;div class="code"
&gt;class AbledPublisher extends Publisher[Abled]
&lt;/div&gt;&lt;/pre&gt;

I want to easily publish the
&lt;code&gt;Enabled&lt;/code&gt; or &lt;code&gt;Disabled&lt;/code&gt; object based on
the current state of a checkbox, so I added an &lt;code&gt;AbledPublisher&lt;/code&gt;
companion object with an &lt;code&gt;apply&lt;/code&gt; method to do that:
&lt;pre&gt;&lt;div class="code"
&gt;object AbledPublisher {
    object Abled { def apply(b:Boolean) = if (b) Enabled else Disabled }
}
&lt;/div&gt;&lt;/pre&gt;

Conversely, upon receiving an &lt;code&gt;Abled&lt;/code&gt; event
in a subscriber for a UI component I want to be able to
enable or disable that component.
I could use a &lt;code&gt;match&lt;/code&gt; statement with cases for
&lt;code&gt;Enabled&lt;/code&gt; and &lt;code&gt;Disabled&lt;/code&gt;,
but a simpler way is to
modify the &lt;code&gt;Abled&lt;/code&gt; case class hierarchy to
encode a boolean state value into the &lt;code&gt;Abled&lt;/code&gt; case object to allow
easy translation from an &lt;code&gt;Abled&lt;/code&gt; object back to a state:
&lt;pre&gt;&lt;div class="code"
&gt;sealed abstract class Abled { val state:Boolean }
case object Enabled extends Abled { override val state = true }
case object Disabled extends Abled { override val state = false }
&lt;/div&gt;&lt;/pre&gt;

Finally, I packaged up the case class hierarchy inside the
&lt;code&gt;AbledPublisher&lt;/code&gt; object to control scoping.
The final &lt;code&gt;AbledPublisher&lt;/code&gt; file looks like this:
&lt;pre&gt;&lt;div class="code"
&gt;package net.jimmc.util

//For subscribers of things that turn on and off
class AbledPublisher extends Publisher[AbledPublisher.Abled]

// use "import AbledPublisher._" to pick up these definitions
object AbledPublisher {

    sealed abstract class Abled { val state:Boolean }
    case object Enabled extends Abled { override val state = true }
    case object Disabled extends Abled { override val state = false }

    object Abled { def apply(b:Boolean) = if (b) Enabled else Disabled }
}
&lt;/div&gt;&lt;/pre&gt;

Given the above &lt;code&gt;AbledPublisher&lt;/code&gt; class and object,
I modified my code so that the action method called by each menu checkbox
publishes an &lt;code&gt;Enabled&lt;/code&gt; or &lt;code&gt;Disabled&lt;/code&gt; event that
matches the new state of the checkbox, and for each place in the old
code where an action method called a state-changing method on another
component I set up that target component as a subscriber to the
appropriate publisher that, when it receives a published event,
takes appropriate action on itself.

&lt;br/&gt;&lt;br/&gt;
With the above changes, and a slight change to my
&lt;code&gt;SCheckBoxMenuItem&lt;/code&gt; class
so that it passes itself to the action callback, the code now looks
like this:
&lt;pre&gt;&lt;div class="code"
&gt;import net.jimmc.util.AbledPublisher
import net.jimmc.util.AbledPublisher._

class ViewListGroup ... {
    vlg:ViewListGroup =&gt;
    ...
    private val showFileInfoPublisher = new AbledPublisher
    private val showSingleViewerPublisher = new AbledPublisher
    private val showDirectoriesPublisher = new AbledPublisher
    ...

    def getComponent():Component = {
        ...
        val singleComp = playViewSingle.getComponent()
        showSingleViewerPublisher.subscribe((ev)=&gt; {
            singleComp.setVisible(ev.state)
            singleComp.getParent.asInstanceOf[JSplitPane].resetToPreferredSizes()
        })
        ...
        //Add our menu items
        val mShowFileInfo = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowFileInfo")((cb)=&gt;
                    showFileInfo(cb.getState))
        mShowFileInfo.setState(true)
        showFileInfoPublisher.subscribe((ev)=&gt;
            mShowFileInfo.setState(ev.state)
        )
        m.add(mShowFileInfo)

        val mShowFileIcons = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowFileIcons")((cb)=&gt;
                    showFileIcons(cb.getState))
        mShowFileIcons.setState(false)
        showFileInfoPublisher.subscribe((ev)=&gt;
            mShowFileIcons.setState(ev.state)
        )
        m.add(mShowFileIcons)

        val mShowDirDates = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowDirDates")((cb)=&gt;
                    showDirDates(cb.getState))
        mShowDirDates.setState(playViewList.includeDirectoryDates)
        mShowDirDates.setVisible(includeDirectories)
        showFileInfoPublisher.subscribe((ev)=&gt;
            mShowDirDates.setState(ev.state)
        )
        showDirectoriesPublisher.subscribe((ev)=&gt;
            mShowDirDates.setVisible(ev.state)
        )
        m.add(mShowDirDates)

        val mShowSingleViewer:SCheckBoxMenuItem = new SCheckBoxMenuItem(
                viewer,"menu.List.ShowSingleViewer")((cb)=&gt;
                    showSingleViewer(cb.getState))
        mShowSingleViewer.setState(true)
        showSingleViewerPublisher.subscribe((ev)=&gt;
            mShowSingleViewer.setState(ev.state)
        )
        m.add(mShowSingleViewer)
        showSingleViewer(mShowSingleViewer.getState)
                //make sure window state is in sync with menu item state
        ...
    }
    ...

    def showFileInfo(b:Boolean) {
        playViewList.showFileInfo(b)
        showFileInfoPublisher.publish(Abled(b))
    }

    def showFileIcons(b:Boolean) {
        playViewList.showFileIcons(b)
        playViewList.redisplayList()
    }

    def showDirDates(b:Boolean) {
        playViewList.includeDirectoryDates = b
        playViewList.redisplayList()
    }

    def showSingleViewer(b:Boolean) {
        showSingleViewerPublisher.publish(Abled(b))
        playViewList.requestSelect
    }
    ...
}
&lt;/div&gt;&lt;/pre&gt;

The total number of lines of code in ViewListGroup
is actually a bit more than before,
but I find the code a little easier to understand because all of the
code that acts on a UI component
is now localized in one place in the source file.
All of the &lt;code&gt;var&lt;/code&gt;s
that held pointers to those components are now gone,
replaced by a few &lt;code&gt;val&lt;/code&gt;s for the publishers.
The publishers use &lt;code&gt;var&lt;/code&gt;s
to maintain internal state, but that state is
simple and easily understood,
well encapsulated and multi-thread safe.

&lt;br/&gt;&lt;br/&gt;
There is still more cleanup work to be done in Mimprint.
For example, in the above code
the checkbox action methods such as &lt;code&gt;showFileInfo&lt;/code&gt;
and &lt;code&gt;showFileIcons&lt;/code&gt; call methods on the
&lt;code&gt;playViewList&lt;/code&gt; object as well as publishing an
&lt;code&gt;Abled&lt;/code&gt; event.
Instead, I could set up &lt;code&gt;playViewList&lt;/code&gt; as a listener on
each of the published events, then make the menu checkbox actions directly
publish an event and get rid of the &lt;code&gt;showXXX&lt;/code&gt; methods.
I will leave that for another round of cleanup.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-906076456470111236?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/906076456470111236/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=906076456470111236' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/906076456470111236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/906076456470111236'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/10/simple-publishsubscribe-example-in.html' title='A Simple Publish/Subscribe Example in Scala'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4893930403529645772</id><published>2009-10-01T21:53:00.000-07:00</published><updated>2009-10-08T08:51:24.394-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Initializing Immutable Variables in Scala</title><content type='html'>One of the guidelines I picked up when I learned Scala is to use
immutable variables as much as possible.
Besides the trivial but satisfying detail of making the declaration
of an immutable variable (&lt;code&gt;val&lt;/code&gt;)
take no more characters than a mutable one (&lt;code&gt;var&lt;/code&gt;),
Scala also provides some interesting ways to set the values into
those immutable variables.

&lt;br/&gt;&lt;br/&gt;
In Scala, immutable variables are identified by declaring them
using the &lt;code&gt;val&lt;/code&gt; keyword rather than &lt;code&gt;var&lt;/code&gt;.
In Java, immutable variables are identified by adding the
&lt;code&gt;final&lt;/code&gt; qualifier to the variable declaration.
But a Java &lt;code&gt;final&lt;/code&gt; variable has slightly different
semantics than a Scala &lt;code&gt;val&lt;/code&gt;:
in Java, you can declare a &lt;code&gt;final&lt;/code&gt; variable without
specifying a value for it,
then fill in the value later.
Java allows the variable to be assigned once, after which it can not
be assigned again.
In Scala,
a concrete &lt;code&gt;val&lt;/code&gt; must have its value assigned as part of the
definition.

&lt;br/&gt;&lt;br/&gt;
Consider this sample Java class, &lt;code&gt;Interval&lt;/code&gt;,
which represents an interval on the real number line.
We want to allow the constructor to be called with endpoints in
either order, but we want to store them internally in sorted order.

&lt;pre&gt;&lt;div class="code"
&gt;//Java code
public class Interval {
    final double start;
    final double end;  //invariant: end&amp;gt;=start

    public Interval(double x1, double x2) {
        if (x1&amp;gt;x2) {
            start = x2;
            end = x1;
        } else {
            start = x1;
            end = x2;
        }
    }

    //other methods that use start and end go here
}
&lt;/div&gt;&lt;/pre&gt;

If you try this idiom in Scala,
by replacing each &lt;code&gt;final&lt;/code&gt; variable with a &lt;code&gt;val&lt;/code&gt;
but continuing to use the same initialization construct,
you will get a compiler error
"reassignment to val".
When using a concrete &lt;code&gt;val&lt;/code&gt; in Scala, you must supply the
value in the statement where you declare the &lt;code&gt;val&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
For relatively simple cases, as in this example, we can take advantage
of the fact that Scala allows us to build expressions with &lt;code&gt;if&lt;/code&gt;
in them,
so we can express the same functionality as in the above Java
code as follows:

&lt;pre&gt;&lt;div class="code"
&gt;class Interval(x1:Double, x2:Double) {
    val start = if (x2&amp;gt;x1) x1 else x2
    val end   = if (x2&amp;gt;x1) x2 else x1

    //other methods that use start and end go here
}
&lt;/div&gt;&lt;/pre&gt;

Sometimes the logic to calculate the values for the immutable variables
is much more complicated than this and more
expensive to calculate.
Perhaps,
as in our Java example,
we don't want to recalculate that condition
over again for each variable.
We might also be more comfortable building up our values using
mutable variables.
We could take the easy and straightforward way and just use
&lt;code&gt;var&lt;/code&gt; rather than &lt;code&gt;val&lt;/code&gt; for our variables,
but it is worth a bit of effort to retain the immutability of our variables.
Here is an approach I sometimes take:

&lt;pre&gt;&lt;div class="code"
&gt;class Interval(x1:Double, x2:Double) {
    val (start, end) = {
        def intervalNeedsReversing(a:Double,b:Double) = (a&gt;b)
        if (intervalNeedsReversing(x1,x2))
            (x2, x1)
        else
            (x1, x2)
    }

    //other methods that use start and end go here
}
&lt;/div&gt;&lt;/pre&gt;

In the above approach, we have a block of code that calculates our
values.
Though not needed in this case,
the &lt;code&gt;intervalNeedsReversing&lt;/code&gt; function is an example of how you
you can define functions within a block in order to refactor that
code or better organize it.
The value of the block is a tuple,
which we then assign using a tuple-assignment to
our immutable variables &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
A tuple-assignment is a pattern-matching operation that pulls apart the
tuple data and stores each piece into the separate variables.
It looks like the second line in this example:
&lt;pre&gt;&lt;div class="code"
&gt;val t2 = (123, "abc") //the type of t2 is Tuple2[Int, java.lang.String]
val (n, s) = t2  //assigns n=123, s="abc"
&lt;/div&gt;&lt;/pre&gt;

You can use any expression in place of t2 that has the same
type, including a function call, a variable, a literal tuple, or a code block.

&lt;br/&gt;&lt;br/&gt;
You can include a type on each variable name; if the types of
the assigned variables don't match the corresponding types of the value on the
right hand side, you will get a compiler error.

&lt;pre&gt;&lt;div class="code"
&gt;val (n:Int, s:String) = t2 //ok
val (s:String, n:Int) = t2 //error
&lt;/div&gt;&lt;/pre&gt;

The tuple syntax of parentheses
around a comma-separated list of values is actually
a shorthand for the &lt;code&gt;Tuple&lt;i&gt;N&lt;/i&gt;&lt;/code&gt; class.
For each pair of lines below, the first line is a shorthand
("&lt;a href="http://en.wikipedia.org/wiki/Syntactic_sugar"&gt;syntactic sugar&lt;/a&gt;")
for the second.

&lt;pre&gt;&lt;div class="code"
&gt;(a, b)
Tuple2(a, b)

(1, "x", "y")
Tuple3(1, "x", "y")

val (n, s) = t2
val Tuple2(n, s) = t2
&lt;/div&gt;&lt;/pre&gt;

The last of the three examples above is
a pattern-matching assignment statement.

&lt;br/&gt;&lt;br/&gt;
You can use the List pattern in an assignment as well:
&lt;pre&gt;&lt;div class="code"
&gt;val a :: b :: c = List(1,2,3,4)
    //This assigns a:Int=1, b:Int=2, c:List[Int]=List(3,4)
&lt;/div&gt;&lt;/pre&gt;

The List and Tuple classes can be used in a pattern-matching
assignment like this because they each have an
&lt;a href="http://www.scala-lang.org/node/112"&gt;
extractor&lt;/a&gt;
defined by the &lt;code&gt;unapply&lt;/code&gt; method in their companion object.
You can use any extractor
(that is, any declared object that includes
an &lt;code&gt;unapply&lt;/code&gt; method) in this way.
For example, a case class can be used:
&lt;pre&gt;&lt;div class="code"
&gt;case class Foo(num:Int, str:String)
val f = Foo(42,"ok")
val Foo(n,s) = f //assigns n:Int=42, s:String="ok"
&lt;/div&gt;&lt;/pre&gt;

This works even if the case class happens to use mutable fields:
the values at the time of the pattern match assignment are set
into the new variables, which are immutable.

&lt;pre&gt;&lt;div class="code"
&gt;case class Bar(var num:Int, var str:String)
val b = Bar(42,"ok")
b.num += 1
b.str = "no"
val Bar(n,s) = b //assigns n:Int=43, s:String="no"
b.num += 1              //does not change n
&lt;/div&gt;&lt;/pre&gt;

For example, if you have a large number of values to set at once,
you could declare a case class to represent them, and match on that
to assign the values:

&lt;pre&gt;&lt;div class="code"
&gt;class AnotherExample {
    case class MyArgs(var name:String, var pathPart:String, var someNumber:Int)
    val MyArgs(path, part, num) = {
        val m = MyArgs("/path/foo/bar", "partX", 123)
        //change values of fields in m as desired
        m
    }
}
&lt;/div&gt;&lt;/pre&gt;

You thus get the benefit of having immutable variables for use in your
constructed object, but you can use mutable private data within
the block to make it easier to do your construction.

&lt;br/&gt;&lt;br/&gt;
You can use this technique to initialize immutable variables
within a method as well.
Effectively, you are using mutable variables only for the limited scope
in which they are desired.
By enclosing them in a block you prevent
code outside that block from modifying those mutable values.

&lt;br/&gt;&lt;br/&gt;
Since this technique is based on pattern matching,
you can use it with any legal pattern.
Pattern matching is typically used in the &lt;code&gt;case&lt;/code&gt;
clauses of &lt;code&gt;match&lt;/code&gt; statements.

&lt;br/&gt;&lt;br/&gt;
Patterns can include nested constructs, which allows you to pull
out values from deep within a structure when that structure is known.
By using the &lt;code&gt;@&lt;/code&gt; operator within a pattern you can extract
the value of an entire subpattern:
&lt;pre&gt;&lt;div class="code"
&gt;case class Foo(n:Int, var s:String)
case class Baz(f:Foo, b:Option[Baz])
val data = 123 :: Baz(Foo(3,"c"),Some(Baz(Foo(4,"d"),None))) :: 456 :: Nil

val _ :: Baz(Foo(_,a),Some(b @ Baz(c @ Foo(d,e),_))) :: f :: _ = data
// The above val statement assigns these values:
// a = "c"
// b = Baz(Foo(4,"d"),None)
// c = Foo(4,"d")
// d = 4
// e = "d"
// f = 456
&lt;/div&gt;&lt;/pre&gt;
The undersccore indicates a placeholder for a part of the pattern whose
value we don't care about and don't want assigned to anything.

&lt;br/&gt;&lt;br/&gt;
Note that the variable &lt;code&gt;c&lt;/code&gt;
refers to the same object as the &lt;code&gt;Foo&lt;/code&gt;
object that appears in variable &lt;code&gt;b&lt;/code&gt;.
We defined &lt;code&gt;Foo&lt;/code&gt; with a
&lt;code&gt;var&lt;/code&gt; for &lt;code&gt;s&lt;/code&gt;.
If we change the value of the &lt;code&gt;Foo&lt;/code&gt; object referenced by
variable &lt;code&gt;c&lt;/code&gt;,
then we will see that change when we ask for the value of
variable &lt;code&gt;b&lt;/code&gt;:
&lt;pre&gt;&lt;div class="code"
&gt;scala&amp;gt; b
res0: Baz = Baz(Foo(4,d),None)

scala&amp;gt; c
res1: Foo = Foo(4,d)

scala&amp;gt; c.s = "x"

scala&amp;gt; c
res2: Foo = Foo(4,x)

scala&amp;gt; b
res3: Baz = Baz(Foo(4,x),None)
&lt;/div&gt;&lt;/pre&gt;

Although &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt; are themselves immutable
variables, if they point to the same mutable object then changes made
to that object through one variable
will be visible through the other variable.

&lt;br/&gt;&lt;br/&gt;
As you learn Scala and see examples of &lt;code&gt;case&lt;/code&gt; statements,
remember that any syntax that is valid as the pattern match in a
&lt;code&gt;case&lt;/code&gt; statement is also valid as a pattern match
in a &lt;code&gt;val&lt;/code&gt; assignment.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4893930403529645772?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4893930403529645772/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4893930403529645772' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4893930403529645772'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4893930403529645772'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/10/initializing-immutable-variables-in.html' title='Initializing Immutable Variables in Scala'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-936445998608234242</id><published>2009-09-10T21:24:00.001-07:00</published><updated>2009-09-10T21:24:50.793-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Type Safe Builder in Scala, Part 4</title><content type='html'>A type-safe builder with mutually exclusive parameters.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-using-church.html"&gt;
previous&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html"&gt;
three&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-3.html"&gt;
posts&lt;/a&gt;
I presented various versions of a type-safe builder that enforced,
at compile time,
that required setters were called exactly once and optional setters
were called no more than once.
In those examples there was a one-to-one correspondence between the
setters (such as &lt;code&gt;withBrand&lt;/code&gt;)
and type variables that were used to enforce the number of calls to
those methods (such as &lt;code&gt;HAS_BRAND&lt;/code&gt;).
We can use the type variables in different ways to change what
combination of methods calls is allowed.
In particular, we can have multiple parameters which can be set in
various combinations, only some of which are allowed.
For example, we can have two mutually exclusive parameters,
where you must set either one but not the other.

&lt;br/&gt;&lt;br/&gt;
To show how this works, I have created a builder for a Pyramid calculator
that can be used to calculate the physical parameters of a rectangular
pyramid.
After creating a builder, you can call various setter methods to set
some of
the physical parameters of the pyramid, including the length, width and
area of the base, the height, and the length of an upright edge.
You must set exactly two out of three of the length, width and area of
the base,
and you must set one but not both of the height and edge.
Once you have done that, you call &lt;code&gt;build&lt;/code&gt; to get back the
calculator from which you can retrieve any of the five physical parameters
listed above plus volume.
If you call too few or too many of the setters in each group,
the call to &lt;code&gt;build&lt;/code&gt; will not compile.

&lt;pre&gt;&lt;div class="code"
&gt;object Pyramid {

    //A small collection of class types to define a state machine that counts
    abstract class COUNTER              { type Count &amp;lt;: COUNTER }
    abstract class MANY extends COUNTER { type Count = MANY }
    abstract class TWO  extends COUNTER { type Count = MANY }
    abstract class ZERO_OR_ONE extends COUNTER
    abstract class ONE  extends ZERO_OR_ONE { type Count = TWO }
    abstract class ZERO extends ZERO_OR_ONE { type Count = ONE }

    //We require positive values for our calls
    class Positive(val d:Double) {
        if (d&amp;lt;=0) throw new IllegalArgumentException("non-positive value")
    }
    implicit def doubleToPositive(d:Double) = new Positive(d)
    implicit def intToPositive(n:Int) = new Positive(n)

    //The class that manages the state of our specification
    class Specs private[Pyramid]() { self:Specs =&amp;gt;
        //Caller must set exactly two out of three of these
        val length:Double = 0
        val width:Double = 0
        val area:Double = 0

        //Caller must set exactly one of these two heights
        val height:Double = 0   //vertical height to the tip
        val edge:Double = 0     //from base to tip along an edge

        //We maintain compiler-time state to count the two types of calls
        type TT &amp;lt;: {
            type COUNT_LENGTH &amp;lt;: COUNTER
            type COUNT_WIDTH &amp;lt;: COUNTER
            type COUNT_AREA &amp;lt;: COUNTER
            type COUNT_BASE &amp;lt;: COUNTER          // length, width or area
            type COUNT_HEIGHT &amp;lt;: COUNTER
            type COUNT_EDGE &amp;lt;: COUNTER
            type COUNT_VERT &amp;lt;: COUNTER          // height or edge
        }

        class SpecsWith(bb:Specs) extends Specs {
            override val length = bb.length
            override val width = bb.width
            override val area = bb.area
            override val height = bb.height
            override val edge = bb.edge
        }

        def setLength(d:Positive) = new SpecsWith(this) {
            override val length:Double = d.d
            type TT = self.TT {
                type COUNT_LENGTH = self.TT#COUNT_LENGTH#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setWidth(d:Positive) = new SpecsWith(this) {
            override val width:Double = d.d
            type TT = self.TT {
                type COUNT_WIDTH = self.TT#COUNT_WIDTH#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setArea(d:Positive) = new SpecsWith(this) {
            override val area:Double = d.d
            type TT = self.TT {
                type COUNT_AREA = self.TT#COUNT_AREA#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setHeight(d:Positive) = new SpecsWith(this) {
            override val height:Double = d.d
            type TT = self.TT {
                type COUNT_HEIGHT = self.TT#COUNT_HEIGHT#Count
                type COUNT_VERT = self.TT#COUNT_VERT#Count
            }
        }

        def setEdge(d:Positive) = new SpecsWith(this) {
            override val edge:Double = d.d
            type TT = self.TT {
                type COUNT_EDGE = self.TT#COUNT_EDGE#Count
                type COUNT_VERT = self.TT#COUNT_VERT#Count
            }
        }

    }

    //Starting point: nothing is set
    def apply() = new Specs {
        type TT = {
            type COUNT_LENGTH = ZERO
            type COUNT_WIDTH = ZERO
            type COUNT_AREA = ZERO
            type COUNT_BASE = ZERO
            type COUNT_HEIGHT = ZERO
            type COUNT_EDGE = ZERO
            type COUNT_VERT = ZERO
        }
    }

    //Required ending point: two base measures, one height measure,
    //no single parameter more than once
    type CompleteSpecs = Specs {
        type TT &amp;lt;: {
            type COUNT_LENGTH &amp;lt;: ZERO_OR_ONE
            type COUNT_WIDTH &amp;lt;: ZERO_OR_ONE
            type COUNT_AREA &amp;lt;: ZERO_OR_ONE
            type COUNT_BASE = TWO
            type COUNT_HEIGHT &amp;lt;: ZERO_OR_ONE
            type COUNT_EDGE &amp;lt;: ZERO_OR_ONE
            type COUNT_VERT = ONE
        }
    }

    //Calc1 includes the first set of values that can be calculated
    class Calc1 private[Pyramid](spec:CompleteSpecs) {
        import java.lang.Math.sqrt

        //The three related base measures
        lazy val length = if (spec.length!=0) spec.length
                          else spec.area/spec.width
        lazy val width =  if (spec.width!=0) spec.width
                          else spec.area/spec.length
        lazy val area =   if (spec.area!=0) spec.area
                          else spec.length*spec.width

        //The two related height measures
        lazy val height = if (spec.height!=0) spec.height
            else sqrt(spec.edge*spec.edge-length*length/4-width*width/4)
        lazy val edge =   if (spec.edge!=0) spec.edge
            else sqrt(length*length/4+width*width/4+spec.height*spec.height)

        lazy val volume = length * width * height / 3
    }

    implicit def specsOK(spec:CompleteSpecs) = new {
        def build = new Calc1(spec)
    }

}
&lt;/div&gt;&lt;/pre&gt;

As before, remember to &lt;code&gt;import Pyramid._&lt;/code&gt; when using
this code.

&lt;br/&gt;&lt;br/&gt;
Let's examine the code.

&lt;br/&gt;&lt;br/&gt;
To start, I have a small type-based state machine,
similar to what I used in my previous post.
I changed the names of the classes
to more accurately reflect the fact that I am
counting the number of calls to methods,
and I added a class to represent a count of two as distinct from
larger numbers.

&lt;br/&gt;&lt;br/&gt;
Next I define a class that ensures that the values passed to the setters
are all strictly positive numbers (since they represent physical quantities),
and I add a couple of implicit conversion methods to allow the caller
to pass in ints or doubles.
If the caller passes in a negative number, the builder will throw
a runtime exception.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;Specs&lt;/code&gt; class is where I maintain my state information
as the builder is being constructed.
I use the same basic approach as in my previous post, with a set of
parameter values and a set of compile-time constraints, the latter
represented by the &lt;code&gt;TT&lt;/code&gt; compound type.
Note that
in addition to the type parameter values that are directly associated
with the parameters, which are used to ensure that each individual
setter is called not more than once,
there are two additional type values that do not directly
correspond to parameter values or individual setters.
The &lt;code&gt;COUNT_BASE&lt;/code&gt; type value is associated with the
length, width and area parameters,
while the &lt;code&gt;COUNT_VERT&lt;/code&gt; type value is associated with
the height and edge parameters.
This association is specified in the setter methods.

&lt;br/&gt;&lt;br&gt;
The &lt;code&gt;SpecsWith&lt;/code&gt; class allows me to default all values to
the previous step in the builder chain, so that I can override just
the value I want to change in each setter.

&lt;br/&gt;&lt;br/&gt;
Each of the five setters sets its parameter value,
sets a new value for its individual type value counter,
and also sets a new
type value for one of the non-individual counters.
Note how the setters for length, width and area all refer to
&lt;code&gt;COUNT_BASE&lt;/code&gt;,
while the setters for height and edge both refer to
&lt;code&gt;COUNT_VERT&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
I chose to define an &lt;code&gt;apply()&lt;/code&gt; method rather than call it
&lt;code&gt;builder&lt;/code&gt;.
This allows me to start my builder chain by specifying just
&lt;code&gt;Pyramid()&lt;/code&gt;.

&lt;br/&gt;&lt;br/&gt;
The &lt;code&gt;CompleteSpecs&lt;/code&gt; type definition defines the end point
that will be valid for a call to &lt;code&gt;build&lt;/code&gt;.
You can see here how it requires that &lt;code&gt;COUNT_BASE&lt;/code&gt;
be &lt;code&gt;TWO&lt;/code&gt; and &lt;code&gt;COUNT_HEIGHT&lt;/code&gt; be &lt;code&gt;ONE&lt;/code&gt;.
The other call counts can be zero or one.

&lt;br/&gt;&lt;br/&gt;
&lt;code&gt;Calc1&lt;/code&gt; is the class that actually does the calculation
of the physical parameters.

&lt;br/&gt;&lt;br/&gt;
Finally, the &lt;code&gt;specsOK&lt;/code&gt; implicit method provides the
link that allows only a complete builder to call the &lt;code&gt;build&lt;/code&gt;
method that returns the calculator.

&lt;br/&gt;&lt;br/&gt;
Here is an example of how you use it:
&lt;pre&gt;&lt;div class="code"
&gt;import Pyramid._       //we need the implicit conversion to be in scope
val p = Pyramid().setLength(10).setWidth(8).setHeight(6).build
p.length                //returns 10
p.area                  //returns 80
p.volume                //returns 160
&lt;/div&gt;&lt;/pre&gt;

Each of the following will give a compiler error:
&lt;pre&gt;&lt;div class="code"
&gt;Pyramid().setWidth(2).setHeight(2).build        //only one BASE param, need 2
Pyramid().setWidth(2).setLength(3).setArea(6).setHeight(2).build  //too many BASE params
Pyramid().setWidth(2).setWidth(3).setHeight(2).build  //setWidth called twice
&lt;/div&gt;&lt;/pre&gt;

Note the second example: even though the base area (6) is compatible with
the width and length,
this line gives a compiler error because three BASE parameters were
specified; the compile time checks do not look at the values of
the parameters.

&lt;br/&gt;&lt;br/&gt;
Let's add a parameter for density such that,
if we have called the setter for density,
we can get back a calculator that can tell us the mass of the pyramid
as well as everything else.
The code below shows, in &lt;b&gt;bold&lt;/b&gt;, what needs to be added to
the above example in order to do that.

&lt;pre&gt;&lt;div class="code"
&gt;object Pyramid {

    //A small collection of class types to define a state machine that counts
    abstract class COUNTER              { type Count &amp;lt;: COUNTER }
    abstract class MANY extends COUNTER { type Count = MANY }
    abstract class TWO  extends COUNTER { type Count = MANY }
    abstract class ZERO_OR_ONE extends COUNTER
    abstract class ONE  extends ZERO_OR_ONE { type Count = TWO }
    abstract class ZERO extends ZERO_OR_ONE { type Count = ONE }

    //We require positive values for our calls
    class Positive(val d:Double) {
        if (d&amp;lt;=0) throw new IllegalArgumentException("non-positive value")
    }
    implicit def doubleToPositive(d:Double) = new Positive(d)
    implicit def intToPositive(n:Int) = new Positive(n)

    //The class that manages the state of our specification
    class Specs private[Pyramid]() { self:Specs =&amp;gt;
        //Caller must set exactly two out of three of these
        val length:Double = 0
        val width:Double = 0
        val area:Double = 0

        //Caller must set exactly one of these two heights
        val height:Double = 0   //vertical height to the tip
        val edge:Double = 0     //from base to tip along an edge

        &lt;b&gt;//Optional value; if set, we can calculate mass
        val density:Double = 0&lt;/b&gt;

        //We maintain compiler-time state to count the two types of calls
        type TT &amp;lt;: {
            type COUNT_LENGTH &amp;lt;: COUNTER
            type COUNT_WIDTH &amp;lt;: COUNTER
            type COUNT_AREA &amp;lt;: COUNTER
            type COUNT_BASE &amp;lt;: COUNTER          // length, width or area
            type COUNT_HEIGHT &amp;lt;: COUNTER
            type COUNT_EDGE &amp;lt;: COUNTER
            type COUNT_VERT &amp;lt;: COUNTER          // height or edge
            &lt;b&gt;type COUNT_DENSITY &amp;lt;: COUNTER&lt;/b&gt;
        }

        class SpecsWith(bb:Specs) extends Specs {
            override val length = bb.length
            override val width = bb.width
            override val area = bb.area
            override val height = bb.height
            override val edge = bb.edge
            &lt;b&gt;override val density = bb.density&lt;/b&gt;
        }

        def setLength(d:Positive) = new SpecsWith(this) {
            override val length:Double = d.d
            type TT = self.TT {
                type COUNT_LENGTH = self.TT#COUNT_LENGTH#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setWidth(d:Positive) = new SpecsWith(this) {
            override val width:Double = d.d
            type TT = self.TT {
                type COUNT_WIDTH = self.TT#COUNT_WIDTH#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setArea(d:Positive) = new SpecsWith(this) {
            override val area:Double = d.d
            type TT = self.TT {
                type COUNT_AREA = self.TT#COUNT_AREA#Count
                type COUNT_BASE = self.TT#COUNT_BASE#Count
            }
        }

        def setHeight(d:Positive) = new SpecsWith(this) {
            override val height:Double = d.d
            type TT = self.TT {
                type COUNT_HEIGHT = self.TT#COUNT_HEIGHT#Count
                type COUNT_VERT = self.TT#COUNT_VERT#Count
            }
        }

        def setEdge(d:Positive) = new SpecsWith(this) {
            override val edge:Double = d.d
            type TT = self.TT {
                type COUNT_EDGE = self.TT#COUNT_EDGE#Count
                type COUNT_VERT = self.TT#COUNT_VERT#Count
            }
        }

        &lt;b&gt;def setDensity(d:Positive) = new SpecsWith(this) {
            override val density:Double = d.d
            type TT = self.TT {
                type COUNT_DENSITY = self.TT#COUNT_DENSITY#Count
            }
        }&lt;/b&gt;

    }

    //Starting point: nothing is set
    def apply() = new Specs {
        type TT = {
            type COUNT_LENGTH = ZERO
            type COUNT_WIDTH = ZERO
            type COUNT_AREA = ZERO
            type COUNT_BASE = ZERO
            type COUNT_HEIGHT = ZERO
            type COUNT_EDGE = ZERO
            type COUNT_VERT = ZERO
            &lt;b&gt;type COUNT_DENSITY = ZERO&lt;/b&gt;
        }
    }

    //Required ending point: two base measures, one height measure,
    //no single parameter more than once
    type CompleteSpecs = Specs {
        type TT &amp;lt;: {
            type COUNT_LENGTH &amp;lt;: ZERO_OR_ONE
            type COUNT_WIDTH &amp;lt;: ZERO_OR_ONE
            type COUNT_AREA &amp;lt;: ZERO_OR_ONE
            type COUNT_BASE = TWO
            type COUNT_HEIGHT &amp;lt;: ZERO_OR_ONE
            type COUNT_EDGE &amp;lt;: ZERO_OR_ONE
            type COUNT_VERT = ONE
        }
    }

    //Calc1 includes the first set of values that can be calculated
    class Calc1 private[Pyramid](spec:CompleteSpecs) {
        import java.lang.Math.sqrt

        //The three related base measures
        lazy val length = if (spec.length!=0) spec.length
                          else spec.area/spec.width
        lazy val width =  if (spec.width!=0) spec.width
                          else spec.area/spec.length
        lazy val area =   if (spec.area!=0) spec.area
                          else spec.length*spec.width

        //The two related height measures
        lazy val height = if (spec.height!=0) spec.height
            else sqrt(spec.edge*spec.edge-length*length/4-width*width/4)
        lazy val edge =   if (spec.edge!=0) spec.edge
            else sqrt(length*length/4+width*width/4+spec.height*spec.height)

        lazy val volume = length * width * height / 3
    }

    implicit def specsOK(spec:CompleteSpecs) = new {
        def build = new Calc1(spec)
    }

    &lt;b&gt;//Second set of allowable computations
    type CompleteSpecs2 = Specs {
        type TT &amp;lt;: {
            type COUNT_LENGTH &amp;lt;: ZERO_OR_ONE
            type COUNT_WIDTH &amp;lt;: ZERO_OR_ONE
            type COUNT_AREA &amp;lt;: ZERO_OR_ONE
            type COUNT_BASE = TWO
            type COUNT_HEIGHT &amp;lt;: ZERO_OR_ONE
            type COUNT_EDGE &amp;lt;: ZERO_OR_ONE
            type COUNT_VERT = ONE
            type COUNT_DENSITY = ONE
        }
    }

    class Calc2 private[Pyramid](spec:CompleteSpecs2) extends Calc1(spec) {
        lazy val mass = spec.density * volume
    }

    implicit def specsOK2(spec:CompleteSpecs2) = new {
        def build2 = new Calc2(spec)
    }&lt;/b&gt;
}
&lt;/div&gt;&lt;/pre&gt;

To get the mass, we have to call all of the appropriate setters,
including density,
then call &lt;code&gt;build2&lt;/code&gt; rather than &lt;code&gt;build&lt;/code&gt; to
get our calculator.
That will give us a calculator that can give us the &lt;code&gt;mass&lt;/code&gt;
value as well as all the values in the calculator returned by
&lt;code&gt;build&lt;/code&gt;.

&lt;pre&gt;&lt;div class="code"
&gt;val p = Pyramid().setHeight(2).setWidth(3).setArea(6).setDensity(2).build2
p.volume        //returns 4
p.mass          //returns 8
&lt;/div&gt;&lt;/pre&gt;

These examples fail:
&lt;pre&gt;&lt;div class="code"
&gt;Pyramid().setHeight(2).setWidth(3).setArea(6).build2   //no density
Pyramid().setHeight(2).setWidth(3).setArea(6).setDensity(2).setDensity(3).build2
        //density specified twice
&lt;/div&gt;&lt;/pre&gt;

I intentionally left out one line when adding the density code,
so the following code compiles:
&lt;pre&gt;&lt;div class="code"
&gt;Pyramid().setHeight(2).setWidth(3).setArea(6).setDensity(2).setDensity(3).build
&lt;/div&gt;&lt;/pre&gt;

Since the &lt;code&gt;build&lt;/code&gt; method returns a calculator that does not
do anything with the density, this is perhaps not a problem.
You can test your understanding of Scala types
by figuring out what one line you need
to add to the density example to make this last call fail to compile.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-936445998608234242?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/936445998608234242/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=936445998608234242' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/936445998608234242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/936445998608234242'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-4.html' title='Type Safe Builder in Scala, Part 4'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4541129778634138924</id><published>2009-09-09T20:41:00.000-07:00</published><updated>2009-09-09T20:41:39.937-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Type Safe Builder in Scala, Part 3</title><content type='html'>Another solution to implementing a type-safe builder in Scala.

&lt;br/&gt;&lt;br/&gt;
In my previous
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-using-church.html"&gt;
two&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html"&gt;
posts&lt;/a&gt;
I presented a couple of different implementations of the
type-safe builder pattern originally
&lt;a href="http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html"&gt;
presented&lt;/a&gt;
by Rafeal de F. Ferreira.
But there was something about them that bothered me:
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-using-church.html"&gt;
both&lt;/a&gt;
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html"&gt;
of&lt;/a&gt;
my implementations,
&lt;a href="http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html"&gt;
both&lt;/a&gt;
&lt;a href="http://snippets.dzone.com/posts/show/5741"&gt;
of&lt;/a&gt; Rafael's implementations,
and
gambistics'
&lt;a href="http://gist.github.com/182249"&gt;
implementation&lt;/a&gt;
written in
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html#c7627891170725872935"&gt;
response&lt;/a&gt; to my second attempt,
all exhibit the same bothersome characteristic:
every setter includes references to all of the state information
(both type information and parameter values)
being built up in the Builder.

&lt;br/&gt;&lt;br/&gt;
I found this to be a very inelegant aspect of these solutions.
Given N parameters with their corresponding setters,
there is an
&lt;a href="http://en.wikipedia.org/wiki/Big_O_notation"&gt;
O(N^2)&lt;/a&gt; maintenance problem:
every time you add or remove a parameter, or make certain
changes to existing parameters, such as changing name or type,
you have to make that change in every setter method.

&lt;br/&gt;&lt;br/&gt;
Below is an implementation without any source code interaction between
the setters or parameters; each setter only deals with its own data.
Adding, removing or changing any parameter and corresponding setter
can be done without dealing with any of the other parameters,
making this implementation O(N) for N parameters.
As with my
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html"&gt;
Part 2&lt;/a&gt; implementation,
this implementation handles both optional and required parameters,
ensuring at compile time that required setters are called exactly once
and optional setters are not called more than once.

&lt;pre&gt;&lt;div class="code"
&gt;object Scotch {

  //A small collection of class types to define a state machine
  abstract class STATE { type TrueOnce &lt;: STATE }
  abstract class NOT_MULTI extends STATE
  abstract class MULTI extends STATE { type TrueOnce = MULTI }
  abstract class TRUE  extends NOT_MULTI { type TrueOnce = MULTI }
  abstract class FALSE extends NOT_MULTI { type TrueOnce = TRUE }

  sealed abstract class Preparation
  case object Neat extends Preparation
  case object OnTheRocks extends Preparation
  case object WithWater extends Preparation

  sealed abstract class Glass
  case object Short extends Glass
  case object Tall extends Glass
  case object Tulip extends Glass

  case class OrderOfScotch private[Scotch] (
        val brand:String, val mode:Preparation,
        val isDouble:Boolean, val glass:Option[Glass])

  class ScotchBuilder private[Scotch]() { self:ScotchBuilder =&gt;
    val theBrand:Option[String] = None
    val theMode:Option[Preparation] = None
    val theDoubleStatus:Option[Boolean] = None
    val theGlass:Option[Glass] = None
    type TT &lt;: {
      type HAS_BRAND &lt;: STATE
      type HAS_MODE &lt;: STATE
      type HAS_DOUBLE &lt;: STATE
      type HAS_GLASS &lt;: STATE
    }

    class ScotchBuilderWith(sb:ScotchBuilder) extends ScotchBuilder {
      override val theBrand = sb.theBrand
      override val theMode = sb.theMode
      override val theDoubleStatus = sb.theDoubleStatus
      override val theGlass = sb.theGlass
    }

    def withBrand(b:String) = new ScotchBuilderWith(this) {
      override val theBrand:Option[String] = Some(b)
      type TT = self.TT { type HAS_BRAND = self.TT#HAS_BRAND#TrueOnce }
    }

    def withMode(p:Preparation) = new ScotchBuilderWith(this) {
      override val theMode:Option[Preparation] = Some(p)
      type TT = self.TT { type HAS_MODE = self.TT#HAS_MODE#TrueOnce }
    }

    def isDouble(b:Boolean) = new ScotchBuilderWith(this) {
      override val theDoubleStatus:Option[Boolean] = Some(b)
      type TT = self.TT { type HAS_DOUBLE = self.TT#HAS_DOUBLE#TrueOnce }
    }
     
    def withGlass(g:Glass) = new ScotchBuilderWith(this) {
      override val theGlass:Option[Glass] = Some(g)
      type TT = self.TT { type HAS_GLASS = self.TT#HAS_GLASS#TrueOnce }
    }
  }

  //Starting point: nothing is set
  lazy val builder = new ScotchBuilder {
    type TT = {
      type HAS_BRAND = FALSE
      type HAS_MODE = FALSE
      type HAS_DOUBLE = FALSE
      type HAS_GLASS = FALSE
    }
  }

  //Required ending point: TRUE for required, NOT_MULTI for optional
  type CompleteBuilder = ScotchBuilder {
    type TT &lt;: {
      type HAS_BRAND = TRUE
      type HAS_MODE = TRUE
      type HAS_DOUBLE = TRUE
      type HAS_GLASS &lt;: NOT_MULTI
    }
  }

  implicit def enableBuild(builder:CompleteBuilder) = new {
    def build() = new OrderOfScotch(
            builder.theBrand.get, builder.theMode.get,
            builder.theDoubleStatus.get, builder.theGlass);
  }

}
&lt;/div&gt;&lt;/pre&gt;

It actually took me quite a while to come up with this solution.
I spent a lot of time trying different things that &lt;i&gt;almost&lt;/i&gt; worked.
Scala's type system is powerful, but debugging is a bear.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4541129778634138924?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4541129778634138924/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4541129778634138924' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4541129778634138924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4541129778634138924'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-3.html' title='Type Safe Builder in Scala, Part 3'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-4979407567454223262</id><published>2009-09-07T09:17:00.000-07:00</published><updated>2009-09-09T20:45:19.541-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Type Safe Builder in Scala, Part 2</title><content type='html'>Here is a way to limit the number of calls to each setter method
in a builder without using Church Numerals.

&lt;br/&gt;&lt;br/&gt;
Update 2009-09-09: See also
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-3.html"&gt;
Part 3&lt;/a&gt;, which shows an O(N) implementation.

&lt;a name="copyright-note"&gt;&lt;/a&gt;
&lt;blockquote style="background-color: #ffd0d0; padding:6; border: medium ridge black"&gt;
&lt;b&gt;Copyright Note:&lt;/b&gt;
The code in this post may not be covered by the LGPL.
&lt;br/&gt;&lt;br/&gt;
The BuilderPattern code is a derivative of code
&lt;a href="http://snippets.dzone.com/posts/show/5741"&gt;posted&lt;/a&gt;
by
&lt;a href="http://blog.rafaelferreira.net/"&gt;
Rafael de F. Ferreira&lt;/a&gt;,
so is covered by his copyright.
&lt;br/&gt;&lt;br/&gt;
All code is used here for educational purposes under
&lt;a href="http://www.copyright.gov/fls/fl102.html"&gt;
Fair Use&lt;/a&gt;.
&lt;/blockquote&gt;

A number of people commented in response to Rafael's original post
that this type-safe builder approach (of explicitly using types to
track desired behavior at compile-time)
is much more complicated than a simple builder class in which the required
parameters are constructor args to the builder, and the optional
parameters are setters.
Of course that's true, and with named parameters and default values
coming in Scala 2.8, defining and using such builders can be even simpler.
But, besides the fact that the type-safe approach can be used in
more complex builders in which the constructor approach does not work well,
the issue is not relevant, because the main point of this exercise is to
see how Scala's type system can be used to do interesting things.

&lt;br/&gt;&lt;br/&gt;
In my
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-using-church.html"&gt;
previous blog post&lt;/a&gt;
I showed how to use Church Numerals
to limit (at compile time)
calls to setters to no more than once per setter.
Using Church Numerals was handy for me because I already had them,
so it was easy to switch from booleans to integers to keep track
of how many times a setter was called.

&lt;br/&gt;&lt;br/&gt;
Keeping count of calls this way could be useful in some context,
but in this case all I was doing was ensuring that an item was called
no more than once.
Also, the recommendation I made in my closing paragraph to use
multiple implicit conversion functions to deal with optional
setters does not scale well when there are multiple optional setters.

&lt;br/&gt;&lt;br/&gt;
Below is a simpler approach that ensures that required setters are
called exactly once, that optional setters are called not more than
once, that doesn't require Church Numerals, and that uses only a
single implicit conversion method.
As before, my changes from Rafael's original are in &lt;b&gt;bold&lt;/b&gt;.

&lt;pre&gt;&lt;div class="code"
&gt;object BuilderPattern {
  sealed abstract class Preparation
  case object Neat extends Preparation
  case object OnTheRocks extends Preparation
  case object WithWater extends Preparation

  sealed abstract class Glass
  case object Short extends Glass
  case object Tall extends Glass
  case object Tulip extends Glass

  case class OrderOfScotch private[BuilderPattern] (val brand:String, val mode:Preparation, val isDouble:Boolean, val glass:Option[Glass])

  &lt;b&gt;abstract class STATE { type TrueOnce &amp;lt;: STATE }
  abstract class NOT_MULTI extends STATE
  abstract class MULTI extends STATE { type TrueOnce = MULTI }
  abstract class TRUE  extends NOT_MULTI { type TrueOnce = MULTI }
  abstract class FALSE extends NOT_MULTI { type TrueOnce = TRUE }&lt;/b&gt;

  abstract class ScotchBuilder { self:ScotchBuilder =&amp;gt;
    protected[BuilderPattern] val theBrand:Option[String]
    protected[BuilderPattern] val theMode:Option[Preparation]
    protected[BuilderPattern] val theDoubleStatus:Option[Boolean]
    protected[BuilderPattern] val theGlass:Option[Glass]

    type HAS_BRAND &amp;lt;: STATE
    type HAS_MODE &amp;lt;: STATE
    type HAS_DOUBLE_STATUS &amp;lt;: STATE
    &lt;b&gt;type HAS_GLASS &amp;lt;: STATE&lt;/b&gt;

    def withBrand(b:String) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = Some(b)
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = self.HAS_BRAND#TrueOnce
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
      &lt;b&gt;type HAS_GLASS = self.HAS_GLASS&lt;/b&gt;
    }

    def withMode(p:Preparation) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = Some(p)
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = self.HAS_MODE#TrueOnce
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
      &lt;b&gt;type HAS_GLASS = self.HAS_GLASS&lt;/b&gt;
    }

    def isDouble(b:Boolean) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = Some(b)
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS#TrueOnce
      &lt;b&gt;type HAS_GLASS = self.HAS_GLASS&lt;/b&gt;
    }
     
    def withGlass(g:Glass) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = Some(g)

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
      &lt;b&gt;type HAS_GLASS = self.HAS_GLASS#TrueOnce&lt;/b&gt;
    }

  }

  type CompleteBuilder = ScotchBuilder {
    type HAS_BRAND = TRUE
    type HAS_MODE = TRUE
    type HAS_DOUBLE_STATUS = TRUE
    &lt;b&gt;type HAS_GLASS &amp;lt;: NOT_MULTI&lt;/b&gt;
  }

  implicit def enableBuild(builder:CompleteBuilder) = new {
    def build() = 
      new OrderOfScotch(builder.theBrand.get, builder.theMode.get, builder.theDoubleStatus.get, builder.theGlass);
  }

  def builder = new ScotchBuilder {
    protected[BuilderPattern] val theBrand:Option[String] = None
    protected[BuilderPattern] val theMode:Option[Preparation] = None
    protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = None
    protected[BuilderPattern] val theGlass:Option[Glass] = None

    type HAS_BRAND = FALSE
    type HAS_MODE = FALSE
    type HAS_DOUBLE_STATUS = FALSE
    &lt;b&gt;type HAS_GLASS = FALSE&lt;/b&gt;
  }
}
&lt;/div&gt;&lt;/pre&gt;

As before, remember to &lt;code&gt;import BuilderPattern._&lt;/code&gt;
to ensure that the implicit conversion method is in scope when
using this patterm.

&lt;br/&gt;&lt;br/&gt;
The changes are straightforward:
&lt;ol&gt;
&lt;li&gt;I added the &lt;code&gt;HAS_GLASS&lt;/code&gt;
    type to track the state of the number of calls
    to the &lt;code&gt;withGlass&lt;/code&gt; method.
    It is coded in exactly the same way as the other state tracking types
    with the one exception of its value in the
    &lt;code&gt;CompleteBuilder&lt;/code&gt; type.
    The &lt;code&gt;CompleteBuilder&lt;/code&gt; type encodes which parameters are
    optional and which are required.
&lt;li&gt;Rather than having two states
    (just &lt;code&gt;TRUE&lt;/code&gt; and &lt;code&gt;FALSE&lt;/code&gt;),
    I am using a little class hierarchy with three states, representing
    no calls to a setter (&lt;code&gt;FALSE&lt;/code&gt;),
    one call to a setter (&lt;code&gt;TRUE&lt;/code&gt;),
    and more than one call to a setter (&lt;code&gt;MULTI&lt;/code&gt;).
&lt;li&gt;Instead of just setting a state to
    &lt;code&gt;TRUE&lt;/code&gt; when a setter is called,
    I am using a type member of the current state to implement a little
    state machine.
    The state machine transitions from
    &lt;code&gt;FALSE&lt;/code&gt; to &lt;code&gt;TRUE&lt;/code&gt; to &lt;code&gt;MULTI&lt;/code&gt; and then
    stays in the &lt;code&gt;MULTI&lt;/code&gt; state.
&lt;li&gt;The &lt;code&gt;TRUE&lt;/code&gt; and &lt;code&gt;FALSE&lt;/code&gt;
    states are in a separate subtree &lt;code&gt;NOT_MULTI&lt;/code&gt; so that
    I can specify a value that includes either of those states,
    but not the &lt;code&gt;MULTI&lt;/code&gt; state.
    I use this value in the &lt;code&gt;CompleteBuilder&lt;/code&gt;
    type to specify that the
    &lt;code&gt;HAS_GLASS&lt;/code&gt; value can be either
    &lt;code&gt;TRUE&lt;/code&gt; or &lt;code&gt;FALSE&lt;/code&gt;.
&lt;/ol&gt;

Bottom line:
this implementation provides compile-time checking that the required
setters are called exactly once,
that the optional setters are called
at most once,
and is much simpler than the Church Numerals implementation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-4979407567454223262?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/4979407567454223262/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=4979407567454223262' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4979407567454223262'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/4979407567454223262'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html' title='Type Safe Builder in Scala, Part 2'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-6987730749420931748</id><published>2009-09-06T20:49:00.000-07:00</published><updated>2009-09-09T20:49:37.596-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Type Safe Builder in Scala Using Church Numerals</title><content type='html'>I present another possibly practical application of
Church Numerals in Scala.

&lt;br/&gt;&lt;br/&gt;
Update 2009-09-07: See also
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-2.html"&gt;
Part 2&lt;/a&gt;, which shows similar functionality without using Church Numerals.

&lt;br/&gt;&lt;br/&gt;
Update 2009-09-09: See also
&lt;a href="http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-part-3.html"&gt;
Part 3&lt;/a&gt;, which shows an O(N) implementation.

&lt;a name="copyright-note"&gt;&lt;/a&gt;
&lt;blockquote style="background-color: #ffd0d0; padding:6; border: medium ridge black"&gt;
&lt;b&gt;Copyright Note:&lt;/b&gt;
The code in this post may not be covered by the LGPL.
&lt;br/&gt;&lt;br/&gt;
The BuilderPattern code is a derivative of code
&lt;a href="http://snippets.dzone.com/posts/show/5741"&gt;posted&lt;/a&gt;
by
&lt;a href="http://blog.rafaelferreira.net/"&gt;
Rafael de F. Ferreira&lt;/a&gt;,
so is covered by his copyright.
&lt;br/&gt;&lt;br/&gt;
The Church Numerals code is from my
&lt;a href="http://jim-mcbeath.blogspot.com/2008/11/practical-church-numerals-in-scala.html"&gt;
Practical Church Numerals&lt;/a&gt;
blog post; see that post for copyright information.
&lt;br/&gt;&lt;br/&gt;
All code is used here for educational purposes under
&lt;a href="http://www.copyright.gov/fls/fl102.html"&gt;
Fair Use&lt;/a&gt;.
&lt;/blockquote&gt;

Recently I came across an
&lt;a href="http://blog.rafaelferreira.net/2008/07/type-safe-builder-pattern-in-scala.html"&gt;
article&lt;/a&gt;
by
&lt;a href="http://blog.rafaelferreira.net/"&gt;
Rafael de F. Ferreira&lt;/a&gt;,
and a
&lt;a href="http://snippets.dzone.com/posts/show/5741"&gt;
followup article&lt;/a&gt;
with an alternate approach,
on how to implement the Builder pattern in Scala in a way that is type-safe.
In particular,
his implementation allows for compile-time checking to ensure that
each required field has been set by a call to a setter
(in Rafael's code, the &lt;code&gt;withXXX&lt;/code&gt; methods)
in the Builder object.
If the developer fails to call all of the required methods,
the application will not compile.
The error messages given by the compiler in this case
are not necessarily clear,
but I like the concept.

&lt;br/&gt;&lt;br/&gt;
One of the thoughts that crossed my mind was,
what happens if you call one of the setter methods twice?
Rafael's type-safe code does not prevent that scenario;
if the coder puts in two calls to the same setter method,
it compiles without problem and calls the setter twice at run time.

&lt;br/&gt;&lt;br/&gt;
Perhaps that is not a concern,
but if we can use the compiler to ensure that required setters are
called, could we also use it to ensure that setters are called no
more than once?
Rafael's code uses boolean type values to encode whether or not
a value has been set.
By using integer values for the types, we can keep a count of how many times
a setter has been called.
That sounds like a job for Church Numerals.

&lt;br/&gt;&lt;br/&gt;
In my post
&lt;a href="http://jim-mcbeath.blogspot.com/2008/11/practical-church-numerals-in-scala.html"&gt;
Practical Church Numerals in Scala&lt;/a&gt;
I showed how you could encode integers in the Scala type system
using the Church Numerals construct,
and use those numerals to construct a type-safe units class.
We can do something similar here.

&lt;br/&gt;&lt;br/&gt;
First, here is an implementation of Church Numerals taken from that
blog posting.
See that post for a description of how it works.

&lt;pre&gt;&lt;div class="code"
&gt;object ChurchNumerals {

    trait CInt {
        type Succ &amp;lt;: CInt
        type Add[N &amp;lt;: CInt] &amp;lt;: CInt
    }

    trait CPos extends CInt

    class CSucc[P &amp;lt;: CPos] extends CPos {
        type Succ = CSucc[CSucc[P]]
        type Add[N &amp;lt;: CInt] = P#Add[N]#Succ
    }

    final class _0 extends CPos {
        type Succ = CSucc[_0]
        type Add[N &amp;lt;: CInt] = N
    }

    type +[N1&amp;lt;:CInt, N2&amp;lt;:CInt] = N1#Add[N2]

    type _1 = _0#Succ
    type _2 = _1#Succ
    type _3 = _2#Succ
}
&lt;/div&gt;&lt;/pre&gt;

Below is my modified version of the type-safe Builder pattern
using Church Numerals instead of booleans for the types.
My changes from Rafael's original are in &lt;b&gt;bold&lt;/b&gt;.

&lt;pre&gt;&lt;div class="code"
&gt;object BuilderPattern {
  &lt;b&gt;import ChurchNumerals._&lt;/b&gt;

  sealed abstract class Preparation
  case object Neat extends Preparation
  case object OnTheRocks extends Preparation
  case object WithWater extends Preparation

  sealed abstract class Glass
  case object Short extends Glass
  case object Tall extends Glass
  case object Tulip extends Glass

  case class OrderOfScotch private[BuilderPattern] (val brand:String, val mode:Preparation, val isDouble:Boolean, val glass:Option[Glass])

  &lt;b&gt;//FALSE and TRUE are not used, we use _0 and _1 instead&lt;/b&gt;

  abstract class ScotchBuilder { self:ScotchBuilder =&amp;gt;
    protected[BuilderPattern] val theBrand:Option[String]
    protected[BuilderPattern] val theMode:Option[Preparation]
    protected[BuilderPattern] val theDoubleStatus:Option[Boolean]
    protected[BuilderPattern] val theGlass:Option[Glass]

    type HAS_BRAND &lt;b&gt;&amp;lt;: ChurchNumerals.CInt&lt;/b&gt;
    type HAS_MODE &lt;b&gt;&amp;lt;: ChurchNumerals.CInt&lt;/b&gt;
    type HAS_DOUBLE_STATUS &lt;b&gt;&amp;lt;: ChurchNumerals.CInt&lt;/b&gt;

    def withBrand(b:String) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = Some(b)
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = &lt;b&gt;self.HAS_BRAND + _1&lt;/b&gt;
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
    }

    def withMode(p:Preparation) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = Some(p)
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = &lt;b&gt;self.HAS_MODE + _1&lt;/b&gt;
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
    }

    def isDouble(b:Boolean) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = Some(b)
      protected[BuilderPattern] val theGlass:Option[Glass] = self.theGlass

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = &lt;b&gt;self.HAS_DOUBLE_STATUS + _1&lt;/b&gt;
    }
     
    def withGlass(g:Glass) = new ScotchBuilder {
      protected[BuilderPattern] val theBrand:Option[String] = self.theBrand
      protected[BuilderPattern] val theMode:Option[Preparation] = self.theMode
      protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = self.theDoubleStatus
      protected[BuilderPattern] val theGlass:Option[Glass] = Some(g)

      type HAS_BRAND = self.HAS_BRAND
      type HAS_MODE = self.HAS_MODE
      type HAS_DOUBLE_STATUS = self.HAS_DOUBLE_STATUS
    }

  }

  type CompleteBuilder = ScotchBuilder {
    type HAS_BRAND = &lt;b&gt;_1&lt;/b&gt;
    type HAS_MODE = &lt;b&gt;_1&lt;/b&gt;
    type HAS_DOUBLE_STATUS = &lt;b&gt;_1&lt;/b&gt;
  }

  implicit def enableBuild(builder:CompleteBuilder) = new {
    def build() = 
      new OrderOfScotch(builder.theBrand.get, builder.theMode.get, builder.theDoubleStatus.get, builder.theGlass);
  }

  def builder = new ScotchBuilder {
    protected[BuilderPattern] val theBrand:Option[String] = None
    protected[BuilderPattern] val theMode:Option[Preparation] = None
    protected[BuilderPattern] val theDoubleStatus:Option[Boolean] = None
    protected[BuilderPattern] val theGlass:Option[Glass] = None

    type HAS_BRAND = &lt;b&gt;_0&lt;/b&gt;
    type HAS_MODE = &lt;b&gt;_0&lt;/b&gt;
    type HAS_DOUBLE_STATUS = &lt;b&gt;_0&lt;/b&gt;
  }
}
&lt;b&gt;//Remember to use "import BuilderPattern._" in your code&lt;/b&gt;
&lt;/div&gt;&lt;/pre&gt;

As you can see, I have replaced the use of the boolean values
&lt;code&gt;TRUE&lt;/code&gt; and &lt;code&gt;FALSE&lt;/code&gt;
by the Church Numerals &lt;code&gt;_0&lt;/code&gt; and &lt;code&gt;_1&lt;/code&gt;.
Specifying the &lt;code&gt;CompleteBuilder&lt;/code&gt; type as requiring &lt;code&gt;_1&lt;/code&gt;
in all the positions ensures that the &lt;code&gt;enableBuild&lt;/code&gt;
implicit method will only be available if each setter has been
called exactly once.
You can try this out yourself in the Scala REPL.
Copy and paste the above two chunks of code,
plus the command &lt;code&gt;import BuilderPattern._&lt;/code&gt;
to ensure the implicit conversion is in scope.
You can then try the following examples.
In particular, the third example contains two calls to
&lt;code&gt;withBrand&lt;/code&gt;, which fails in this implementation
but succeeds in Rafael's original implementation.

&lt;pre&gt;&lt;div class="code"
&gt;builder.withBrand("x").withMode(BuilderPattern.Neat).build       //error
builder.withBrand("x").withMode(BuilderPattern.Neat).isDouble(true).build  //ok
builder.withBrand("x").withMode(BuilderPattern.Neat).isDouble(true).withBrand("y").build  //error
&lt;/div&gt;&lt;/pre&gt;

You may have noticed that I did not modify the &lt;code&gt;withGlass&lt;/code&gt;
method, so that method could be called more than once.
If you want to restrict that method to be called zero or one time,
you could add a &lt;code&gt;HAS_GLASS&lt;/code&gt; type parameter for it,
which you would keep track of in exactly the same way as the other
three type parameters.
The only difference would then be that you would have two flavors of
&lt;code&gt;CompleteBuilder&lt;/code&gt;, one with
&lt;code&gt;type HAS_GLASS = _1&lt;/code&gt; and one with
&lt;code&gt;type HAS_GLASS = _0&lt;/code&gt;,
along with two implicit conversion methods,
one for each of the two &lt;code&gt;CompleteBuilder&lt;/code&gt; types.
This is an example of Rafael's comment that
"we can specify any other point in the lattice"
as a legal state from which to execute the &lt;code&gt;build&lt;/code&gt; method.
I leave the implementation of that step as an exercise for the reader.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7045524330253482541-6987730749420931748?l=jim-mcbeath.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://jim-mcbeath.blogspot.com/feeds/6987730749420931748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7045524330253482541&amp;postID=6987730749420931748' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/6987730749420931748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7045524330253482541/posts/default/6987730749420931748'/><link rel='alternate' type='text/html' href='http://jim-mcbeath.blogspot.com/2009/09/type-safe-builder-in-scala-using-church.html' title='Type Safe Builder in Scala Using Church Numerals'/><author><name>Jim McBeath</name><uri>http://www.blogger.com/profile/10541190774989580614</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7045524330253482541.post-8369970190994897667</id><published>2009-08-21T17:48:00.000-07:00</published><updated>2010-10-17T21:05:48.062-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala Class Linearization</title><content type='html'>When you use Scala traits to inherit implementation
from more than one parent,
Scala uses a technique called linearization to resolve ambiguities.

&lt;h3&gt;Contents&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#overview"&gt;Overview&lt;/a&gt;
&lt;li&gt;&lt;a href="#traits"&gt;Trait Declarations&lt;/a&gt;
&lt;li&gt;&lt;a href="#trait-files"&gt;Trait Class Files&lt;/a&gt;
&lt;li&gt;&lt;a href="#rules"&gt;Linearization Rules&lt;/a&gt;
&lt;li&gt;&lt;a href="#class-initialization"&gt;Class Initialization&lt;/a&gt;
&lt;li&gt;&lt;a href="#methods"&gt;Method Overriding&lt;/a&gt;
&lt;li&gt;&lt;a href="#variables"&gt;Variable Overriding&lt;/a&gt;
&lt;li&gt;&lt;a href="#types"&gt;Type Overriding&lt;/a&gt;
&lt;li&gt;&lt;a href="#glossary"&gt;Glossary&lt;/a&gt;
&lt;/ul&gt;

&lt;a name="overview"&gt;&lt;/a&gt;
&lt;h3&gt;Overview&lt;/h3&gt;

Unlike Java interfaces,
Scala traits can include code, which effectively gives
the ability to do multiple inheritance.
Implementations of multiple inheritance without linearization
can suffer from the
&lt;a href="http://en.wikipedia.org/wiki/Diamond_problem"&gt;
diamond inheritance&lt;/a&gt; problem,
in which there is an ambiguity in how to deal with an attribute
such as a method which could be inherited from either parent.
Linearization specifies a single linear order for all of the
ancestors of a class, including both the regular superclass chain
and the parent chains of all of the traits.

&lt;br/&gt;&lt;br/&gt;
Scala traits that contain code are called &lt;i&gt;mixin&lt;/i&gt; (or mix-in) traits.
If you don't use any mixin traits,
then the only code is in the regular superclass chain,
just as in Java, and linearization is not an issue.
Since linearization is only of interest when using mixin traits,
it is worth reviewing the characteristics and
constraints on such traits.

&lt;a name="traits"&gt;&lt;/a&gt;
&lt;h3&gt;Trait Declarations&lt;/h3&gt;

A Scala trait  can include code,
in which case the trait is called a mixin trait.
The code can be any of the following:
&lt;ul&gt;
&lt;li&gt;method definitions.
&lt;li&gt;mutable and immutable variables (vars and vals).
&lt;li&gt;a no-argument constructor
    (a trait may not have a constructor with parameters).
&lt;/ul&gt;

When considering the inheritance hierarchies of classes and traits,
they are in many ways similar:
&lt;ul&gt;
&lt;li&gt;Both can use &lt;code&gt;with&lt;/code&gt; clauses to inherit from additional traits
    (with some restrictions, see &lt;a href="#rules"&gt;below&lt;/a&gt;).
&lt;li&gt;Every class and trait declaration is always implicitly extended
    to include &lt;code&gt;with ScalaObject&lt;/code&gt; at the end.
    If you explicitly add &lt;code&gt;with ScalaObject&lt;/code&gt;
    you will get an error that it has been inherited twice.
&lt;li&gt;If a class or trait does not explicitly extend any class or trait,
    then its superclass is AnyRef, which compiles to
    java.lang.Object (just as in Java).
&lt;li&gt;If a class or trait is declared to extend a trait directly rather than
    extending a class &lt;code&gt;with&lt;/code&gt; that trait, that declaration
    is treated the same as if it explicitly extended the trait's
    superclass &lt;code&gt;with&lt;/code&gt; t
