<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Josh Suereth</title>
    <subtitle>Josh Suereth&#39;s technical writing, systems architecture, and engineering reflections.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://jsuereth.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://jsuereth.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-09-19T00:00:00+00:00</updated>
    <id>https://jsuereth.com/atom.xml</id>
    <entry xml:lang="en">
        <title>Dead simple Kubernetes home setup</title>
        <published>2026-09-19T00:00:00+00:00</published>
        <updated>2026-09-19T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/dead-simple-kubernetes-home-setup/"/>
        <id>https://jsuereth.com/posts/dead-simple-kubernetes-home-setup/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/dead-simple-kubernetes-home-setup/">&lt;p&gt;Hello Internet. It&#39;s been a hot minute since I used to write here. Life got crazy,
with personal tragedy and changing jobs and demands on my free time, I just didn&#39;t
have the energy to write, but also there were important things I wanted to spend
my time on: Family, Friends, Health. I encourage anyone who&#39;s a chronically online
or open-source advocate to take the time they need to for themselves, for their
lives offline, and health.&lt;/p&gt;
&lt;p&gt;That said, I&#39;ve recently been doing a lot of tinkering, experimenting and learning,
as has the entire Software industry - being thrown on its head by AI. This has led to,
for myself, a dramatically increased velocity of personal projects. I have a lot I&#39;d
love to share, but first - a bit about those personal projects.&lt;/p&gt;
&lt;p&gt;Recently, I took a foray into building out some home infrastructure. Why?
Two reasons - Quota and Learning. The first—running out of GitHub Actions quota—was
a big impetus for me. I&#39;m building out a game, and my GitHub Actions tests for
a private repo were exhausting my quota in the first week of the month, with ~$5-10 per week
in action fees. Doing that math with my subscriptions, we&#39;re talking ~$500 / year
if I continued down the path I was going.&lt;/p&gt;
&lt;p&gt;SO - I decided it was time to look into alternatives &lt;em&gt;and&lt;/em&gt; I&#39;ve been using a lot
more k8s for work and OpenTelemetry. I decided to purchase a refurbished Dell
OptiPlex (~$300) and turn it into a single-node k8s cluster, upon which I can start
hoisting my unbridled enthusiasm (and pet projects). This was less than the
expected yearly bill of github actions!&lt;/p&gt;
&lt;h3 id=&quot;the-pet-infrastructure-trap&quot;&gt;The &quot;Pet Infrastructure&quot; Trap&lt;/h3&gt;
&lt;p&gt;Herein lies the danger - Does this new cluster &lt;em&gt;become&lt;/em&gt; the hobby, or will
I be able to continue working on, e.g. my board game monte-carlo simulations?&lt;/p&gt;
&lt;p&gt;I wanted a no-fuss system that I could get up and running that would accelerate
my personal projects, not cause me to tinker around.&lt;/p&gt;
&lt;p&gt;A few things enable this for me:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;I have sufficient kubernetes knowledge for managing systems, I can spot / preempt maintenance areas.&lt;/li&gt;
&lt;li&gt;I was willing to isolate the cluster and give Agents full debugging access to it.
It&#39;s not used for production, just personal - however there&#39;s still danger here (more on that in separate blog)&lt;/li&gt;
&lt;li&gt;Talos Linux - A product I&#39;m extremely pleased with.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Getting the kubernetes cluster up and running with Talos was mere minutes. Getting the cluster to
actually talk to GitHub Actions and be a viable Action Runner (ARC) took an entire evening - a lot
of debugging.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the actual &lt;em&gt;hard&lt;/em&gt; decisions that had to be made - and you can evaluate if I&#39;ve turned
this into a &quot;pet&quot; or not at the end of the article.&lt;/p&gt;
&lt;h3 id=&quot;dead-simple-native-kubernetes&quot;&gt;Dead Simple, Native Kubernetes&lt;/h3&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://www.siderolabs.com/talos-linux&quot;&gt;Talos Linux&lt;/a&gt; is a no-fuss, minimal operating system that&#39;s designed to be kubernetes natively. Accessing your
box is done through &lt;code&gt;talosctl&lt;/code&gt; (similar to &lt;code&gt;kubectl&lt;/code&gt; but with different functions).  The entire OS is setup and configured similar to k8s, with resources
and YAML files.&lt;/p&gt;
&lt;p&gt;What I really like - is this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Flashing a box takes minutes&lt;/li&gt;
&lt;li&gt;Pushing config brings a new box to the same state as a previous box.&lt;/li&gt;
&lt;li&gt;Forcing all interaction to be through declarative config, means disaster recovery is easier.&lt;/li&gt;
&lt;li&gt;Super minimal footprint / overhead compared to other solutions for k8s.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, on my laptop I can now run the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; talosctl get mounts&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NODE&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;             NAMESPACE   TYPE          ID          VERSION   SOURCE      TARGET   FILESYSTEM TYPE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;192.168.86.243&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;   runtime     MountStatus   EPHEMERAL&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;   1&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;         /dev/sda4   /var     xfs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There&#39;s a variety of &quot;node level&quot; resources and controls you can use to inspect the machine.&lt;/p&gt;
&lt;p&gt;You can then use all your favorite kubectl commands, e.g here&#39;s the current load on the box:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; kubectl&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; top node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NAME&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;            CPU&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;cores&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;   CPU&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;   MEMORY&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;   MEMORY&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;talos-s13-64i&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;   363m         6%       2032Mi          6%&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: When using GitHub Actions - so far - CPU tends to spike to 100%, memory to ~30%.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Within minutes I was literally moving from &quot;setting up the box&quot; to &quot;fitting kubernetes deployments and services on the box&quot;.&lt;/p&gt;
&lt;h3 id=&quot;secrets-and-painless-disaster-recovery&quot;&gt;Secrets and Painless Disaster Recovery&lt;/h3&gt;
&lt;p&gt;The catch with Talos is that the machine config (e.g. &lt;code&gt;taloscfg/controlplane.yaml&lt;/code&gt; which describes your control plane nodes)
&lt;em&gt;also&lt;/em&gt; contains cluster PKI certs. You can&#39;t just check them in plaintext into git and say &quot;I&#39;m safe&quot;.&lt;/p&gt;
&lt;p&gt;My workflow now is to have a script which pushes this file into a secret manager (e.g. 1password, google secret manager), and
another script which can pull that down on new machines. Anytime I make changes to the node (I&#39;m still running 1 box, so it&#39;s a control plane),
I need to push my disaster recovery to a &lt;em&gt;secret manager&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;I then have all my configuration for the kubernetes cluster stored in git. I believe, on failure of this node, I could
purchase a new node and have it running in ~1 hr. This really is a huge win compared to previous &quot;home lab&quot; setups I&#39;ve done
that basically were configure once, forget everything, never use after failure.&lt;/p&gt;
&lt;p&gt;One of the patterns I use here is to make sure any manual configuration steps, if possible, are done via a &lt;code&gt;job&lt;/code&gt; that runs after a service is stood up,
e.g. for MinIO (blob storage) on the device - I have a job which will update its configuration:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;apiVersion&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; batch/v1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;kind&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; Job&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;metadata&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;  name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; minio-init-buckets&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;  namespace&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; caching&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;spec&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;  template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;    spec&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;      ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;      containers&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        -&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;          ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;          command&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /bin/sh&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; -c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          -&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;            echo &amp;quot;Here is my initial setup commands...&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;            echo &amp;quot;We first wait for minio to be reachable, then issue `mc` commands&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;my-new-pet-storage-choices&quot;&gt;My new &quot;Pet&quot;: Storage Choices&lt;/h3&gt;
&lt;p&gt;The biggest &quot;gotcha&quot; I had with Talos was actually choosing storage. For
GitHub Actions - I wanted to re-use caching, and this is relatively easy with
MinIO and gha-cache-server. However - I ran into a lot of issues because it
turns out I did &lt;em&gt;not&lt;/em&gt; understand kubernetes storage options well &lt;em&gt;and&lt;/em&gt;
persistent volume claims on Talos were odd.&lt;/p&gt;
&lt;p&gt;The initial plan was to use Rancher&#39;s &lt;code&gt;local-path-provisioner&lt;/code&gt; since I&#39;m on
a single node. The issue was that this was the limit of what I understand
for kubernetes. I&#39;d never had to dive &lt;em&gt;past&lt;/em&gt; persistent volume declarations
and mounts on services - to actually understand how the provisioning works.&lt;/p&gt;
&lt;p&gt;The short answer is - it didn&#39;t.&lt;/p&gt;
&lt;p&gt;This is where agents came in - both helpful and possibly a little overzealous.
I was able to get them to debug the issue and offer me options, but at this point
I realized I had some things I need to think about, else this turn into a &quot;pet&quot;
infrastructure project:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If I want persistent volumes to be shared across nodes, I need some way to
provision them so all nodes see them, some distributed file storage. Given I only
have one node now, this is overkill, but it&#39;s now in the back of my mind&lt;/li&gt;
&lt;li&gt;The fact Talos has read-only root (it&#39;s baremetal), means some expectations on
mounts were wrong in the system. Gemini and Claude helped get this sorted, BUT
I&#39;m still not certain if their solution isn&#39;t bloated misinterpretation. It
&lt;em&gt;seems&lt;/em&gt; like the solution so far has been 100% in local-path-provisioner, but
there&#39;s also a usage of &lt;code&gt;extraMounts&lt;/code&gt; we had to make inside Talos machine config.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The good news is - the fix I have is repeatable on new machines. It works, I believe
it will continue to, and I was able to get 90% of my private builds migrated from GitHub
Actions remote runners to my custom runner, and I can now work on my personal projects
without sinking money (beyond my subscription) into GitHub.&lt;/p&gt;
&lt;p&gt;The bad news is - this &lt;em&gt;may&lt;/em&gt; still become a pet project for me. There&#39;s a heavy
temptation to go figure out how to optimize the kubernetes cluster - which was
not the point of it.&lt;/p&gt;
&lt;h3 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;Setting up your own kubernetes cluster is &lt;em&gt;dead simple&lt;/em&gt; with Talos. I was floored
at how fast I was able to spin one up, and how minimal / low overhead the cluster
is for the machine. At the rate my hobbies are ramping up, I may purchase another
refurbished box in a year or so to scale out capacity, as I&#39;m already at ~50% of
what I expect this box can handle at a sustained rate.&lt;/p&gt;
&lt;p&gt;I &lt;em&gt;would&lt;/em&gt; recommend anyone doing heavy hobby project activity spin up your own Talos
box - and use declarative config for your setups.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Effective sbt</title>
        <published>2013-06-11T00:00:00+00:00</published>
        <updated>2013-06-11T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/effective-sbt/"/>
        <id>https://jsuereth.com/posts/effective-sbt/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/effective-sbt/">&lt;p&gt;Here&#39;s the slides from my sbt 0.13 talk today at Scaladays [Effective Sbt]:&lt;/p&gt;
&lt;iframe src=&quot;https://docs.google.com/presentation/d/15gmGLiD4Eiyx8e5Kg2eXge5wD80DF6lLG8hSqc6ohmo/embed?start=false&amp;loop=false&amp;delayms=3000&quot; frameborder=&quot;0&quot; width=&quot;580&quot; height=&quot;452&quot; allowfullscreen=&quot;true&quot; mozallowfullscreen=&quot;true&quot; webkitallowfullscreen=&quot;true&quot;&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Functional Programming Patterns for the Asynchronous Web</title>
        <published>2013-02-13T00:00:00+00:00</published>
        <updated>2013-02-13T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/nescala-intro-to-fp-talk/"/>
        <id>https://jsuereth.com/posts/nescala-intro-to-fp-talk/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/nescala-intro-to-fp-talk/">&lt;p&gt;NEScala happened this year, and it was amazing!   Not only was I able to give a &lt;a rel=&quot;external&quot; href=&quot;http://nescala.org/#t-12229286&quot;&gt;talk&lt;/a&gt;, we were also able to record a &lt;a rel=&quot;external&quot; href=&quot;http://scalawags.tv/scalawags-live-at-ne-scala&quot;&gt;scalawags podcast&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For those who missed the talk, it was about how to script together a bunch of asynchronous APIs into a new asynchronous APIs.  SO, generically, we have:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AsynchSubService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; getData&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Data&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AsynchSubService2&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; getData&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Data2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What you want to do is create a new service that can call both asynchronously and non-blocking so that we don&#39;t suck up too many threads waiting for IO (or network response).&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; UberAsynchService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; getData&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Future&lt;/span&gt;&lt;span&gt;[(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Data&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Data2&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;SO, how do I accomplish this across a variety of goals?   The example application is one that checks github statistics to see which projects are more likely to accept pull requests from non-committers.&lt;/p&gt;
&lt;p&gt;Here&#39;s the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/intro-to-fp&quot;&gt;example application&lt;/a&gt;.   It implements the same github statistics service in two ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Directly using &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/intro-to-fp/blob/intro-to-fp/src/main/scala/futures/service.scala&quot;&gt;futures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Using external &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/intro-to-fp/blob/intro-to-fp/src/main/scala/generic/service.scala&quot;&gt;abstractions&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&#39;ll notice that the code to implement the Future-only version is almost exactly the same as the &quot;generic&quot; version.  There&#39;s a bit of difference in supporting code, but most of that is just an implementation of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/scalaz/scalaz&quot;&gt;scalaz&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What &lt;em&gt;is&lt;/em&gt; different is the testing strategies.   The future-based code &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/intro-to-fp/blob/intro-to-fp/src/test/java/futures/ServiceSpec.scala#L30&quot;&gt;has to use &quot;Await&quot; for a result&lt;/a&gt;, which in my experience usually leads to flaky tests in some manner or other, while the abstracted service can implement &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/intro-to-fp/blob/intro-to-fp/src/test/java/generic/ServiceSpec.scala#L12&quot;&gt;single-threaded tests&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;SO, the gain of all that abstraction is the ability to substitute different runtime execution models into our business logic.   Regardless of whether or not you find the gain worth the cost, the underlying patterns of joining and sequencing computations are useful for anyone delving into Scala&#39;s &lt;code&gt;Future&lt;/code&gt; library.&lt;/p&gt;
&lt;p&gt;Here&#39;s a &lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/presentation/d/1EgWaM4Cey1VJE_AfRwS64dKMRffBbpHfMtCm57nc714/pub?start=false&amp;amp;loop=false&amp;amp;delayms=3000&quot;&gt;copy of the slides&lt;/a&gt;, and I beleive a video will be posted shortly.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Coding in Style</title>
        <published>2012-10-14T00:00:00+00:00</published>
        <updated>2012-10-14T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/scala-210/"/>
        <id>https://jsuereth.com/posts/scala-210/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/scala-210/">&lt;p&gt;Last week I re-gave my Scala 2.10 talks from Scalathon at both Twitter and Foursquare.  While the talk still needs some more info in places, I figure it&#39;s a good helper for folks interested in 2.10.0, and now that we&#39;re in RC phase, even more relevant.&lt;/p&gt;
&lt;h2 id=&quot;coding-in-style-scala-2-10-features&quot;&gt;Coding in Style - Scala 2.10 features&lt;/h2&gt;
&lt;p&gt;This talk covers some basic scala style guidelines for what is emerging as &#39;canonical&#39; Scala code in the standard library, as well as how styles are altering based on new Scala 2.10 features.&lt;/p&gt;
&lt;p&gt;Some of these features are considered &#39;obscure&#39; or &#39;non-standard&#39;, I still try to cover what they are and how best to use them.  Some features, like Dynamic types, are considered dangerous and require a language import.  I&#39;d probably save those for a dynamic setting, such as using the Scala REPL for compiling/running your configuration, where runtime is fail-fast and won&#39;t affect correctness of production.&lt;/p&gt;
&lt;p&gt;Other features are still experimental, like the Scala reflection library and macros.  Here, I try to cover some example use cases, as we&#39;re trying to elicit feedback for a final design.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/presentation/pub?id=1o3MC401IwvKUkUp9ydAmgOeZY-Z1ATmWj2VPaOkvuts&amp;amp;start=false&amp;amp;loop=false&amp;amp;delayms=3000&quot;&gt;slides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Map-Reduce - Lessons Learned</title>
        <published>2012-09-26T00:00:00+00:00</published>
        <updated>2012-09-26T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/mapreduce/"/>
        <id>https://jsuereth.com/posts/mapreduce/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/mapreduce/">&lt;p&gt;Yesterday I gave a talk to &lt;a rel=&quot;external&quot; href=&quot;http://www.pghtech.org/networks/PittJug/Default.aspx&quot;&gt;PittJug&lt;/a&gt; about Map-Reduce and a few patterns that are useful.  While I plan to update
the slides with additional drawings, better examples and removed typos, I thought perhaps those in attendance would appreciate being able to see them.&lt;/p&gt;
&lt;p&gt;This talk is meant to provide very basic introduction to map-reduce and show the power of the abstraction through a few high-level patterns.  Hopefully it guides you to the important decisions you have to make when using map-reduce.&lt;/p&gt;
&lt;h2 id=&quot;map-reduce-lessons-learned&quot;&gt;Map-Reduce - Lessons Learned&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/presentation/pub?id=1VuB6IOq7TfnrRTCqd5_S08M_tBt0iF9OxUx2NcTsgic&amp;amp;start=false&amp;amp;loop=false&amp;amp;delayms=5000&quot;&gt;slides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ScalaDays 2012</title>
        <published>2012-04-22T00:00:00+00:00</published>
        <updated>2012-04-22T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/scaladays/"/>
        <id>https://jsuereth.com/posts/scaladays/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/scaladays/">&lt;p&gt;I thought I&#39;d do a quick recap of ScalaDays from my perspective.  ScalaDays has always been a great event, and this one was no exception.&lt;/p&gt;
&lt;p&gt;However, before going to the bits about my impressions and talks, I&#39;ll dump the links to the two talks I gave.   Yes, that&#39;s two talks.  I was grabbed in the hallway and told a speaker didn&#39;t show.  Dumbly I said, &quot;Why don&#39;t we find someone to go up and distract the audience before the next talk?&quot;  This, of course, meant that &lt;em&gt;I&lt;/em&gt; was the someone to distract the audience.   So, I hastily gave a version of the Effective Scala presentation (this one without all the nervous &quot;right&quot;s).&lt;/p&gt;
&lt;p&gt;I also gave a talk on Binary Resilience.  The first three slides tell the story of how &lt;em&gt;defining&lt;/em&gt; binary compatibility is the hard.  After that it&#39;s just a matter of highlighting the issues and showing some alternatives to avoid &lt;code&gt;java.util.Date&lt;/code&gt; issues.  (That is, an issue where you can&#39;t remove an obviously bad class because it breaks binary compatibility).&lt;/p&gt;
&lt;h2 id=&quot;effective-scala&quot;&gt;Effective Scala&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://skillsmatter.com/podcast/scala/effective-scala&quot;&gt;video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/presentation/pub?id=1VjKahh5CMSZ9sKdmHD5HSaO2sloPUKne_yxcnjWYyZk&amp;amp;start=false&amp;amp;loop=false&amp;amp;delayms=3000&quot;&gt;slides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;binary-resilience&quot;&gt;Binary Resilience&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://skillsmatter.com/podcast/scala/binary-resilience&quot;&gt;video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/presentation/pub?id=16E5veHv4Kh3w4t-MjkiHSsjXrqjwvH0Lf8rSN-I23mQ&amp;amp;start=false&amp;amp;loop=false&amp;amp;delayms=3000&quot;&gt;slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/binary-resilience&quot;&gt;project&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;recap&quot;&gt;Recap&lt;/h1&gt;
&lt;p&gt;Well, in the interest of not wasting everyone&#39;s time, I decided I&#39;d pick out three things that struck me.  These aren&#39;t the three most important things, just the first three I resolved out of introspection.  &lt;em&gt;Maybe&lt;/em&gt; that means they&#39;re the three biggest things that hit me, may not.  So, in no particular order:&lt;/p&gt;
&lt;h2 id=&quot;skillsmatter-is-awesome&quot;&gt;SkillsMatter is awesome&lt;/h2&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://skillsmatter.com/&quot;&gt;SkillsMatter&lt;/a&gt; organized the event this year, and did a wonderful job.  They&#39;re changing the game for tech conferences.  They had a live podcast of the conference for people to watch talks &lt;em&gt;and&lt;/em&gt; they posted many talks live almost immediately after shooting.  This helped folks not at the conference keep up with releases and news and such.  Not only that, the conference ran very smoothly and the action never stopped.  The value of any conference is not just the talks, but the &lt;em&gt;interaction&lt;/em&gt; with the speakers and attendees.  I don&#39;t think live videos lesson the attendance of these events, since most people know the value is the &lt;em&gt;people&lt;/em&gt; not the presentation.  SkillsMatter figured this out and brings more interaction, even for those not at the conference.&lt;/p&gt;
&lt;p&gt;If you&#39;re going to run a tech conference, talk with SkillsMatter.  Not only is everyone (i met) who works for them nice/friendly/amazing, they have some rockin anime.  If you&#39;re feeling left out of ScalaDays, go check out the &lt;a rel=&quot;external&quot; href=&quot;http://skillsmatter.com/event/scala/scala-days-2012&quot;&gt;talks&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;scala-usage-is-on-the-rise&quot;&gt;Scala usage is on the rise&lt;/h2&gt;
&lt;p&gt;There were a lot more people at the conference than in previous years.  Not only that, lots of people &lt;em&gt;actively&lt;/em&gt; using scala at work.  I thought I was lucky to bag a typesafe job, but many of these people are doing really interesting work with Scala.  I learned a lot just talking with the folks and seeing what was high prioirty.   We all know compiler speed is important, but I would say that was the #1 thing discussed in hallway conversations.  This has its own implications.  With more of us using Scala, we&#39;re past the honeymoon phase and starting to dig into the real issues in the language and look for solutions.&lt;/p&gt;
&lt;h2 id=&quot;the-scala-community-is-growing&quot;&gt;The Scala Community is growing&lt;/h2&gt;
&lt;p&gt;There was a time where I felt I knew all the projects going on in the Scala community.  This year&#39;s ScalaDays proved that I can&#39;t even delude myself into thinking that anymore.  The diversity of talks, the quality of the talks and the &lt;em&gt;complexity&lt;/em&gt; described in each talk showed how far into each topic the community is diving.  We&#39;re well past &quot;thin wrappers on Java&quot; and &quot;What is the cake pattern&quot;.  The downside is we need more intro or entry-level talks.  Given the size, I think we need some talks for people to learn how to use technologies, like lift, play, rogue, mongo, akka, scalaz, etc.  The community is rich, and we need to teach people the areas they may not be able to follow.&lt;/p&gt;
&lt;h1 id=&quot;fin&quot;&gt;Fin&lt;/h1&gt;
&lt;p&gt;Basically, Scaladays was pretty amazing.  I spent most of my days talking with folks and my evenings catching up on talks I missed (thankfully published online).  It was exhausting, but well worth it.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Effective Scala</title>
        <published>2012-03-16T00:00:00+00:00</published>
        <updated>2012-03-16T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/effective-scala/"/>
        <id>https://jsuereth.com/posts/effective-scala/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/effective-scala/">&lt;p&gt;Well it looks like &lt;a rel=&quot;external&quot; href=&quot;http://marakana.com/s/video_nescala_keynote_effective_scala_with_josh_suereth,1093/index.html&quot;&gt;my keynote from nescala&lt;/a&gt; is now live! The nescala conference is was good times last year, and this year was no exception.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note: If you want a drinking game, just take a shot everytime I say &quot;right&quot;.&lt;/em&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Iteratees</title>
        <published>2012-02-29T00:00:00+00:00</published>
        <updated>2012-02-29T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/iteratees/"/>
        <id>https://jsuereth.com/posts/iteratees/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/iteratees/">&lt;p&gt;There&#39;s been a lot of hype about Iteratees in the past, as well as several good &lt;a rel=&quot;external&quot; href=&quot;http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/&quot;&gt;articles&lt;/a&gt;.  I&#39;ve been playing around with them in my spare time, and they have some really neat properties.  Before getting into those, let&#39;s cover the basics.&lt;/p&gt;
&lt;p&gt;Iteratees are an immutable implementation of a Consumer/Producer pattern.  A Consumer takes input and produces an output. A Consumer can have one of three states:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Processing&lt;/li&gt;
&lt;li&gt;Finished&lt;/li&gt;
&lt;li&gt;Encountered an Error&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&#39;s look at a possible implementation.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;sealed&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; I&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Done&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Error&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;t&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Throwable&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Processing state contains a function that takes input from a Producer, and returns the next consumer.  Remember that these are immutable, so any state change must return a new object.  The Done state contains the result of a given consumer, and the FatalError state holds a single java error representing what failed.  The actual consumer class will look like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; fold&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;fold&lt;/code&gt; method is the only necessary method for a consumer.  The fold method takes a function that operates on any consumer state and returns a value in the iteratee &lt;code&gt;Context&lt;/code&gt;.   The &lt;code&gt;Context&lt;/code&gt; is the mechanism that is evaluating an iteratee.   This is what lets asynchronous iteratees return values in a &lt;code&gt;Future&lt;/code&gt; and operate on different threads.
h
&lt;em&gt;Note: &lt;code&gt;Consumer&lt;/code&gt;s are called &lt;code&gt;Iteratee&lt;/code&gt;s in Haskell/Scalaz iteratee libraries.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Next is the Producer interface.  A Producer should generate input values to feed into a Consumer.   The producer &#39;drives&#39; the Consumer to some final state, which is returned.   Let&#39;s look at the interface:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; into&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;into&lt;/code&gt; method takes a consumer, drives whatever data is in the Producer through the consumer, and returns the resulting consumer state.   Note that this resulting consumer could still accept more input.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note: &lt;code&gt;Producer&lt;/code&gt;s are called &lt;code&gt;Enumerator&lt;/code&gt;s in Haskell/Scalaz iteratee libraries.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Let&#39;s define a &lt;code&gt;Producer.once&lt;/code&gt; method that can take a single value and feed it into an &lt;code&gt;Consumer&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; once&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; into&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; input fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;(f)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; f(value)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; other         &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(other)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The new producer simply folds over the consumer&#39;s state.   If it&#39;s a processing consumer, we feed the value and return the result.   If the consumer is in any other state, we return that state in a consumer.&lt;/p&gt;
&lt;p&gt;The code has one major problem.  The fold method returns values &lt;em&gt;inside&lt;/em&gt; the &lt;code&gt;Context&lt;/code&gt; of the iteratee.  That is, we can&#39;t peek into an consumer, unless we know where/how it is running and &lt;em&gt;are in that context&lt;/em&gt;.   This way, if the consumer is running inside a &lt;code&gt;Future&lt;/code&gt;, all processing code will join with the consumer.  Let&#39;s modify the code.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; once&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; into&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;.flatten(input fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;(f)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(f(value))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; other         &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;(other))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code assumes two methods.   The &lt;code&gt;contexted&lt;/code&gt; method takes any expression, and evaluates it in the iteratee context.   The signature is &lt;code&gt;def contexted[A](a: =&amp;gt;A): Context[A]&lt;/code&gt;.  The &lt;code&gt;Consumer.flatten&lt;/code&gt; method can take a &lt;code&gt;Context[Consumer[I,O]]&lt;/code&gt; and flatten it out into a regular &lt;code&gt;Consumer[I,O]&lt;/code&gt;.  (&lt;em&gt;Implementation is &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/generic/iteratees.scala#L156&quot;&gt;here&lt;/a&gt;&lt;/em&gt;).&lt;/p&gt;
&lt;h2 id=&quot;chaining-consumers&quot;&gt;Chaining Consumers&lt;/h2&gt;
&lt;p&gt;The biggest reason to use Iteratees is their composability.   That is, we should be able to chain Consumers, such that one will run and then another.   We should also be able to &quot;zip&quot; Consumers, such that two can run against the same input.   To make this happen, we need to adapt our notion of consumer state a bit.  First, let&#39;s ensure that &#39;leftover&#39; input from one Consumer can be fed to the next when chaining.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Done&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;result&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;remainingInput&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, the completed state has an additional value of the remaining input that was not consumed by the first iteratee.  We can use this to implement our sequencing operation &lt;code&gt;andThen&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; andThen&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O2&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;next&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; O&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O2&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt; flatten this.fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Done&lt;/span&gt;&lt;span&gt;(a, remaining)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  =&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Producer&lt;/span&gt;&lt;span&gt;.once(remaining) into next(a))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;(k)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  =&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(in &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; k(in) andThen next)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Error&lt;/span&gt;&lt;span&gt;(e)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(e)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The algorithm for chaining is simple:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If the first consumer is done, feed the remaining input into the next consumer and return the resulting state.&lt;/li&gt;
&lt;li&gt;If the first consumer is still processing, feed data into it, and delay creating the second consumer.&lt;/li&gt;
&lt;li&gt;If the first consumer is in the error state, return that error state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The fold pattern-match above reflects exactly that.&lt;/p&gt;
&lt;p&gt;For those, who may be curious, the above is actually the implementation for &lt;code&gt;flatMap&lt;/code&gt; on &lt;code&gt;Consumer&lt;/code&gt; and will let us use &lt;code&gt;Consumer&lt;/code&gt;s in for expressions.  To experiment, let&#39;s implement a few Consumers, starting with &lt;code&gt;peek&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;peek&lt;/code&gt; consumer should take in an input value, return that as its done state and also put that value back onto its &lt;code&gt;remaining&lt;/code&gt; value so that the next &lt;code&gt;Consumer&lt;/code&gt; in the sequence will receive it again.  Hence, we just peek at the value without really consuming it.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; peek&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; handler&lt;/span&gt;&lt;span&gt;(input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(input, input))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(handler))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;peek&lt;/code&gt; consumer is pretty trivial.   Let&#39;s implement a &lt;code&gt;head&lt;/code&gt; consumer that will actually consume the first element.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; head&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; handler&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(input, &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;???&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(handler))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This looks just like &lt;code&gt;peek&lt;/code&gt;, except we need some way of not placing the previous input as the remaining input.   To do this, we need a way of creating an &quot;empty&quot; input.   While there are several ways to do this, the simplest is by adding an additonal level of abstraction.   Let&#39;s change our definition of consumers as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;sealed&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;value&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EmptyChunk&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EOF&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Nothing&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, there is a &lt;code&gt;StreamValue&lt;/code&gt; trait that could be one of there things:  A valid input, an empty input or a marker that input is at its end.  The Processing state is updated to now take in a &lt;code&gt;StreamValue&lt;/code&gt; and return the next consumer.&lt;/p&gt;
&lt;p&gt;Let&#39;s reimplement &lt;code&gt;peek&lt;/code&gt; and &lt;code&gt;head&lt;/code&gt; now.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; peek&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; handler&lt;/span&gt;&lt;span&gt;(input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; input &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;(input)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(input), &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Chunk&lt;/span&gt;&lt;span&gt;(input)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EOF&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;None&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;EmptyChunk&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span&gt; _            &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; peek&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(handler))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; head&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; handler&lt;/span&gt;&lt;span&gt;(input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; input &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;(input)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(input), &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;EmptyChunk&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EOF&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;None&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;EmptyChunk&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span&gt; _            &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(handler))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;peek&lt;/code&gt; and &lt;code&gt;head&lt;/code&gt; consumers now need to return optional values, because they could receive an &lt;code&gt;EOF&lt;/code&gt; input before having a chance to see the value.   The only significant difference between these two is that &lt;code&gt;head&lt;/code&gt; will return an &lt;code&gt;EmptyChunk&lt;/code&gt; for remaining input, whereas &lt;code&gt;peek&lt;/code&gt; will return the original input.   Now that we have these consumers, we can stream them together.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; headTwice&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Boolean&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    p &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; peek&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    h &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; yield&lt;/span&gt;&lt;span&gt; p &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;==&lt;/span&gt;&lt;span&gt; h&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will run the &lt;code&gt;peek&lt;/code&gt; Consumer and then feed remaining input to &lt;code&gt;head&lt;/code&gt;.   The headTwice observer should &lt;em&gt;always&lt;/em&gt; return true, since peek does not actually consume the value from the stream.&lt;/p&gt;
&lt;p&gt;The ability to sequence Consumers using for-expressions is one of their strengths.  Complex protocols can be as simple as a large for-expression.&lt;/p&gt;
&lt;h2 id=&quot;zipping-consumers&quot;&gt;Zipping Consumers&lt;/h2&gt;
&lt;p&gt;Two Consumers can also be &#39;zipped&#39; or run off the same input.   This can be handy, e.g. when checksuming a file while parsing it.  Let&#39;s define a ZippedConsumer:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ZippedConsumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;first&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;second&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;      // Helper method to push data through both iteratees.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; drive&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ZippedConsumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Producer&lt;/span&gt;&lt;span&gt;.once(input) into first, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Producer&lt;/span&gt;&lt;span&gt;.once(input) into second)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; fold&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Context&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        first.fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;span&gt; state1 &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          second.fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;span&gt; state2 &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (state1, state2)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;              case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(a, i), &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(b, _))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; f(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(a&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;b, i))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;              // TODO - Both cases are errors....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;              case&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(e,k), other)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;               =&amp;gt;&lt;/span&gt;&lt;span&gt; f(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(e, k))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;              case&lt;/span&gt;&lt;span&gt; (other, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(e,k))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;               =&amp;gt;&lt;/span&gt;&lt;span&gt; f(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Error&lt;/span&gt;&lt;span&gt;(e, k))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;              case&lt;/span&gt;&lt;span&gt; _                                          &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; f(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(drive))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;ZippedConsumer&lt;/code&gt; implements two methods.  The first, &lt;code&gt;drive&lt;/code&gt; is a private helper method.  If both underlying consumers are in a processing state, this will feed incoming data into them and return a new ZippedConsumer with the resulting new consumers.  This method is used later.
The second method, &lt;code&gt;fold&lt;/code&gt; is the core method of Consumers.  This method in turn, folds over the underlying states of the two consumers and determines a new combined state.  In the event that both are completed, then the ZippedConsumer enters its completed state, returning a tuple of both results.   If either consumer is in an error state, the zipped consumer is in an error state.   If either consumer is still processing, the zipped consumer uses the &lt;code&gt;drive&lt;/code&gt; method to continue processing.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;ZippedConsumer&lt;/code&gt; can be exposed via a &lt;code&gt;zip&lt;/code&gt; method.   Now, given a consumer protocol consisting of &lt;code&gt;msgHdr&lt;/code&gt; and &lt;code&gt;bodyProcessor&lt;/code&gt; and a consumer &lt;code&gt;sha1sum&lt;/code&gt;, we can construct a new Consumer which reads in a message and validates its sha1sum:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; protocol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ByteBuffer&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Message&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      hdr &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; msgHdr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      (body, sha)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span&gt; bodyProcessor(hdr) zip sha1sum&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      if&lt;/span&gt;&lt;span&gt; sha &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;==&lt;/span&gt;&lt;span&gt; hdr.sha&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; yield&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Message&lt;/span&gt;&lt;span&gt;(hdr,body)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Note: the above assumes we have a valid &lt;code&gt;withFilter&lt;/code&gt; or &lt;code&gt;filter&lt;/code&gt; method on &lt;code&gt;Consumer&lt;/code&gt;s.  This method converts the Consumer state to an error if the condition does not hold.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There&#39;s a lot of flexibility with iteratees now that we can parallel or sequentially consume data.  Since every consumer is immutable, the sky&#39;s the limit on how you compose them.&lt;/p&gt;
&lt;h2 id=&quot;stream-conversions&quot;&gt;Stream Conversions&lt;/h2&gt;
&lt;p&gt;The Iteratee library is starting to look nice, but there&#39;s still another concern.  Say, we have a &lt;code&gt;Producer&lt;/code&gt; of byte buffers from files.   However, in my program, I&#39;d like to write consumers against &lt;em&gt;lines&lt;/em&gt; of input.  If I have a conversion from streams of byte buffers to streams of character buffers, and another from character buffers to delineated &quot;lines&quot;, then I should be able to chain these on a producer and use consumers of lines.&lt;/p&gt;
&lt;p&gt;For example, here&#39;s what a line-count algorithm should look like against a file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; read_channel_bytes(fileChannel, directBuffers&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; chars&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; bytes convert charset.decoder()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; lines&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; chars convert lineDecoder&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; lineCount&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; lines into counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  lineCount fold &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Done&lt;/span&gt;&lt;span&gt;(count, _)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; contexted(count)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    case&lt;/span&gt;&lt;span&gt; _              &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; sys.error(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;NO result!&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, &lt;code&gt;read_channel_bytes&lt;/code&gt; constructs a consumer of byte buffers from the file.   The &lt;code&gt;convert&lt;/code&gt; method takes a stream conversion and returns a new Producer with transformed stream.   So, first &lt;code&gt;bytes&lt;/code&gt; are converted into &lt;code&gt;chars&lt;/code&gt; by a &lt;code&gt;charset.decoder()&lt;/code&gt;, then &lt;code&gt;chars&lt;/code&gt; are converted into &lt;code&gt;lines&lt;/code&gt; by the &lt;code&gt;lines&lt;/code&gt; decoder.  Finally, &lt;code&gt;lines&lt;/code&gt; are fed into a &lt;code&gt;counter&lt;/code&gt; Consumer, yielding a line count.&lt;/p&gt;
&lt;p&gt;Let&#39;s first look at the count consumer, since that&#39;s the simplest:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; counter&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Long&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; step&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;count&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Long&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Long&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; e &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EOF&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Done&lt;/span&gt;&lt;span&gt;(count, e))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Chunk&lt;/span&gt;&lt;span&gt;(_)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Procesing&lt;/span&gt;&lt;span&gt;(step(count &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; EmptyChunk&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(step(count)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;    Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(step(&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;0&lt;/span&gt;&lt;span&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;counter&lt;/code&gt; consumer keeps track of the count in the &lt;code&gt;step&lt;/code&gt; function.   This function returns the next processing step.&lt;/p&gt;
&lt;p&gt;Now, let&#39;s look at the interface for a StreamConversion.  &lt;em&gt;Note: In Haskell/Scalaz these are called &lt;code&gt;Enumeratee&lt;/code&gt;s.&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamConversion&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; apply&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;i&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; convert&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;i&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I2&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        apply(i).join&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A StreamConversion has two methods: &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;convert&lt;/code&gt;.  The &lt;code&gt;convert&lt;/code&gt; method, seen above, can take a &lt;code&gt;Consumer&lt;/code&gt; of one type of input stream (&lt;code&gt;I&lt;/code&gt;) and change it into a consumer of another type (&lt;code&gt;I2&lt;/code&gt;).  The &lt;code&gt;apply&lt;/code&gt; method is the only abstract method of a stream conversion.   This method constructs a Consumer that has a result of an underlying consumer.   This is essentially what a conversion is.  We construct one consumer, which adapts an incoming stream and feeds into the underlying consumer, eventually returning the result.  The &lt;code&gt;join&lt;/code&gt; method, found &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/generic/iteratees.scala#L72&quot;&gt;here&lt;/a&gt; is a convenience method in this situation.   Most of the time, we don&#39;t really care how a stream is being converted, we just want to pretend there is a new consumer.&lt;/p&gt;
&lt;p&gt;Finally, &lt;code&gt;Producer&lt;/code&gt;s can also have a &lt;code&gt;convert&lt;/code&gt; method that takes a &lt;code&gt;StreamConversion&lt;/code&gt; and modifes their &lt;em&gt;output&lt;/em&gt;.   Let&#39;s take a look.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; self &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; into&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; convert&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;conversion&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamConversion&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; into&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;c&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;        Consumer&lt;/span&gt;&lt;span&gt; flatten (self into (conversion apply c)).result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; toString&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;ConvertedProducer(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;+&lt;/span&gt;&lt;span&gt;self&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; by &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; +&lt;/span&gt;&lt;span&gt; conversion &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This &lt;code&gt;convert&lt;/code&gt; method simply takes any Consumer passed in and first wraps it by the stream conversion before pushing data into it.  Since everything in this framework is immutable, it&#39;s ok to re-use the same stream conversions (decoders) over and over and over again.&lt;/p&gt;
&lt;p&gt;For some example Stream Conversions, see the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/channels/charchannels.scala#L24&quot;&gt;charset decoder&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/channels/charchannels.scala#L90&quot;&gt;line splitter&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/channels/charchannels.scala#L64&quot;&gt;word splitter&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;communication-channel-between-producers-consumers&quot;&gt;Communication channel between Producers/Consumers&lt;/h2&gt;
&lt;p&gt;So far, we&#39;ve seen that iteratees are great for streaming data.   However, what if the stream support random access?   We need a way to communicate &lt;em&gt;between&lt;/em&gt; &lt;code&gt;Producer&lt;/code&gt;s and &lt;code&gt;Consumer&lt;/code&gt;s.   So far, a &lt;code&gt;Producer&lt;/code&gt; can find out a consumer&#39;s state simply by &lt;code&gt;fold&lt;/code&gt;ing on it, and a Consumer receives information from the producer via the &lt;code&gt;StreamValue&lt;/code&gt; hierarchy.  If we want the &lt;code&gt;Consumer&lt;/code&gt; to be able to tell the &lt;code&gt;Producer&lt;/code&gt; to move his input stream to a new location, we can accomplish this through some kind of channel in the &lt;code&gt;ConsumerState&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since a &lt;code&gt;Consumer&lt;/code&gt; requesting some random-access seek, or other operation, be performed is probably in the midst of processing data, it makes sense to place a new message channel on the &lt;code&gt;Processing&lt;/code&gt; state class.   Let&#39;s take a look:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ProcessingMessage&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;case class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Processing&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;next&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StreamValue&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;                           optMsg&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ProcessingMessage&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; None&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ConsumerState&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;I&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;O&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, a &lt;code&gt;Consumer&lt;/code&gt; can include an optional processing message for &lt;code&gt;Producer&lt;/code&gt;s.  All of the &lt;code&gt;ZippedConsumer&lt;/code&gt;, &lt;code&gt;andThen&lt;/code&gt; and other combinator logic has to be changed, such that the processing messages can be propogated.   However, when complete, we can create a new Consumer called &quot;seekTo&quot;.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; seekTo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;offset&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Long&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ByteBuffer&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Unit&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;      Consumer&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Processing&lt;/span&gt;&lt;span&gt;(in &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;.done((), in), &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Some&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;RandomAccessMsg&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;SeekTo&lt;/span&gt;&lt;span&gt;(offset))))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This consumer will accept one input, and immediately be done, placing that input back into the stream.  It also carries the &lt;code&gt;RandomAccess.SeekTo&lt;/code&gt; message (not shown before).  This message may or may not be handled by our Producer.  However, if the Producer can support the SeekTo message, it will move the stream and then continue sending input.  You can see an example implementation for file channels &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scalaz/blob/scalaz-nio2/nio/src/main/scala/scalaz/nio/channels/filechannels.scala#L41&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This allows us to use for-expressions to send messages to the producer.  e.g., we could write the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; findData&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;key&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Long&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ByteBuffer&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Data&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    index &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; readIndex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    hdr &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&lt;/span&gt;&lt;span&gt; index get key&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _     &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; seekTo(hdr.diskLocation)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data  &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; grabBlob(hdr.size) &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; yield&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This consumer will first consume the index at the head of a file, and then look for the location on disk to consume the remaining data.  This is a very elegant way to nest seek commands with other &#39;normal&#39; consumers.&lt;/p&gt;
&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;
&lt;p&gt;Hopefully, we&#39;ve shown some insight in to the design of a good iteratee library here.  I&#39;d like to finish up with an example program from my current toy branch of Scalaz where I play with iteratees.&lt;/p&gt;
&lt;p&gt;Here&#39;s a program with will perform the same calculations as the linux command line &lt;code&gt;wc&lt;/code&gt;.  That is, it will count words, characters and lines in a file.   Note that if the file does not line up with the default encoding for a platform, this will barf error messages.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scalaz&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;nio&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scalaz&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;effect&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;IO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scalaz&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;syntax&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;monad&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scalaz&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;nio&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;buffers&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;/** Ensures a file is closed. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; withFile&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;file&lt;/span&gt;&lt;span&gt;: java.io.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;File&lt;/span&gt;&lt;span&gt;)(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;proc&lt;/span&gt;&lt;span&gt;: java.nio.channels.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;FileChannel&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stream &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; IO&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span&gt; java.io.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;FileInputStream&lt;/span&gt;&lt;span&gt;(file))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    result &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; proc(stream.getChannel) onException(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;IO&lt;/span&gt;&lt;span&gt;(stream.close()))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    _      &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; IO&lt;/span&gt;&lt;span&gt;(stream.close())&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; yield&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;// Mimics WC command-line&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; wcLzy&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;file&lt;/span&gt;&lt;span&gt;: java.io.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;File&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; IO&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  withFile(file)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ImmutableBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Byte&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Read&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      bytechannels.read_channel_bytes(c, directBuffers&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=true&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; chars&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Producer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ImmutableBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Char&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Read&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      bytes convert charsets.decoder()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; wordCount&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ImmutableBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Char&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Read&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Long&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      charchannels.words convert utils.counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; lineCount&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ImmutableBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Char&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Read&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Long&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      charchannels.lines convert utils.counter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; allCount&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Consumer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ImmutableBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Char&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Read&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;String&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      lineCount zip wordCount zip utils.lengthCounter(_.remaining) map &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;        case&lt;/span&gt;&lt;span&gt;((lc, wc), cc)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;lines: %d, words %d, chars %s&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt; format (lc,wc,cc)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    chars into allCount result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; wc&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;file&lt;/span&gt;&lt;span&gt;: java.io.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;File&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  wcLzy(file).unsafePerformIO&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the Consumers are being threaded through Scalaz&#39;s IO monad.  This is not a necessity of the library and in fact, my goal for the asynchronous bits is to use futures rather than IO to better represent the execution model.&lt;/p&gt;
&lt;p&gt;I hope that this has simplified your view of Iteratees.  They really are an amazingly flexible, elegant system of processing inputs and producing outputs.  I&#39;m only at the beginnings of a decent Iteratee library, and already I can see the huge power in the ability to avoid re-streaming data through combining consumers.&lt;/p&gt;
&lt;p&gt;It&#39;s my personal opinion that Iteratees demonstrate a lot of good design principles of functional programming.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Implementing class state in terms of a &#39;fold&#39; operation  (Note: We combine &#39;fold&#39; with classical OO inheritance here).&lt;/li&gt;
&lt;li&gt;Sequencing workflows inside a monadic context&lt;/li&gt;
&lt;li&gt;Combinator operators to build large things from small things&lt;/li&gt;
&lt;li&gt;Leveraging immutability to acccomplish the above.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;One aspect that I didn&#39;t get to delve into much is &quot;Leveraging category theory for minimal implemenations with rich APIs&quot;.  Perhaps in a blog post covering Scalaz7.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>SBT Cookbook Talk</title>
        <published>2012-01-11T00:00:00+00:00</published>
        <updated>2012-01-11T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/sbt-cookbook-talk/"/>
        <id>https://jsuereth.com/posts/sbt-cookbook-talk/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/sbt-cookbook-talk/">&lt;p&gt;Well, I was in the SF/Bay Area and gave two talks on SBT while I was there.   Here&#39;s the &lt;a rel=&quot;external&quot; href=&quot;http://www.youtube.com/watch?v=V2rl62CZPVc&quot;&gt;first&lt;/a&gt;, which had a few slide organization issues I corrected before the second (which I hope to post shortly, assuming the video makes it online soon.)  This video is really high quality, so thanks to Markana, &lt;a rel=&quot;external&quot; href=&quot;http://klout.com&quot;&gt;Klout&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http://www.twitter.com/mslinn&quot;&gt;Michael Slinn&lt;/a&gt; and the &lt;a href=&quot;ttp://www.meetup.com/Scala-for-startups&quot;&gt;Scala for Startups&lt;/a&gt; / &lt;a rel=&quot;external&quot; href=&quot;http://www.meetup.com/SF-Scala/&quot;&gt;SF Scala&lt;/a&gt; meetup folks!&lt;/p&gt;
&lt;p&gt;Here&#39;s a &lt;a rel=&quot;external&quot; href=&quot;https://docs.google.com/present/view?id=dfqn4jb_115x89dq2dg&quot;&gt;copy of the slides&lt;/a&gt; (updated for the second talk, hopefully it&#39;s not confusing).&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Scala Community Awards</title>
        <published>2012-01-10T00:00:00+00:00</published>
        <updated>2012-01-10T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/scala-community-awards/"/>
        <id>https://jsuereth.com/posts/scala-community-awards/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/scala-community-awards/">&lt;p&gt;&lt;em&gt;UPDATE&lt;/em&gt; - Akka has weighed in on the award!&lt;/p&gt;
&lt;p&gt;Recently, a lot of Scala projects have moved to github!  This has drastically reduced the overhead of accepting community patches, and the community has been &lt;em&gt;wonderful&lt;/em&gt; in making Scala and its documentation better.  The number of contributions and the care in commits shows that Scala has one of the best open source communities around!&lt;/p&gt;
&lt;p&gt;I plan to show statistics for the increase in community contributions for Scala, but I&#39;m waiting a bit until we have some statistical relevance in contributions.  Right now, it&#39;s still to early to &#39;prove&#39; anything, but the numbers appear to be tracking (if you ignore the blip known as Paul Phillips before he was a Typesafe employee).&lt;/p&gt;
&lt;p&gt;However, in the meantime, I&#39;d like to start a quarterly tradition of recognizing those who have been invaluable in their contributions to Scala and are not members of either Typesafe, Inc. or students/teachers at EPFL.  I&#39;m calling this the &quot;Josh Suereth thinks you&#39;re a swell contributor Award&quot;.  Copyright pending.&lt;/p&gt;
&lt;h2 id=&quot;the-scala-documentation-site&quot;&gt;The Scala Documentation Site&lt;/h2&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;http://docs.scala-lang.org&quot;&gt;documentation site&lt;/a&gt; is the brainchild of Heather Miller at EPFL.  It simplifies the process of creating and updating Scala documentation for all of us.  The source code is available &lt;a rel=&quot;external&quot; href=&quot;http://github.com/scala/scala.github.com&quot;&gt;here&lt;/a&gt; and all merged pull requests are updated &lt;em&gt;immediately&lt;/em&gt; thanks to github pages.&lt;/p&gt;
&lt;p&gt;This new site gained many contributors almost immediately, and the community has been wonderful in making sure that the documentation is high quality, relevant and up to date.&lt;/p&gt;
&lt;p&gt;While there are &lt;em&gt;MANY&lt;/em&gt; contributors here (Thanks so much to all of you), two names stand out as distinguished contributors:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Daniel Sobral - For his continued vigilance in ensuring free documentation and translating his great Stack Overflow answers to the documentation format&lt;/li&gt;
&lt;li&gt;Santiago Basulto - For his translation of the tour of Scala into Spanish.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-scala-ide&quot;&gt;The Scala IDE&lt;/h2&gt;
&lt;p&gt;The Scala IDE has undergone a lot of clean up and stabilization efforts from my coworkers at typesafe recently.   However, the community has contributed some outstanding features that I make use of all the time.   In particular, two names stand out:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Mirko Stocker - for his refactoring library and continued maintenance and improvements.&lt;/li&gt;
&lt;li&gt;Matt Russell - for scalariform and its integration into the Scala IDE.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;scala&quot;&gt;Scala&lt;/h2&gt;
&lt;p&gt;Scala is one of the most difficult projects to contribute to in the Scala community.  Due to its widespread user base, and complicated interaction of compiler features, patches must be rigorously tested, reviewed and revisited (although adding tests is usually easy).  Even with a high bar, Scala receives many great contributions and has a good community around it.  Since this is the first swell contributor award, I&#39;m going to pull some old commits that I feel are significant.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Todd Vierling - For his patch to drastically reduce the size of the collections library&lt;/li&gt;
&lt;li&gt;Rex Kerr - For his murmur hash implementation and performance considerations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Also special thanks to Seth Tissue for ensuring great Java-Scala interaction and Jason Zaugg, who offers a free code review service for all of us contributors and has a fair number of impressive patches.  We have lots of users contributing testing fixes and patches, too many to name all of them here.  I hope to recognize you each individually over the coming quarters, so if your name was not listed, it&#39;s not because we don&#39;t appreciate you!  Everyone who has a closed pull request &lt;a rel=&quot;external&quot; href=&quot;https://github.com/scala/scala/pulls&quot;&gt;here&lt;/a&gt; deserves a beer! (Just ask me sometime and I&#39;ll provide).&lt;/p&gt;
&lt;h2 id=&quot;akka&quot;&gt;Akka&lt;/h2&gt;
&lt;p&gt;I was just going to cover the core Scala projects themselves, but my coworkers have nominated their picks for the Akka project as well!  With no further ado, the &quot;Swell Contributor&quot; awards go to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Derek Williams - For revamping Futures and Dataflow&lt;/li&gt;
&lt;li&gt;Martin Krasser - For the akka-camel module (with its amazing documentation).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;fin&quot;&gt;Fin&lt;/h2&gt;
&lt;p&gt;Kudos to all those selected!  I hope to one to be as awesome as you all, and keep up the good work.   If anyone has any nominations, feel free to send me an email for next quarter.   I&#39;ll try to choose around April (ScalaDays 2011 anyone?)&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Plus-colon extractor for collections.</title>
        <published>2011-12-28T00:00:00+00:00</published>
        <updated>2011-12-28T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/pluscolon-extractor/"/>
        <id>https://jsuereth.com/posts/pluscolon-extractor/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/pluscolon-extractor/">&lt;p&gt;I often find myself writing the following for helpful head-tail decomposition:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scala&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;collection&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; +:&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; unapply&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SeqLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;]](&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;      t&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; with&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SeqLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t.headOption map ( h &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt; (h, t.tail))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And then using it for head-tail decomposition like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; val x = Vector(1,2,3)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; x match {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     |   case x +: xs =&amp;gt; println(x); xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     | }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res1: scala.collection.immutable.Vector[Int] = Vector(2, 3)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;rather than the &#39;classical&#39; variant:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; x match {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     |   case Seq(x, xs @ _*) =&amp;gt; println(x); xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     | }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res2: Seq[Int] = Vector(2, 3)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I was curious if anyone else desired a parallelism to &lt;code&gt;+:&lt;/code&gt; in pattern matching that mimics what&#39;s available for &lt;code&gt;List&lt;/code&gt;s (&lt;code&gt;::&lt;/code&gt;).  If you&#39;re like me, feel free to borrow the code, but also speak up!  I&#39;ll find out about adding it into the standard library.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note: We could also have a matching &lt;code&gt;:+&lt;/code&gt; operator for &lt;code&gt;(x.init, x.last)&lt;/code&gt; decomposition as well.  (Only really efficient for IndexedSeq, I would guess).&lt;/em&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Scala Fresh is Alive</title>
        <published>2011-12-07T00:00:00+00:00</published>
        <updated>2011-12-07T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/scala-fresh/"/>
        <id>https://jsuereth.com/posts/scala-fresh/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/scala-fresh/">&lt;p&gt;It seems a lot of posts I&#39;ve meant to write for a &lt;em&gt;long&lt;/em&gt; time have been able to be used optimally in response to other blogs.&lt;/p&gt;
&lt;p&gt;Todays post is brought to you by &lt;a rel=&quot;external&quot; href=&quot;http://t.co/qjBQJomR&quot;&gt;David Pollak&#39;s fragility post&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;So let&#39;s talk about the problem of &quot;Binary Compatibility(TM)&quot; in Scala.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;First off, David raises a big issue in Scala.  One that most of us have seen.  Scala&#39;s new language features necessarily break binary compatibility of the bytecode.  Adding things like Specialization change Scala&#39;s standard library enough that you cannot use code compiled against older versions with the new version.   This is a two-edged sword.
The Scala experience should be as smooth as possible for all customers.
Things like java.util.Date shouldn&#39;t survive endlessly our standard library.
This requires a careful balance between breaking binary compatibility and advancing the language.   So far, I&#39;m pretty happy with the way this has been done, but would like to see things stabilize in the future.  Let&#39;s take a look at what&#39;s happened:&lt;/p&gt;
&lt;h3 id=&quot;scala-s-new-binary-compatible-releases&quot;&gt;Scala&#39;s new binary compatible releases&lt;/h3&gt;
&lt;p&gt;Scala 2.9.1 is binary compatible with Scala 2.9.0.   If you compile code against Scala 2.9.0, you can use it with the standard library from Scala 2.9.1.   This will hold for &lt;em&gt;all&lt;/em&gt; 2.9.x releases.   In Scala, binary compatibility will be at the bug-fix release.  All 2.10.x releases will be binary compatible with previous 2.10 releases.  This means that as long as your dependencies are compiling for a given Scala minor version, then you can continue to enjoy binary compatibility of libraries.&lt;/p&gt;
&lt;p&gt;This was made possible by the freely available &lt;a rel=&quot;external&quot; href=&quot;http://downloads.typesafe.com/migration-manager/0.1/migration-manager-0.1.jar&quot;&gt;Migration Manager tool&lt;/a&gt;.  A lot of us at typesafe use the publicly available version when developing to ensure binary compatibility of libraries.&lt;/p&gt;
&lt;p&gt;Note:  This was &lt;em&gt;not&lt;/em&gt; true for a lot of David Pollak&#39;s Scala experience, and is the result of many of us petitioning for better binary compatibility.  It&#39;s my opinion that this guarantee solves 50-80% of the problem.&lt;/p&gt;
&lt;h3 id=&quot;what-happened-to-scala-fresh&quot;&gt;What happened to Scala fresh?&lt;/h3&gt;
&lt;p&gt;I&#39;ve begun work on what can only be called &quot;Scala Fresh 2.0&quot;.  That is, a place where community libraries will be built and deployed against the latest version of Scala.&lt;/p&gt;
&lt;p&gt;This can serve two purposes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ensure that future versions of Scala do not break community libraries&lt;/li&gt;
&lt;li&gt;Ensure that the core libraries of Scala are available for every major version of Scala.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can find the project publicly available &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scala-cel&quot;&gt;here&lt;/a&gt;.  All of the work is public and will be migrate to the &quot;scala&quot; user on github once complete and ready for more contributions.  Feel free to contribute or offer suggestions.&lt;/p&gt;
&lt;p&gt;People may not realize but Scala Fresh failed because &lt;em&gt;I&lt;/em&gt; failed Scala Fresh.   I had very little time between a more than full time commitment at Google, &lt;a rel=&quot;external&quot; href=&quot;http://www.manning.com/suereth&quot;&gt;writing commitments&lt;/a&gt;, and kids.  This should not be an issue in the future, thanks to Typesafe taking binary compatibility seriously.  I now have a &lt;a rel=&quot;external&quot; href=&quot;https://github.com/jsuereth/scala-cel/blob/master/project/Build.scala&quot;&gt;prototype build&lt;/a&gt; that you can migrate your projects into.&lt;/p&gt;
&lt;p&gt;My coworker likens this idea to linux distribution repositories.  I think it&#39;s a decent way to think of it.  I, and others, are working hard to ensure the ecosystem for every major Scala release is easy to use and stable.   Migrating major Scala versions should be as difficult as finding deprecation warnings and removing them.&lt;/p&gt;
&lt;h3 id=&quot;sbt-cross-releasing&quot;&gt;SBT cross releasing&lt;/h3&gt;
&lt;p&gt;Mark Harrah, recognized that Scala remains generally &lt;em&gt;source&lt;/em&gt; compatible between major releases (2.8 to 2.9).   A &lt;em&gt;long&lt;/em&gt; time ago, Mark met me at a Panera in Anne Arundel, MD to discuss a mechanism of cross deploying libraries so that they were available for every release of Scala.   This was well before the 2.9.x binary compatibility days and is still in heavy use within the community.   It&#39;s been pretty widely adopted, but can only go so far.   It&#39;s a good stop-gap solution that we, the community, can improve on.&lt;/p&gt;
&lt;p&gt;Binary compatibility is a community effort.  I know Typesafe is doing what it can with its resources, and I&#39;m personally tackling as much as I can (probably trying to juggle too many balls).  However, if you want to help, please email me!&lt;/p&gt;
&lt;p&gt;The JVM is amazing, don&#39;t doubt its powers
Finally, I want to clarify a confusion that a lot of people have about binary compatibility.  That is, that traits are &lt;em&gt;HUGE ISSUES FOR BINARY COMPATIBILITY ZOMG!!!&lt;/em&gt;.   In actuality, traits are only slightly more dangerous than interfaces + implementation pairs.&lt;/p&gt;
&lt;p&gt;In the code below, the method colored in red was added in a bug-fix release.   It is &lt;em&gt;binary compatible&lt;/em&gt; with the previous version.&lt;/p&gt;
&lt;pre&gt;
trait Foo {
  def foo = &quot;foo&quot;
  &lt;span class=&quot;red&quot;&gt;*def bar = &quot;NEW BAR IS NEW BAR!&quot;*&lt;/span&gt;
}
&lt;/pre&gt;
&lt;p&gt;That&#39;s right, adding methods, even with implementation, does &lt;em&gt;NOT&lt;/em&gt; break linkage.  Think about it.  How did Java support JDBC 4.0 interfaces running JDBC 3.0 drivers?   The trick is that trait-linkage errors happen at &lt;em&gt;runtime&lt;/em&gt; when &lt;em&gt;calling&lt;/em&gt; a method that has no linked implementation.  This is the kind of magic that Java can get away with to ensure binary compatibility and it&#39;s just as useful in Scala.&lt;/p&gt;
&lt;p&gt;Now, there is an issue where you can break binary compatibility.   Again, red italics will denote new code&lt;/p&gt;
&lt;pre&gt;
trait Bar {
  def foo = {
     &lt;span class=&quot;red&quot;&gt;*bar +*&lt;/span&gt; &quot; foo&quot;
  }
  &lt;span class=&quot;red&quot;&gt;*def bar = &quot;NEW BAR IS NEW BAR!&quot;*&lt;/span&gt;
}
&lt;/pre&gt;
&lt;p&gt;NOW, the implementation of a previous method calls a new method.   While this new method has an implementation in the trait, compiled code against the trait does not.   This is akin to the following Java scenario:&lt;/p&gt;
&lt;pre&gt;
interface Foo {
  public String foo();
  &lt;span class=&quot;red&quot;&gt;*public String bar();*&lt;/span&gt;
}
abstract public class AbstractFoo implements Foo {
  @Override public String foo() { return &lt;span class=&quot;red&quot;&gt;*bar() + &quot;*&lt;/span&gt; foo&quot; }
}
&lt;/pre&gt;
&lt;p&gt;The abstract method implements one of the trait&#39;s methods, but not the new one.  However, the implementation is modified to call the new method.&lt;/p&gt;
&lt;p&gt;Scala&#39;s collections underwent a minor change in 2.10 to improve both class file size &lt;em&gt;and&lt;/em&gt; unintentionally binary compatibility.   That is, the collections now follow this pattern:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Traversable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //....&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;abstract&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AbstractTraversable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Traversable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Vector&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AbstractTraversable&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; with&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Traversale&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this scenario, if your collection extends an Abstract collection (rather than its own parent class), you can remain binary compatble &lt;em&gt;and allow&lt;/em&gt; new methods to be added to a trait &lt;em&gt;and be called&lt;/em&gt; be the implementation.   Vector is now &quot;binary resilient&quot; to changes in the Traversable trait.  There are no guarantees that a trait is not used directly, so we can&#39;t take advantage of this when fixing bugs in collections in the Scala library.   It&#39;s still a nice trick to use.&lt;/p&gt;
&lt;p&gt;You &lt;em&gt;do&lt;/em&gt; lose some flexibility doing this.   Specifically, you can&#39;t have multiple class parents unless they&#39;re linear (like Java).&lt;/p&gt;
&lt;p&gt;So, my point is that Binary Compatibility is a community issue.  The JVM and Scala do what they can. I hope to see the compiler do more in the future.   The Migration Manager tool from Typesafe can detect a lot of binary compatibility issues and has been crucial to ensuring 2.9.x Scala releases are binary compatible.&lt;/p&gt;
&lt;p&gt;I&#39;m trying to organize projects to help ensure &lt;em&gt;all&lt;/em&gt; libraries for Scala can be released against all the major versions and remain binary compatible.  The story is changing, and has done so rapidly over the past two years.&lt;/p&gt;
&lt;p&gt;I&#39;d love to see Lift, and others start adopting binary compatibility standards for there releases as well.   This is not just a scala-the-library or scala-the-langauge issue.   Libraries matter, just as they do in Java.&lt;/p&gt;
&lt;p&gt;So while David&#39;s post highlights an issue, my response is:&lt;/p&gt;
&lt;p&gt;Help us (the Scala community) out!  We&#39;re going there, we&#39;re doing that.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Macro vs. Micro Optimisation</title>
        <published>2011-11-29T00:00:00+00:00</published>
        <updated>2011-11-29T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/macro-vs-micro-optimisation/"/>
        <id>https://jsuereth.com/posts/macro-vs-micro-optimisation/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/macro-vs-micro-optimisation/">&lt;p&gt;So there&#39;s recently been a bit of hype about another Colebourne article: &lt;a rel=&quot;external&quot; href=&quot;http://blog.joda.org/2011/11/real-life-scala-feedback-from-yammer.html&quot;&gt;http://blog.joda.org/2011/11/real-life-scala-feedback-from-yammer.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&#39;d like to respond to a few points he makes.&lt;/p&gt;
&lt;h3 id=&quot;first&quot;&gt;First&lt;/h3&gt;
&lt;p&gt;You should evaluate Scala and pay attention to its benefits and flaws before adopting it.  Yes, there are flaws to Scala.   Working at typesafe makes you more aware of some of them.  We&#39;re actively working to reduce/minimize/get rid of these.   In my opinion, the negatives of using Scala are peanuts compared to the postives of choosing Scala over Java.  I think everyone should make up their own mind about this.   Not everyone is going to choose Scala.  I feel bad for those who don&#39;t, but I make no effort to convince you further than showing you the 40+ Open source Scala projects I have on github.  It&#39;s a language with a lot to like and a bit to dislike.&lt;/p&gt;
&lt;p&gt;Now, to the meat of what I want to say.   Don&#39;t get lost in micro optimization when discussing programming.&lt;/p&gt;
&lt;p&gt;The blog article discusses writing high-performance code in the critical path that has crazy performance needs.   This is not your every day development.   Scala loses a lot of benefits in this world, because features like closures have overhead on the JVM.  Hopefully when Java adopts closures, this overhead can be alleviated, but it is there right now.   The set of rules from the email is known to a lot of us Scala devs when writing a performance intensive section of code.&lt;/p&gt;
&lt;p&gt;I&#39;ll reiterate a few to agree with:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Avoid closure overhead.   If you can&#39;t get the JVM to optimize away and need to allocate a closure constantly in a tight loop, this can slow down a critical section.  This is correlated with: &lt;strong&gt;Don&#39;t use a for loop.  For expressions (and loops) in Scala are implemented as closures.   There are some efforts underway to &lt;em&gt;inline&lt;/em&gt; these closures as an optimization.  This performance hit isn&#39;t a permanent one.   As the compiler matures, you&#39;ll see a lot of optimization work happen.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;private[this]&lt;/code&gt; to avoid an additional method.   Scala generated methods and fields for val/var members.   Using &lt;code&gt;private[this]&lt;/code&gt; informs the compiler that it can optimize away the method.   Again, while hotspot can optimize this away if you&#39;re in a very critical section, it may be a good idea to optimize.   In fact, the whole promotion to fields and methods aspect of Scala classes deserves attention from anyone writing performance critical code.   The rules are also there in Java, it&#39;s just that you probably have people that already know them.&lt;/li&gt;
&lt;li&gt;Avoid invokevirtual calls.  (Coda lists this as avoid Scala&#39;s collections).  The true issue here is that invoke-virtual can make a difference in performance critical sections of code.  Again, this is one I think we can improve with a few final declarations and maybe an annotation or two.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here&#39;s the big &lt;em&gt;missing&lt;/em&gt; point in all that feedback.   This is for performance critical sections of code, not for general purpose applications.   I think when it comes to performance bottlenecks, you need to pull out the stops and optimize the heck out of your apps.   Look at the Akka framework from typesafe. Akka uses a lot of &quot;dirty&quot; scala code to achieve high non-blocking concurrent performance.  It&#39;s one of the most amazing libraries, and the code is pretty low level.   It also supports a very high level of abstraction when writing your application.   It uses PartialFunctions (which are sort of a combination of pattern matching and closures) and traits, both of which have some overhead.   However the resulting &lt;em&gt;application&lt;/em&gt; is fast.  Why?   The inner loops are fast and optimized and the application &lt;em&gt;architecture&lt;/em&gt; can be optimised.  In a high level language, you can take advantages of designs that you would &lt;em&gt;never&lt;/em&gt; execute in a lower-level language because the code would be unmaintainable.&lt;/p&gt;
&lt;p&gt;You see, most of us like to be able to read and understand what our code does.   Scala has some features where you can write very expressive code in a few lines.   After getting over the initial hump, this code is pretty easy to maintain.   If I were to write that same code in Java, it would look odd, confusing as hell, and no one would want to maintain it.&lt;/p&gt;
&lt;h3 id=&quot;i-learned-this-lesson-at-google&quot;&gt;I learned this lesson at Google.&lt;/h3&gt;
&lt;p&gt;Google has a pretty stringent C++/Java style guide.  One that is tuned for high performance servers.   The C++ style guide is public.  The style guide frowns on things like the use of &#39;smart&#39; pointers that &#39;garbage collect&#39; because you can&#39;t be sure that GC will happen at a critical moment in the code.&lt;/p&gt;
&lt;p&gt;Google also has a high performance Map-Reduce library.  This thing is pretty amazing, with all sorts of crazy cool features like joining data on the &lt;em&gt;map&lt;/em&gt; part of the map-reduce.  The library followed all the google coding conventions and is generally held up as a piece of awesome software, which its.&lt;/p&gt;
&lt;p&gt;However, writing applications with the Map-Reduce library was less than idea.   I could write pretty clean code with the MR library.  My Mappers were pretty light weight and minimalistic.  Same with my reducers.   You&#39;d then string together a series of map-reduces in this crazy guitar inspired &quot;patch it together&quot; configuration and the thing would be off to the races.  The downside is that the throughput of this kind of processes was &lt;em&gt;sub optimal&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;It wasn&#39;t anything inherent in the libraries.  The libraries were fast and good.  The APIs were optimised for high speed performance.  However, writing optimal &lt;em&gt;architectures&lt;/em&gt; in the framework was tough.   If I wanted to do any crazy performance features, like combining map functions and reduce functions in a single map reduce run, the code got ugly &lt;em&gt;fast&lt;/em&gt;.  Not only that, it was very difficult to maintain because of all the odd bookkeeping.  I have 10 outputs ordered by key, this is the one writing to that file right?  KRAP.&lt;/p&gt;
&lt;p&gt;I spent 6 months writing applications of this nature and feeling like there had to be something better.   I started on a venture to write something, and as usual found that someone else already had.  That&#39;s when I found out about Flume and met Craig Chambers.&lt;/p&gt;
&lt;p&gt;FlumeJava was a map reduce library that was gaining a lot of traction at Google, but I had heard a lot of complaints about its API.  Around the time I was looking at it, Flume C++ was coming into existence.  My team was one of the alpha users of the C++ variant.&lt;/p&gt;
&lt;p&gt;The C++ API was a thing of beauty.   Like its Java cousin, it treats data a set of Parallel Distributed Collections.   You can see Daniel and my talk on the Scala equivalent here.  Converting Mappers and Reducers into this API was pretty simple.  You could even do it directly by just annotating your Mappers and Reducers with types.&lt;/p&gt;
&lt;p&gt;My team saw a 30-40% reduction in code size for some of our map-reduce pipelines.  We had an 80% reduction in code for unit tests (which was by far the most amazing benefit).   Not only that, the Flume library optimizes the pipeline by performing all sorts of dirty map-reduce tricks for you.  In some cases we dropped a map-reduce call or two.  In the worst case, we had the same number we had started with.&lt;/p&gt;
&lt;p&gt;What was wrong with the library?  It violated &lt;em&gt;almost every&lt;/em&gt; style rule for C++ at google.  That&#39;s right, smart pointers, classes with inline member definitions the works.   I loved every second of it.  Why?  Because I was getting stuff done &lt;em&gt;faster&lt;/em&gt; than before with &lt;em&gt;less&lt;/em&gt; code and the pipelines ran &lt;em&gt;faster&lt;/em&gt;.   It was a crazy win.&lt;/p&gt;
&lt;p&gt;The startup time might not have been as optimal as it could be.  Flume ran an on-the-fly optimization before running your pipeline.  That was being improved all the time with neat tricks.   Things I didn&#39;t have to write to watch my app speed up, both runtime and startup time.&lt;/p&gt;
&lt;p&gt;The key here is that the designers of Flume weren&#39;t focused on micro optimization but &lt;em&gt;macro&lt;/em&gt; optimization.  The inner guts of the library used the very fast and efficient Map-Reduce library and the wrappers they had were as efficient as they could make them.   My code did not have to follow these rules, because the core loops were fast.  When it came down to it, my code used high level concepts.   Something akin to a closure and for-expressions in Scala (Note: The Scala equivalent &lt;em&gt;did&lt;/em&gt; use for-expressions and closures with no noticeable performance hit).&lt;/p&gt;
&lt;p&gt;There are times when writing Scala code requires care and optimization in the micro level.  However, don&#39;t lose the forest for the trees.  Think about the entire application architecture.  Scala will open up possibilities for writing code that you&#39;d never dream of trying to maintain in C++ or Java.  Take Akka as a shining example.&lt;/p&gt;
&lt;p&gt;And when you need the performance, listen to those techniques.   Viktor Klang can probably give you a &lt;em&gt;ton&lt;/em&gt; more.  I know I learn more every time I talk with him.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>SBT and Plugin design</title>
        <published>2011-09-16T00:00:00+00:00</published>
        <updated>2011-09-16T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/sbt-plugin-design/"/>
        <id>https://jsuereth.com/posts/sbt-plugin-design/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/sbt-plugin-design/">&lt;p&gt;Sbt 0.10 brings &lt;em&gt;a lot&lt;/em&gt; of power to the table.   SBT 0.10 switched from a class/inheritance based build system into a more functional approach.   For those who aren&#39;t familiar, here&#39;s the quick spiel on SBT.&lt;/p&gt;
&lt;h2 id=&quot;basics-of-sbt&quot;&gt;Basics of SBT&lt;/h2&gt;
&lt;p&gt;In SBT, a project is composed of &lt;code&gt;Setting[_]&lt;/code&gt; values.  A Setting is sort-of a name-value pair (more of a name &lt;em&gt;computation&lt;/em&gt; pair).  In the SBT command-line you can type the name of a setting and get its value (or computed value).   For example, test is a task in SBT that you can type in the command line.   The setting&#39;s computation is executed and the value returned.   This setting may depend on other settings for its value.&lt;/p&gt;
&lt;p&gt;SBT provides a simple way to construct a project.   In the root directory, any &lt;code&gt;*.sbt&lt;/code&gt; file is compiled to &lt;code&gt;Setting[_]&lt;/code&gt; values.   A &lt;code&gt;Setting[_]&lt;/code&gt; is two things:   A name (&lt;code&gt;Key&lt;/code&gt; + &lt;code&gt;Scope&lt;/code&gt;) and a Value (or computation, called &lt;code&gt;Initialize&lt;/code&gt; in SBT).  One can construct a &lt;code&gt;Setting[_]&lt;/code&gt; via the SBT dsl:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sourceDirectory in Compile &amp;lt;&amp;lt;= baseDirectory apply { dir =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  dir / &amp;quot;src&amp;quot; / &amp;quot;main&amp;quot; / &amp;quot;scala&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, the &lt;code&gt;sourceDirectory&lt;/code&gt; Key (name) is assigned an Initailzation (value/computation).  The &lt;code&gt;&amp;lt;&amp;lt;=&lt;/code&gt; operator is used to construct a &lt;code&gt;Setting[_]&lt;/code&gt; by joining a Key and an Initialiation.  In the above example, the Initialization is constructed to pull the current value of the baseDirectory key and modify it for the value of the sourceDirectory key.&lt;/p&gt;
&lt;p&gt;Note: The &lt;code&gt;apply&lt;/code&gt; method is used on &lt;code&gt;baseDirectory&lt;/code&gt; because both &lt;code&gt;baseDirectory&lt;/code&gt; and &lt;code&gt;sourceDirectory&lt;/code&gt; are &lt;code&gt;SettingKey[_]&lt;/code&gt;s.  SBT distinguishes between three types of &lt;code&gt;Setting[_]&lt;/code&gt; values:  &lt;code&gt;Setting&lt;/code&gt;, &lt;code&gt;Task&lt;/code&gt; and &lt;code&gt;InputTask&lt;/code&gt; with corresponding &lt;code&gt;SettingKey&lt;/code&gt;, &lt;code&gt;TaskKey&lt;/code&gt; and &lt;code&gt;InputKey&lt;/code&gt; &quot;name&quot; types.  The three are distinguished as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SettingKey&lt;/code&gt; - Something that is computed &lt;em&gt;once&lt;/em&gt; on project load (or reload). like a val.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;TaskKey&lt;/code&gt; - Something that is computed each time it is called, like a def.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;InputKey&lt;/code&gt; - Something that takes user input to perform its task.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;configurations&quot;&gt;Configurations&lt;/h2&gt;
&lt;p&gt;SBT uses a configuration matrix to define the same task against different configurations.   For example, SBT defines a task for compiling Scala code called compile.  This mechanism has a bunch of required settings.   However, it wouldn&#39;t be DRY to repeat all these settings for compiling &lt;em&gt;test&lt;/em&gt; code as well.  So instead, SBT defines the &lt;em&gt;same&lt;/em&gt; settings in two different configurations, on called Test and another called Compile.  To compile just tests in SBT, you can prefix a task with its configuration, e.g. &lt;code&gt;test:compile&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;plugin-design&quot;&gt;Plugin Design&lt;/h2&gt;
&lt;p&gt;So what does this have to do with plugin design?  SBT plugins need to integrate &lt;code&gt;Setting[_]&lt;/code&gt; values into a build without conflicting with SBT default settings and other plugins.  To complicate matters, SBT imports all the members of plugin classes into scope of a project using a wildcard import.   This means all the plugins you use could have conflicting names that step on each other.   Combined with potentially conflicting key names, plugins need to be very careful with how they define things.&lt;/p&gt;
&lt;p&gt;Having worked on several plugins recently, I&#39;d like to outline a strategy that I think achieves a certain elegance in definition and usage, as well as the safety one wants from a plugin.&lt;/p&gt;
&lt;p&gt;The basic pattern is as follows.   Define an object with the name you want for your plugin &lt;em&gt;inside&lt;/em&gt; the Plugin class.   For example, if I want a &lt;code&gt;xsbt-suereth-plugin&lt;/code&gt;, I would define the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import sbt._&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import Keys._&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;object SbtSuerethPlugin extends Plugin {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  object suereth {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     // Your code here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Inside of the suereth object I hide all my definitions and code.   This isolates my plugin from other sbt plugins, as long as no one names their methods &quot;suereth&quot;.&lt;/p&gt;
&lt;p&gt;Next, let&#39;s define a new Config object that we can use to protect our keys from other plugins.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;object suereth {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  val Config = config(&amp;quot;suereth&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  // Your settings here&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The Config also shares the name of the plugin, so in the command line tasks and settings can be run using &lt;code&gt;suereth:&amp;lt;your-task-here&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The next step is define whatever custom keys your plugin will use.  Let&#39;s create a blog key.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;object suereth {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   val blog = SettingKey[String](&amp;quot;blog&amp;quot;, &amp;quot;location of the blag&amp;quot;) in Config&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The key is automatically placed into the suereth configuration using the in method.  This has two benefits:&lt;/p&gt;
&lt;p&gt;When defining the Initialization for a &lt;code&gt;Setting[_]&lt;/code&gt;, there&#39;s no need to continue writing &lt;code&gt;blog in Config&lt;/code&gt;.
Users of the plugin can directly access &lt;code&gt;suereth.blog&lt;/code&gt; without needing to specify &lt;code&gt;suereth.blog in suereth.Config&lt;/code&gt;.
Note:  You can also reference SBT keys in your configuration by writing: &lt;code&gt;val sources = Keys.sources in Config&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we can provide default values/computations for tasks and settings in our plugin.   By convention, calling these settings is a good idea.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;object suereth {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   lazy val settings: Seq[Setting[_]] = Seq(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      blog := &amp;quot;http://suereth.blogspot.com&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how the keys are access directly but are actually in the appropriate config matrix.   This helps defining your plugin source code, but will also help users of your plugin.   Let&#39;s look at what a build.sbt file will be for this plugin.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;seq(suereth.settings:_*)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;suereth.blog := &amp;quot;http://blog.typesafe.com&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice how the settings for this plugin are completely namespaced by the &lt;code&gt;suereth&lt;/code&gt; object.   We&#39;ve tied the concept of a &quot;configuration&quot;  axis for keys with accessing values in an object.&lt;/p&gt;
&lt;p&gt;I find this mechanism of defining plugins both helpful from a development perspective and a user perspective.   Curious to hear what others think.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Leaving Google for Typesafe</title>
        <published>2011-07-22T00:00:00+00:00</published>
        <updated>2011-07-22T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/leaving-google/"/>
        <id>https://jsuereth.com/posts/leaving-google/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/leaving-google/">&lt;p&gt;So, as some of you may have found out already, I&#39;ll be leaving Google effective July 22nd and moving to a new role at &lt;a rel=&quot;external&quot; href=&quot;http://www.typesafe.com/&quot;&gt;Typesafe&lt;/a&gt;. Some of you may be wondering why, so I&#39;m offering my reasons in a blog post for the curious:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Scala has been gaining lots of traction in the market space recently. I think Scala is and has been reaching its tipping point. I&#39;m hoping to contribute everything I can to sure it remains a viable language for everyday development.&lt;/li&gt;
&lt;li&gt;While Google does have 20% time to work on individual projects, this correlated to about a day a week to work on all things Scala within Google. Rare was the opportunity to do anything significant or to contribute back to the community, due to time constraints. While I was able to accomplish one &lt;a rel=&quot;external&quot; href=&quot;http://twitter.com/#!/odersky/status/76713658922696704&quot;&gt;pretty&lt;/a&gt; &lt;a rel=&quot;external&quot; href=&quot;http://days2011.scala-lang.org/node/251&quot;&gt;good&lt;/a&gt; &lt;a rel=&quot;external&quot; href=&quot;http://days2011.scala-lang.org/node/138/282&quot;&gt;thing&lt;/a&gt;, aligning my work with my passions seemed like the best thing to do.&lt;/li&gt;
&lt;li&gt;It&#39;s part of a new weight loss program called &quot;pay for your own food&quot;. This was tempered by my wife&#39;s purchase of a new grill for my birthday, which has seen heavy use in the past few weeks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, while I see Scala&#39;s future as bright and rosy and I can&#39;t explain how excited I am to start at typesafe, I&#39;d also like to do a little reflection on Google and some of what makes it a great company.&lt;/p&gt;
&lt;h3 id=&quot;google-cares-about-employees&quot;&gt;Google cares about employees&lt;/h3&gt;
&lt;p&gt;This is not a lie. Google, as best as it can, tries to mean what it says. At my previous companies you&#39;d hear &quot;I&#39;d love to do this, but my hands are tied&quot;. At Google, you often hear &quot;I don&#39;t know, but let&#39;s ask someone who does&quot; which usually turns into &quot;Yes, go ahead&quot;. It was rare that something I asked for was denied. My &lt;em&gt;perception&lt;/em&gt; of what was acceptible is an entirely different thing. At Google, there are people who think what you think (at over 10k engineers, it&#39;s guaranteed one will probably agree with you about something), and that engineer might have also tried to accomplish the same task as you. Go ask about it and find like-minded individuals. You&#39;ll be surprised how much&lt;/p&gt;
&lt;h3 id=&quot;google-tries-to-do-what-they-say&quot;&gt;Google tries to do what they say.&lt;/h3&gt;
&lt;p&gt;The other side to this coin is that the &lt;em&gt;corporate entity&lt;/em&gt; and by that I mean the higher ups like to do things for all Google employees. Like bonuses. They really do happen, and they really do try to treat you like a human being.&lt;/p&gt;
&lt;p&gt;The other side to this coin is the interview process. I&#39;ve seen so many negative posting about the interview process. Well, I&#39;m here to dispell a few of those. Google&#39;s interviews will challenge your technical abilities. If this annoys you and you don&#39;t take the job, then good. You&#39;re probably not the type to herd cats deal with politics/relationships involves with a large developer base. If you can&#39;t cut the interview, then it gives you a reason to go back and practice. However, more importantly, Google would rather turn away a good candidate than hire a bad one. This mindset is very impressive. It really makes a huge difference on the ability of teams to accomplish things. I knew every employee I worked with at google was reliable to get work done, not something that&#39;s always the case externally.&lt;/p&gt;
&lt;p&gt;In the future I know my interview style will change. No more will interviewees be allowed to just talk about experiences without showing some code. It&#39;s amazing some of the depths you can learn here. It&#39;s even great when an interviewee fails to answer the question correctly because you &lt;em&gt;learn their thought process&lt;/em&gt;. I know for me, a few candidates who struggled were the ones I wanted sitting beside me coding, more so than the ones who blazed through a question but had a more &#39;better than thou&#39; aura.
So, the point here is care about your employees and care about who you hire. Your company will be in far better shape if you do this.&lt;/p&gt;
&lt;h3 id=&quot;culture-is-faster-than-process&quot;&gt;Culture is faster than process&lt;/h3&gt;
&lt;p&gt;Google tries to instill a culture of &#39;doing the right thing&#39; its employees, rather than outlining software process to a T. There are a few &#39;inconveniences&#39; that exist, but other than mandatory code reviews, a lot of the process is up to the team to do what&#39;s best. The other side to this coin is the corporate culture tries to help define and change what&#39;s best. It&#39;s amazing how one executive making a statement in an all hands meeting can suddenly alter the perceived &quot;best way to code&quot; and get the company to move. It&#39;s also surprisingly hard to change culture once it has been really embedded into the engineers, which is the danger. A sense of &#39;right&#39; in the ways of writing software can be beneficial, but can also turn into anti-patterns if not tempered. I&#39;d love to go into more details here, just feel free to bug me.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Generic Quicksort in Scala</title>
        <published>2011-06-16T00:00:00+00:00</published>
        <updated>2011-06-16T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/generic-quicksort/"/>
        <id>https://jsuereth.com/posts/generic-quicksort/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/generic-quicksort/">&lt;p&gt;So, I decided to create a quicksort algorithm in Scala that showcases how to write &#39;generic&#39; collection methods. That is, how can we write an external method that works across many types of collections &lt;em&gt;and&lt;/em&gt; preserves the final type.&lt;/p&gt;
&lt;p&gt;Well, here&#39;s how you do it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scala&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;collection&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;SeqLike&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scala&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;collection&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;generic&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;CanBuildFrom&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; scala&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;math&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Ordering&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; QuickSort&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; sort&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;)(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;implicit&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; ev0&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SeqLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;                             cbf&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; CanBuildFrom&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;                             n&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Ordering&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;    import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (a.length &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;      // We pick the first value for the pivot.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; pivot&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; a.head&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;lower&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;tmp&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; a.partition(_ &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; pivot)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;upper&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;same&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; tmp.partition(_ &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; pivot)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; cbf()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      b.sizeHint(a.length)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      b &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;++=&lt;/span&gt;&lt;span&gt; sort[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;](lower)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      b &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;++=&lt;/span&gt;&lt;span&gt; same&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      b &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;++=&lt;/span&gt;&lt;span&gt; sort[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;](upper)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      b.result&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&#39;ve chosen a somewhat imperative approach to the problem. The quick sort algorithm is split into two parts: The first checks for small collections and returns them, the second picks a pivot and decomposes the collection into three pieces. Theses pieces are sorted (if necessary) and pushed into a builder, cleverly named &quot;&lt;code&gt;b&lt;/code&gt;&quot;. This &quot;&lt;code&gt;b&lt;/code&gt;&quot; is given a hint to expect the entire collection to eventually wind up in the built collection (hopefully this helps performance). Finally, after passing the three partitions to the builder, the result is returned.&lt;/p&gt;
&lt;p&gt;The magic here is in the rather confusing type signature:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; sort&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;a&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span&gt;)(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;implicit&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; ev0&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SeqLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;                             cbf&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; CanBuildFrom&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Coll&lt;/span&gt;&lt;span&gt;],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;                             n&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Ordering&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Coll&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&#39;s decompose this a bit. &lt;code&gt;T&lt;/code&gt; is the type parameter representing elements of the collection. &lt;code&gt;T&lt;/code&gt; is required to have an &lt;code&gt;Ordering&lt;/code&gt; in this method (the &lt;code&gt;implicit n: Ordering[T]&lt;/code&gt; parameter in the second parameter list). The ordering members are imported on the first line of the method. This allows the &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt; operations to be &#39;pimped&#39; onto the type &lt;code&gt;T&lt;/code&gt; for convenience.&lt;/p&gt;
&lt;p&gt;The second type parameter is &lt;code&gt;Coll&lt;/code&gt;. This is the concrete Collection type. Notice that &lt;em&gt;no type bounds are defined&lt;/em&gt;. It&#39;s a common habit for folks new to Scala to define generic collection parameters as follows: &lt;code&gt;Col[T] &amp;lt;: Seq[T]&lt;/code&gt;. &lt;strong&gt;Don&#39;t&lt;/strong&gt;. This type does not quite mean what you want. Instead of allowing any subtype of sequence, it only allows subtypes of sequence that &lt;em&gt;also&lt;/em&gt; have type parameters (which of course, is most collections). Where you can run into issues is if your collection has (a) no type parameters or (b) more than one type parameter. For example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Foo&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; DatabaseResultSetWalker&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DbType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Seq&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both of these will fail type checkking when trying to pass them into a method taking &lt;code&gt;Col[T] &amp;gt;: Seq[T]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To get the compiler to infer the type parameter on the lower bound, we have to defer the type inferencer long enough for it to figure this out. To do that, we don&#39;t enforce the type constrait until implicit lookup using the &lt;code&gt;&amp;lt;:&amp;lt;&lt;/code&gt; class.&lt;/p&gt;
&lt;p&gt;The type parameter: &lt;code&gt;Coll &amp;lt;:&amp;lt; SeqLike[T, Coll] &lt;/code&gt;
Ensures that the type &lt;code&gt;Coll&lt;/code&gt; is a valid &lt;code&gt;Seq[T]&lt;/code&gt;. You may be asking why this signature uses &lt;code&gt;SeqLike&lt;/code&gt; rather than &lt;code&gt;Seq&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GOOD QUESTION!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;SeqLike differs from Seq in that it &lt;em&gt;retains the most specific type of the sequence&lt;/em&gt;. This one of the magic tricks behind Scala&#39;s collections always returning the most specific type known. That type is embedded in &lt;code&gt;SeqLike&lt;/code&gt;. To ensure that we can return the most sepcific type, we can Capture &lt;code&gt;Coll&lt;/code&gt; as a &lt;code&gt;SeqLike with Coll&lt;/code&gt; as the specific type. This means that &lt;code&gt;filter&lt;/code&gt;s, &lt;code&gt;map&lt;/code&gt;s, &lt;code&gt;flatMap&lt;/code&gt;s, &lt;code&gt;partition&lt;/code&gt;s should all try to preserve the type &lt;code&gt;Coll&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The last implicit parameter is the &lt;code&gt;cbf&lt;/code&gt; &lt;code&gt;CanBuildFrom&lt;/code&gt;. Because we don&#39;t know how to construct instances of type &lt;code&gt;Coll&lt;/code&gt; (because we don&#39;t know the type &lt;code&gt;Coll&lt;/code&gt; at all), we need to implicitly receive evidence for how to construct a new Coll with sorted data.&lt;/p&gt;
&lt;p&gt;Let&#39;s look at the result:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; QuickSort.sort(Vector(56,1,1,8,9,10,4,5,6,7,8))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res0: scala.collection.immutable.Vector[Int] = Vector(1, 1, 4, 5, 6, 7, 8, 8, 9, 10, 56)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; QuickSort.sort(collection.mutable.ArrayBuffer(56,1,1,8,9,10,4,5,6,7,8))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 4, 5, 6, 7, 8, 8, 9, 10, 56)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; QuickSort.sort(List(56,1,1,8,9,10,4,5,6,7,8))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res18: List[Int] = List(1, 1, 4, 5, 6, 7, 8, 8, 9, 10, 56)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; QuickSort.sort(Seq(56,1,1,8,9,10,4,5,6,7,8))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res: Seq[Int] = List(1, 1, 4, 5, 6, 7, 8, 8, 9, 10, 56)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You may be asking why I&#39;ve chosen &lt;code&gt;Seq&lt;/code&gt; instead of &lt;code&gt;GenSeq&lt;/code&gt; or &lt;code&gt;GenTraversable&lt;/code&gt; or even &lt;code&gt;GenIterable&lt;/code&gt;. No particular reason, besides I wanted a reasonable assurance that the collection author expects the &lt;code&gt;.length&lt;/code&gt; and indexed access methods to be called.&lt;/p&gt;
&lt;p&gt;So, what are the lessons to be learned here?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;*Like&lt;/code&gt; subclasses to preserve the specific collection type&lt;/li&gt;
&lt;li&gt;Defer inference using &lt;code&gt;&amp;lt;:&amp;lt;&lt;/code&gt; to give the type checker a hope of succeeding&lt;/li&gt;
&lt;li&gt;Provide &lt;code&gt;@usecase&lt;/code&gt; comments for scaladoc so users won&#39;t get distracted by typesafe details.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;At lest, IMHO, this is the current way of creating generic collection code.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Scalatypes Podcast</title>
        <published>2011-06-12T00:00:00+00:00</published>
        <updated>2011-06-12T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/scalatypes-podcast/"/>
        <id>https://jsuereth.com/posts/scalatypes-podcast/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/scalatypes-podcast/">&lt;p&gt;Well, Daniel, Yuvi and I started podcasting interviews and discussions on Scala. Here&#39;s the first interview with Paul Phillips. We have a &lt;em&gt;lot&lt;/em&gt; more content to publish, but mastering audio still takes a backseat to day jobs and writing Scala in Depth. Let us know what you think!&lt;/p&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://player.wizzard.tv/player/o/j/x/130785390929/config/k-ced60fc1953fa08d/uuid/root/height/360/width/640/episode/k-24dbe4d7163afc87.m4v&quot;&gt;&lt;/script&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Parallel Distributed Collections API</title>
        <published>2011-06-03T00:00:00+00:00</published>
        <updated>2011-06-03T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/parallel-distributed-collections/"/>
        <id>https://jsuereth.com/posts/parallel-distributed-collections/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/parallel-distributed-collections/">&lt;p&gt;Here&#39;s my ScalaDays 2011 talk that I gave with Daniel Mahler.&lt;/p&gt;
&lt;iframe src=&quot;https://docs.google.com/present/embed?id=dfqn4jb_110dngxgpg3&quot; frameborder=&quot;0&quot; width=&quot;410&quot; height=&quot;342&quot;&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Annotate your type classes</title>
        <published>2011-03-15T00:00:00+00:00</published>
        <updated>2011-03-15T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/annotate-your-type-classes/"/>
        <id>https://jsuereth.com/posts/annotate-your-type-classes/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/annotate-your-type-classes/">&lt;p&gt;This is a real quick post saying that as of 2.8.1, everyone should be annotating their type class traits for better error messages. Obviously, those of us who are supporting 2.8.0 or earlier are left out.&lt;/p&gt;
&lt;p&gt;Here&#39;s an example REPL session:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Welcome to Scala version 2.9.0.r24384-b20110305105029 (OpenJDK 64-Bit Server VM, Java 1.6.0_20).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Type in expressions to have them evaluated.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Type :help for more information.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; import annotation.implicitNotFound&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import annotation.implicitNotFound&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; @implicitNotFound(msg = &amp;quot;Cannot find Serializable type class for ${T}&amp;quot;) trait Serializable[T]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;defined trait Serializable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; def foo[X : Serializable](x : X) = x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;foo: [X](x: X)(implicit evidence$1: Serializable[X])X&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; foo(5)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;:11: error: Cannot find Serializable type class for Int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       foo(5)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          ^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The annotation has very little documentation, but apparently supports &lt;code&gt;${TypeName}&lt;/code&gt; templates to inject the names of types the compiler is looking for. I believe you can also use this implicit on types with no type parameters.&lt;/p&gt;
&lt;p&gt;Happy hacking everyone!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>NEScala 2011 - Implicits without the import tax</title>
        <published>2011-02-18T00:00:00+00:00</published>
        <updated>2011-02-18T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/2011-implicits-without-tax/"/>
        <id>https://jsuereth.com/posts/2011-implicits-without-tax/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/2011-implicits-without-tax/">&lt;p&gt;Here&#39;s the slides for todays talk for those who wish to follow along on their laptops.&lt;/p&gt;
&lt;iframe src=&quot;https://docs.google.com/present/embed?id=dfqn4jb_106hq4mvbd8&amp;size=m&quot; frameborder=&quot;0&quot; width=&quot;555&quot; height=&quot;451&quot;&gt;&lt;/iframe&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>On Macro-Composition vs. Micro-Composition and Social software development.</title>
        <published>2010-10-15T00:00:00+00:00</published>
        <updated>2010-10-15T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/macro-vs-micro-composition/"/>
        <id>https://jsuereth.com/posts/macro-vs-micro-composition/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/macro-vs-micro-composition/">&lt;p&gt;I was talking to a co-worker today about using C++ templates to compose functionality in objects. I also mentioned how you can encode type traits using templates. My Coworker, conveniently aliased as &quot;Bob&quot;, asked why you would want to use type traits rather than defining an interface directly and wrapper objects.&lt;/p&gt;
&lt;p&gt;In response, I gave rather lame answers, which I intend to amend here. The basic gist is that I feel a lot of Object Oriented languages don&#39;t pay enough attention to macro composition of components in a system.&lt;/p&gt;
&lt;h3 id=&quot;first&quot;&gt;First&lt;/h3&gt;
&lt;p&gt;I&#39;d like to state that In a lot of my code, there are a set of classes that define the format of data, or the &quot;data layer&quot;. This layer has a certain &quot;mind-share&quot; around it. It is &quot;owned&quot; be a group of individuals and the portion of code they own. This data is passed around to other teams of people.&lt;/p&gt;
&lt;p&gt;Note that I specifically state &quot;other teams&quot; and not &quot;other components&quot;. In a lot of software shops, we think of software being comprised of sub-components of a system, but I tend to find that the sub-components of a system are also defined by the teams that work on them. If a particular team works on several sub-components, these components tend to be cohesive and reuse a lot of code. The boundaries between these teams are important to pay attention to.&lt;/p&gt;
&lt;p&gt;It&#39;s the boundaries where we tend to do a lot of conversions from one data definition to another. Each team wants to wrap its own &quot;needs&quot; around the ontology of classes. So in an OO system, this usually means wrapping data from one set of classes to another, or bunch of external methods that manipulate the data from another team. Because of this natural bent in programming, it&#39;s sometimes necessary to be able to adapt other people&#39;s specific classes into your own ontology. This is where type classes come in.&lt;/p&gt;
&lt;p&gt;I&#39;ve blogged about type classes/type traits before, so I won&#39;t bore you again. The basic gist of the idea is to define an interface for your method. For example, I&#39;m writing something that works with tree-like data structures where each parent node could have multiple children. I define a type trait as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TreeLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NodeType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; isParent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; NodeType&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Boolean&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; children&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; NodeType&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Iterator&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NodeType&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows me to take any &quot;node&quot; type and traverse it. Now, Let&#39;s write an algorithm where we want to see if a condition holds true down one &quot;branch&quot; of the tree to a leaf for every Node of this path. We can use our TreeLike type trait.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;final&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; hasValidDepthPath&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TreeLike&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;validator&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Boolean&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Boolean&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  if&lt;/span&gt;&lt;span&gt;(validator(node))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; helper&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; implicitly[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;TreeLike&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    !&lt;/span&gt;&lt;span&gt;helper.isParent(node)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; ||&lt;/span&gt;&lt;span&gt; helper.children(node).exists(hasValidDepth(_, validator))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; false&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The hasValidDepthPath method uses the TreeLike type trait rather than a different class. This lets us use non-treelike structures as trees. For example, we can turn a list into a tree where each branch is the same list but missing one item of the index.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; def dropIndex(x : List[Int], idx : Int) = x.zipWithIndex.filter(_._1 != idx).map(_._2) &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dropIndex: (x: List[Int],idx: Int)List[Int]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; dropIndex((1 to 10).toList, 2)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res2: List[Int] = List(0, 2, 3, 4, 5, 6, 7, 8, 9)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;implicit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; val&lt;/span&gt;&lt;span&gt; decomposeList[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; isParent&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; node.length &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; children&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;/span&gt;&lt;span&gt; to node.length).map(dropIndex(node, _))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can use a List of items as an input to our hasValidDepthPath method.&lt;/p&gt;
&lt;p&gt;Now to my coworkers question: Why would I want to add this extra layer?&lt;/p&gt;
&lt;p&gt;My answer is that it helps when working with &quot;ontologies&quot; from different teams. That is, each team has their own way of describing data. This mechanism allows our functions to mostly ubiquitously deal with these differing items, writing adapters for each ontology. This makes more sense when your algorithms deal with many different input types. In fact, I think it makes the most sense for &#39;peripheral&#39; code. That is code that interacts with other teams.&lt;/p&gt;
&lt;h3 id=&quot;now-for-the-second-part-of-the-rant&quot;&gt;Now, for the second part of the rant.&lt;/h3&gt;
&lt;p&gt;I feel a lot of languages don&#39;t support macrocomposition well. A great example of this is the existence of the Spring Framework. Spring allows you to define interfaces, objects and their dependent components. You can then build configurations where you create specific objects, bind them to names and inject them into other objects. You are essentially defining the components of your system at a micro-&amp;gt;macro level. Spring configuration allows you to focus on building the various components as needed at an object-by-object level until you are configuring objects that comprise major subcomponents of your system. Google Guice allows you do to a similar thing in Java using annotations.&lt;/p&gt;
&lt;p&gt;The important thing here, IMO is that there is a distinction between defining components/classes and composing them. We define a component that uses others via some interface in one section, and we combine these components together in another.&lt;/p&gt;
&lt;p&gt;In C++ we can also accomplish this to some extent using type traits. In C++ type traits usually take the form as templated structures. Composition is then done using template arguments. The best example of this is the STL and its usage of traits for allocation, comparison, hashing, etc.&lt;/p&gt;
&lt;p&gt;In C++, we could define a service as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Logger&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Threading&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Service&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;   typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Threading&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;ScopeLock&lt;/span&gt;&lt;span&gt; ScopeLock&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;   void&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; MyMethod&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;     Logger&lt;/span&gt;&lt;span&gt; log&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;     Threading&lt;/span&gt;&lt;span&gt; threading&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;       ScopeLock&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; scoped&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;mutex_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       log&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;OMG!!!&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     }&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; protected&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;   typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Threading&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Mutex&lt;/span&gt;&lt;span&gt; mutex_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This service is really really simple, but shows how to compose with type traits. The Threading interface is any class that defines a Mutex typedef (for mutexes), a ScopeLock typedef (for locking mutexes within a scope). This class could be reused across various threading libraries, by providing something with the correct type definitions. I would expect to see, somewhere else in code, the following &quot;Compositional&quot; Code:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;// This code replaces a Spring XML configuration.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;typedef&lt;/span&gt;&lt;span&gt; Service&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;PthreadThreading&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; DefaultLogging&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; MyService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;typedef&lt;/span&gt;&lt;span&gt; SomeHigherLevelService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;....&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; MyService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; MyHigherLevelService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The nice aspect here is that I can use typedefs to compose &quot;micro&quot; components of my system and aggregate those into &quot;macro&quot; components. It&#39;s the same technique used for both.&lt;/p&gt;
&lt;p&gt;In Scala, I&#39;ve used the following tactic: Define a trait which implies composition. This method has a lot of boilerplate, so bear with me.&lt;/p&gt;
&lt;p&gt;I want to define a Logger service that traits can use. To do this I define the generic Logger trait and a generic HasLogger trait.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SimpleLogger&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; info&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;msg&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; warn&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;msg&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; fatal&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;msg&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; String&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; HasSimpleLogger&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; logger&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SimpleLogger&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This SimpleLogger provides no advanced features, just three levels of logging. The HasSimpleLogger trait defined an abstract value for the Logger. It does not instantiate the logger, it merely denotes that a class will have one (i.e. needs to be composed with one). Later, we will define some service:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MyService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; HasSimpleLogger&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; serviceMethod&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    logger.info(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;MyService: Someone called my method&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is how we define each &quot;abstract&quot; component. We extend each composition trait (i.e. HasFoo) and then utilize the components as necessary. Again, there will be code somewhere that composes these components together. In Scala, the mechanism of composition is the trait &lt;em&gt;not&lt;/em&gt; a class instance:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MyComposedService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MyService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; with&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; HasDefaultLoggerImplementation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; HasMyComposedService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; HasMyService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; mySerivce&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MyComposedService&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why do we have to compose in this manner? Because we&#39;d like to have more control when composing large systems together. This mechanism allows us to &quot;reconfigure&quot; a MyComposedService with additional behavior or alternative subcomponents without having to redeclare &lt;em&gt;all&lt;/em&gt; the subcomponents.&lt;/p&gt;
&lt;p&gt;The downside to this approach is that it is full of boilerplate. However, notice again the distinction between defining components and aggregating behavior into our final system.&lt;/p&gt;
&lt;p&gt;I&#39;m of the opinion that these two functions are key to creating reusable software and they need to be simple and elegant to be used. I&#39;ve yet to see a large system where I felt the composability ever reached the ideals touted by J2EE of old. In fact, I feel EJBs are horrible for microcomposition. The Spring framework further proved this by provided a microcomposition layer below Session beans that became quite popular.&lt;/p&gt;
&lt;p&gt;In any case, these are my ramblings and musings on development. I&#39;d love to hear back from you. I&#39;m of the opinion that after becoming a proficient software developer, the solutions I write solve three issues:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A complex new algorithm to solve a problem &quot;never before solved&quot; (at least by the company I work for), or some foundational library.&lt;/li&gt;
&lt;li&gt;Munging data between social components of software, or differing ontologies&lt;/li&gt;
&lt;li&gt;Composing components to form a complex system.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I feel a lot of languages focus on (1). The languages I tend to prefer allow me to do (2) and (3) with ease.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>What&#39;s the state of Jigsaw</title>
        <published>2010-08-17T00:00:00+00:00</published>
        <updated>2010-08-17T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/state-of-jigsaw/"/>
        <id>https://jsuereth.com/posts/state-of-jigsaw/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/state-of-jigsaw/">&lt;p&gt;I hadn&#39;t been paying close attention, but recently a &lt;a rel=&quot;external&quot; href=&quot;http://mail.openjdk.java.net/pipermail/jigsaw-dev/2010-August/001105.html&quot;&gt;few emails questioning the future of the jigsaw project&lt;/a&gt; have been sent to the jigsaw-dev mailing list. The last check-in posted to the mailing list was on June 8th, and was the tail-end of a flurry of check-ins from the jigsaw team.&lt;/p&gt;
&lt;p&gt;The whole thing is rather curious, but without any specific word from Oracle, it&#39;s all speculation. I have my own guesses, but it&#39;s probably best I keep those to myself for now.&lt;/p&gt;
&lt;h3 id=&quot;update&quot;&gt;Update&lt;/h3&gt;
&lt;p&gt;It appears that after a few months of inactivity, whatever decisions at Oracle needed to be made were made.   Jigsaw appears to be under active public deveopment again.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Monkey Patching, Duck Typing and Type Classes</title>
        <published>2010-07-13T00:00:00+00:00</published>
        <updated>2010-07-13T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/monkey-patching-scala/"/>
        <id>https://jsuereth.com/posts/monkey-patching-scala/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/monkey-patching-scala/">&lt;p&gt;Somewhat recently, there have been a bunch of articles written about Scala and how you can create something that looks like Haskell&#39;s type class feature. I&#39;m not going to duplicate these posts, but I am going to go beyond just the mechanics of creating a type class and discuss how to utilise some of Scala&#39;s advanced features with type classes to create some pretty robust code. A lot of the post borrows ideas from my upcoming book, Scala In Depth. The examples come from the Scala ARM library.&lt;/p&gt;
&lt;p&gt;First off, let&#39;s assume you haven&#39;t read anything at all about Scala&#39;s type classes, but you do have a Ruby background. In Ruby, you really loved duck typing. It&#39;s the best. I can just use methods and pass objects around and things work. If a class doesn&#39;t have a method I need, I can either rely on some method missing magic, or just monkey patch it. To those who don&#39;t know Ruby, monkey patching is the ability to add a method to an existing class on the fly.&lt;/p&gt;
&lt;p&gt;What if I told you that in Scala, for a small penalty, you could create methods that are flexible, like duck typed methods, and 100% compile-time type safe. Not only that, you&#39;re also able to monkey-patch types as needed. Don&#39;t believe me? Let&#39;s look at some of the fun in the Scala ARM library.&lt;/p&gt;
&lt;p&gt;The motivation behind the ARM library is to create a flexible, robust (from exceptions) and simple-to-use automated-resource-management library that can greatly simplify things like JDBC or I/O in Scala. The standard introduction to ARM in Scala is the withResource method outlined below.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; withResource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;: { def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;resource&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; resource&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  try&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    f(r)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; finally&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    r.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The with resource method takes a by-name argument that returns an opened resource, and a function that manipulates the resource. We ensure that the close method on the resource is called after the manipulation function f. We&#39;ve declared the type of the resource using structural typing. This means the compiler will accept anything that defines a close method. We can then utilize withResource as follows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;withResource(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; FileInputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; File&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;myFile.txt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; stream &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;  // Code using stream and returning something&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, this code is already giving us a form of duck-typing (called structural typing in Scala). What about monkey patching? This is where using the type class pattern can help. Also note, that I prefer calling the type class pattern from haskell the &quot;type trait&quot; pattern in Scala. This could be confusing, as C++ has a type trait pattern as well, however the fact that I use traits to define type classes is just as confusing. In any case, please think of type traits and type classes, for the rest of this blog article as the same thing: A manifestation of type classes in Scala.&lt;/p&gt;
&lt;p&gt;Let&#39;s modify the withResource method to use a type class instead of a structural type.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here is our Resource type class. It defines a single method close. We can now modify our withResource method to utilise this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; withResource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;resource&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  val&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; resource&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  try&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    f(r)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; finally&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    implicitly[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]].close(r)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice two things here. First, we use the context-bounds constraint on the resource type parameter (A : Context). This means that we have to look up our implicit type trait using the implicitly method. We could, instead, give our method a well-defined implicit. For now, we&#39;ll use the context-bound syntax as it&#39;s quickly starting to denote type traits in Scala.&lt;/p&gt;
&lt;p&gt;If we attempt to call this method as things stnad, we&#39;ll get an implicit lookup error:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; withResource(new java.io.StringReader(&amp;quot;HAI&amp;quot;)) { input =&amp;gt; () }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;:9: error: could not find implicit value for evidence parameter of type Resource[java.io.StringReader]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       withResource(new java.io.StringReader(&amp;quot;HAI&amp;quot;)) { input =&amp;gt; () }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                                 ^&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&#39;s think through what we&#39;ve done. Any type can be used in the withResource method as long as an implicit is available for the given type in the implicit lookup resolution. The simplest way to create an implicit for usage is to provide one directly. This is easy to do in the REPL:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; implicit object stringResourceTrait extends Resource[java.io.StringReader] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  override def close(r : java.io.StringReader) = r.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;defined module stringResourceTrait&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; withResource(new java.io.StringReader(&amp;quot;HAI&amp;quot;)) { input =&amp;gt; () }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that we&#39;ve satisfied the type system for the particular type java.io.StringReader. Our old method was duck typed to use anything that had a close method. We can actually get this same functionality with type traits as well. Let&#39;s provide a default type trait in the Resource Companion object. To do so, let&#39;s create a resource.scala file that looks like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;  implicit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; genericResourceTrait&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;: { def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; r.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What we&#39;ve done is provide a method &quot;genericResourceTrait&quot; that is available on &lt;em&gt;every implicit search&lt;/em&gt; for any Resource[_] type. We&#39;ve now gotten back to our previous functionality, and we can take this a bit farther. What if we wanted to provide a specific implementation for a type? In the case of resources, Java provides a java.io.Closeable interface that most IO classes implement. Creating a type trait implementation for this will give us the benefit of not needing to use reflection when we know a specific type. Let&#39;s take our first crack at it.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;  implicit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; genericResourceTrait&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;: { def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Unit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; r.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; toString&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Resource[{def close() : Unit }]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;  implicit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; jioResourceTrait&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&lt;/span&gt;&lt;span&gt; java.io.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Closeable&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; close&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;r&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; r.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; toString&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Resource[java.io.Closeable]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We now have a duck typed, reflection based implementation and an implementation for the java.io.Closeable interface. We can prove that this works via the REPL by creating a mechanism of checking what type trait is used:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; def showTypeTraitUsed[A : Resource](a : A) = println(implicitly[Resource])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;showTypeTraitUsed: [A](a: A)(implicit evidence$1: Resource[A])Unit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; showTypeTraitUsed(new java.io.StringReader(&amp;quot;HAI&amp;quot;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Resource[java.io.Closeable]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; showTypeTraitUsed(new { def close() : Unit = () })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Resource[{def close() : Unit }]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We see that when passed a java.io.StringReader, something that subclasses java.io.Closeable, it uses the type trait defined for closeables. When we pass an anonymous object that has a close method, it uses the structural-typed type trait instead. What about monkey patching? Well, using type traits, you can achieve a form of monkey patching. Let&#39;s pretend an integer is a resource. All we have to do is create a new type trait implementation for it. Let&#39;s make that happen:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; implicit object intAsResource extends Resource[Int] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     | override def close(r : Int) = println(&amp;quot;closing: &amp;quot; + r)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     | }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;defined module intAsResource&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; withResource(5)(println)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;closing: 5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define our close method to print out a string, so we can make sure it happens. Finally, we can use an integer in our withResource method as if it were a resource, hence monkey-patching a close method onto it. In fact, we could also use this mechanism to change the implementation of the Resource type trait in a particular context. The key to this, as described in my book, is to ensure that you place your default implicitly available type trait implementation into the correct scope, such that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They can be overridden&lt;/li&gt;
&lt;li&gt;They do not conflict&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can also use certain Scala tricks like having method return types be based on implicits available within scope. Combining these all, you can create some rather interesting libraries, such as what is available in scala arm&#39;s typeclass-fix branch. This branch contains a rather interesting flatMap that can detect if you are mapping to a Traversable type, and return a Traversable. It will also detect if you are flatMapping to another Resource and return a new ManagedResource that will open/close that nested resource. Finally, it also supports the standard flatMap implementation. Working with type classes in Scala adds a bit of overhead in amount of code, and some time to toy with the type system but can provide great flexibility in design. I&#39;m working on a library for interactive with Nexus, using webDAV protocol. Using a &quot;FileLike&quot; type trait has been helpful in the design, especially since certain methods can be optimised in particular algorithms where we know both types. It&#39;s also useful because I&#39;ve adapter several different WebDAV-enabled libraries to the API before finally deciding on the library I disliked the least. So in recap, let&#39;s look at why the typeclass pattern is so powerful in Scala:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Can provide implementations for many differing types, in hierarchical fashion&lt;/li&gt;
&lt;li&gt;Can provide new type trait implementation from any scope&lt;/li&gt;
&lt;li&gt;Allows at-usage-site overriding of implementation details&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>How you should think about Delimited Continuations</title>
        <published>2010-03-10T00:00:00+00:00</published>
        <updated>2010-03-10T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/how-you-should-think-about-delimited-continuations/"/>
        <id>https://jsuereth.com/posts/how-you-should-think-about-delimited-continuations/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/how-you-should-think-about-delimited-continuations/">&lt;p&gt;Scala 2.8.x is going to have a new delimited continuations plugins.   Delimited continuations are interesting animals, and I&#39;ve seen some decent articles about them.   I&#39;m going to portray here how you should think about delimited continuations in a way that will help you know how to make use of them in your coding.&lt;/p&gt;
&lt;p&gt;Let&#39;s take a look at the &lt;a rel=&quot;external&quot; href=&quot;http://en.wikipedia.org/wiki/Delimited_continuation&quot;&gt;wikipedia definition delimited continuations&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
a &lt;b&gt;composable continuation&lt;/b&gt;, &lt;b&gt;delimited continuation&lt;/b&gt; or &lt;b&gt;partial continuation&lt;/b&gt;, is a &quot;slice&quot; of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Continuation&quot; title=&quot;Continuation&quot;&gt;continuation&lt;/a&gt; &lt;a class=&quot;mw-redirect&quot; href=&quot;http://en.wikipedia.org/wiki/Stack_frame&quot; title=&quot;Stack frame&quot;&gt;frame&lt;/a&gt; that has been &lt;a href=&quot;http://en.wikipedia.org/wiki/Reification_%28computer_science%29&quot; title=&quot;Reification (computer science)&quot;&gt;reified&lt;/a&gt; into a &lt;a class=&quot;mw-redirect&quot; href=&quot;http://en.wikipedia.org/wiki/Function_%28computer_science%29&quot; title=&quot;Function (computer science)&quot;&gt;function&lt;/a&gt;.&lt;/blockquote&gt;
&lt;p&gt;This is basically saying that a delimited continuation is marking a particular section of a program in such a way the compiler can &quot;reify&quot; it into a Function (or Closure if you will).  In fact, the compiler can use similar semantics to how a closure is made, the difference here is in how you make arguments to functions.   Let&#39;s take a look at a simple Scala delimited continuation example and try to de-mystify:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import scala.continuations.ContextControl._&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;object SimpleCont {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  def main(args : Array[String]) : Unit = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    val result = reset {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       1 + shift { k =&amp;gt; k(k(5)) } + 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Console.println(result)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This simple example prints 9 to the screen.   So what is happening?   Well, let&#39;s look at the definition: A &quot;slice&quot; of a continuation frame that has been reified into a function....&lt;/p&gt;
&lt;p&gt;Well, we know reify essentially means  to make it a first-class citizen (or first-class-function).   So what is our &quot;slice of continuation frame&quot;?   It&#39;s everything inside the reset block.   This is where the &quot;delimited&quot; portion comes in.  We&#39;re using reset to as an outer-bounds for the compiler to capture code and reify it into a function.   In doing so, the compiler essentially takes all shift keywords and turns them into arguments for the reified function.   Let&#39;s look at what our reified function is equivalent to in the above example:&lt;/p&gt;
&lt;pre&gt;reset {
       1 + &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shift { k =&gt; k(k(5)) }&lt;/span&gt; + 1                   
  }
  val reifiedFunction = { shiftVal1 : Int =&gt; 
       1 + &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shiftVal1&lt;/span&gt; + 1
  }
&lt;/pre&gt;
&lt;p&gt;Notice how the entire &lt;code&gt;shift {..}&lt;/code&gt; expression is converted into an argument of the reified function.  That&#39;s where the continuation comes in.  All code inside the reset, minus the shift expressions, is turned into continuations... Something that could be called later and/or multiple times.   For the real magic, let&#39;s take a look at this shift operation.&lt;/p&gt;
&lt;p&gt;The shift command takes  a function as an argument.  This function takes another function as its only parameter (higher-order) and that function &lt;em&gt;is&lt;/em&gt; the reified continuation.   So... strangely enough, the shift expressions are turned into arguments on functions &lt;em&gt;passed into the functions defined in the shift expression&lt;/em&gt;.  Yes, this blew my mind at first, but becomes very interesting and handy.  Not only that, it&#39;s a true inversion of control.&lt;/p&gt;
&lt;p&gt;In this simple example, the portion of code defined in shift &lt;em&gt;becomes&lt;/em&gt; the main execution path, and calls the reified continuation.  Let&#39;s look at the translated code side-by-side&lt;br /&gt;&lt;/p&gt;
&lt;div style=&quot;width: 700px;&quot;&gt;
&lt;div style=&quot;float: left; width: 50%;&quot;&gt;
&lt;pre&gt;object SimpleCont {
  def main(args : Array[String]) : Unit = {
    val result = reset {
       1 + &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shift { &lt;span style=&quot;color: #771111; font-weight: bold;&quot;&gt;k =&amp;gt; k(k(5))&lt;/span&gt; }&lt;/span&gt; + 1                   
    }
    Console.println(result)
  }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;float: right; width: 50%;&quot;&gt;
&lt;pre&gt;object SimpleCont {
&lt;p&gt;def main(args : Array[String]) : Unit = {&lt;br /&gt;
// Reified function
val &lt;span style=&quot;color: #771111; font-weight: bold;&quot;&gt;k&lt;/span&gt; = { shiftVal1 : Int =&amp;gt;
1 + &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;(shiftVal1)&lt;/span&gt; + 1
}
val result = &lt;span style=&quot;color: #771111; font-weight: bold;&quot;&gt;k(k(5))&lt;/span&gt;
Console.println(result)
}
&lt;/pre&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, we&#39;ve completely reversed the shifts and the resets.  Let&#39;s try to understand an example which involves two variables.  The Continuations paper includes an example that takes the cartesian product of two lists (i.e.  List(A,B) and List(C,D) =&amp;gt; List((A,C), (A,D),(B,C),(B,D)) ).   First the example code straight from &lt;a rel=&quot;external&quot; href=&quot;http://lamp.epfl.ch/~rompf/continuations-icfp09.pdf&quot;&gt;the delimited continuations paper&lt;/a&gt; (with a few modifications for simplicity):&lt;/p&gt;
&lt;pre&gt;implicit def reflectiveList[A](xs:List[A]) = new {
      def reflect[B]() : A @cps[List[B], List[B]] = shift { k:(A =&amp;gt; List[B]) =&amp;gt;
        xs.flatMap(k)
      }
    }
    
    reset {
      val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
      val right = List(4,5,6)
      List((left.reflect[(String,Int)], right.reflect[(String,Int)]))
    }
  }
// result: cartesian product
&lt;/pre&gt;
&lt;p&gt;Let&#39;s decompose this one a bit.   The implicit reflective stuff is used to enabled the reflective method on lists.  We&#39;ll analyze reflective soon, but for now, notice that it calls shift.   This means that calls to reflective are equivalent to calls to shift.  Let&#39;s try to figure out what the reified continuation is by first &quot;inlining&quot; all the code:&lt;/p&gt;
&lt;div style=&quot;width: 600px;&quot;&gt;
&lt;div style=&quot;float: left; width: 50%;&quot;&gt;
&lt;pre&gt;reset {
  val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
  val right = List(4,5,6)
  List(Tuple2(
      &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;left.reflect[Any],&lt;/span&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;span style=&amp;quot;color: #111199; font-weight: bold;&amp;quot;&amp;gt;right.reflect[Any]&amp;lt;/span&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;))
}
&lt;/pre&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div style=&quot;float: right; width: 50%;&quot;&gt;
&lt;pre&gt;reset {
  val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
  val right = List(4,5,6)
  List(Tuple2(
        &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shift { k:(A =&amp;gt; C[B]) =&amp;gt;
          left.flatMap(k)
        },&lt;/span&gt;
        &lt;span style=&quot;color: #111199; font-weight: bold;&quot;&gt;shift { k:(A =&amp;gt; C[B]) =&amp;gt;
          right.flatMap(k)
        }&lt;/span&gt;
  ))
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;First of all, Notice the new type signatures on the shift expressions (without the generics).  We&#39;ll discuss these more in depth later, but if hand-reify the continuation, we need to make sure we match the signatures expected from the shift expressions.  Let&#39;s take our first crack at it:&lt;/p&gt;
&lt;pre&gt;val reified : = { (shift1, shift2) =&amp;gt;
  List(Tuple2(
        &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shift1&lt;/span&gt;,
        &lt;span style=&quot;color: #111199; font-weight: bold;&quot;&gt;shift2&lt;/span&gt; 
  ))
}
&lt;/pre&gt;
&lt;p&gt;Well, that seems simple enough...  except we&#39;re not creating the lists in the reified function!   Ah... remember, we&#39;re only reifying the continuation piece.  We can directly execute the portion of the code that are directly executable.  Let&#39;s try to completely unwind the code and translate it:&lt;/p&gt;
&lt;pre&gt;
// Original Code
reset {
      val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
      val right = List(4,5,6)
      &lt;span style=&quot;color: #660000; font-weight: bold;&quot;&gt;List((&lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;left.reflect[(String,Int)]&lt;/span&gt;, &lt;span style=&quot;color: #111199; font-weight: bold;&quot;&gt;right.reflect[(String,Int)]&lt;/span&gt;))&lt;/span&gt;
    }
&lt;span style=&quot;color: #660000; font-weight: bold;&quot;&gt;val reified : (String, Int) =&amp;gt; List[Any] = { (shift1, shift2) =&amp;gt;
  List(Tuple2(
        &lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;shift1,&lt;/span&gt;
        &lt;span style=&quot;color: #111199; font-weight: bold;&quot;&gt;shift2&lt;/span&gt;
  ))
}&lt;/span&gt;

//Original reset was here...
val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
val right = List(4,5,6)
&lt;span style=&quot;color: #117711; font-weight: bold;&quot;&gt;left.flatMap( l =&amp;gt; &lt;span style=&quot;color: #111199; font-weight: bold;&quot;&gt;right.flatMap(r =&amp;gt; reified(l,r)&lt;/span&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Well that&#39;s interesting....   This is the effect of apply delimited continuations and should help you begin to reason about them and how they operate, however the actual truth of the matter is, your code is transformed differently.  Let&#39;s take a look at what is really happening, just so we can dig deeper if needed.   Technically, there are two reified continuation (for the two shifts) so let&#39;s make our hand-reified code do the same:&lt;/p&gt;
&lt;pre&gt;
//Original reset was here...
val left = List(&quot;x&quot;,&quot;y&quot;,&quot;z&quot;)
val right = List(4,5,6)
val reified1 : (String =&amp;gt; List[Any]) = { shift1 =&amp;gt;
  val reified2 : (Int =&amp;gt; Any) = { shift2 =&amp;gt;
        Tuple2(shift1,shift2)     
  }
  right.flatMap(reified2)
}

left.flatMap(reified1)
&lt;/pre&gt;
&lt;p&gt;So this is where it gets strange.   We create a function &quot;refiied1&quot; which is passed to the first shift expression (left.flatMap).   This contains the outer continuation.   We then have a second call to shift.  This means there is a second continuation generated inside the first continuation.   The second function is responsible for generating the tuples.   This is based to the second shift expression &quot;right.flatMap&quot;.  As you can see, every time you use the shift function, you&#39;re causing the compiler to generate a new continuation to defer the next set of processing.   You can also see how in &quot;pure&quot; form, this has a very nested structure, whereas the original code was very &quot;flat&quot;.  Also, because work is automatically defered into continuation functions, you can use this style for concise and performant asynchronous work.&lt;/p&gt;
&lt;p&gt;Let&#39;s take a mini-dive into a continuations for automated resource management.  First, let&#39;s make our sample file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;HAI!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;MY!&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let&#39;s right a continuation-based program to read each line and give us the size of it.  I&#39;m purposely making the code slightly confusing here to demonstrate the inversions.   Well, let&#39;s start off with a mechanism of &quot;reflecting&quot; or &quot;inverting&quot; a resource for delimited continuations.&lt;br /&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;trait&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; flatMap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;U&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; U&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; U&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; reflect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; @&lt;/span&gt;&lt;span&gt;cps[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; shift &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; k&lt;/span&gt;&lt;span&gt; : (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; flatMap(k)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Closeable&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;acquire&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Resource&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;R&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; flatMap&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;U&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; R&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; U&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; U&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        val&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; acquire&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;        try&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          f(x)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; finally&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;          System&lt;/span&gt;&lt;span&gt;.out.println(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Closing Resource!&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          x.close()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We define a simple Resource trait that gives us a flatMap (some may call this &quot;using&quot;) and a reflect (or inverse) method.   This will let us &quot;flatten&quot; resource usage inside a reset block.   The next component is the implementation of resource for any java.io.Closeable implementation.   This will take a function to acquire a resource and implement the flatMap method correctly.   This should look &lt;em&gt;very&lt;/em&gt; similar to the reflect method defined on lists...  One might even say this method works to flatten any monad ;)&lt;/p&gt;
&lt;p&gt;Now, let&#39;s get really dirty and define an inversion on java.io.InputStream to deal with lines:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; reflect&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;A&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; InputStream&lt;/span&gt;&lt;span&gt;](&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;input&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; A&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; each_line&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; String&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; @&lt;/span&gt;&lt;span&gt;cps[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; shift &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; k&lt;/span&gt;&lt;span&gt; : (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; B&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    val&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; BufferedReader&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; InputStreamReader&lt;/span&gt;&lt;span&gt;(input))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    var&lt;/span&gt;&lt;span&gt; line&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; b.readLine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    var&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ListBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;    while&lt;/span&gt;&lt;span&gt;(line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;!= null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      list &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+=&lt;/span&gt;&lt;span&gt; (k(line))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&lt;/span&gt;&lt;span&gt; b.readLine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    list.toList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&#39;ve defined our reflect method to work on every line within the file.  It builds up a list and returns it as the final result.  You&#39;ll notice the shift call takes a function requiring a String and returning a &quot;B&quot;.   The fun type-system issue we have to deal with here is you need to know the result of the reified continuation in the return value for each_line.   We&#39;ll get to that more in a moment, but for now make note of this.&lt;/p&gt;
&lt;p&gt;Alright, now let&#39;s define our simple function to manipulate every line in a file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;reset &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; resource(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; FileInputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;test.txt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)).reflect[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Any&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //We cheat on the return value here!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; line&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; reflect(input).each_line[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //We need to know the continuation returns an Int Here!!!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;      System&lt;/span&gt;&lt;span&gt;.out.println(line)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      line.size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wow... so really all we&#39;re doing is grabing our input, inverting to the &quot;each_line&quot; call and calling line.size after printing the line to the console.&lt;/p&gt;
&lt;p&gt;So what&#39;s the output look like?&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;scala&amp;gt; Main.resourceTest()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;HAI!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;O&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;MY!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Closing Resource!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;res0: Any = List(4, 0, 1, 3, 0)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is great!   It&#39;s doing what we wanted.   Notice how we&#39;ve &quot;flattened&quot; the code substantially compared with other methods.  Contrast the above with the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;val&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span&gt; scala.io.magic.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;File&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;test.txt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  stream &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; file.managedInput&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; input.lines&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; yield&lt;/span&gt;&lt;span&gt; line.size&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&#39;ll notice a few similarities (in my made up library).   For-expressions are another area in scala where you&#39;re reifing portions of the code into closures and passing them to flatMap blocks.  However, delimited continuations open the doors to way more than flatMap.   Let&#39;s go back to that typing issue though.&lt;/p&gt;
&lt;p&gt;If you remember, I was returning Any because I was being lazy with my type annotations when reflecting the input stream.   Let&#39;s use the correct annotation:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;reset &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; resource(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; FileInputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;test.txt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)).reflect[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      val&lt;/span&gt;&lt;span&gt; line&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; invert(input).each_line[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //We need to know the continuation returns an Int Here!!!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;      System&lt;/span&gt;&lt;span&gt;.out.println(line)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      line.size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So... what do these type annotations mean?   In the case of the resource&#39;s reflect method, the type annotation is the ultimate result from the reset call (i.e. the result of the reified continuation) because of how we defined flatMap on Resource.   The type annotation for each_line describes the type the resulting List of processed lines should contain... i.e. the result of the last portion of the continuation.  If we were to hand-code this reset piece we&#39;d have:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;val&lt;/span&gt;&lt;span&gt; rcont1&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;FileInputStream&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  inputStream &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     val&lt;/span&gt;&lt;span&gt; rcont2&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;span&gt; line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;        System&lt;/span&gt;&lt;span&gt;.out.println(line)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        line.size&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     val&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; BufferedReader&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; InputStreamReader&lt;/span&gt;&lt;span&gt;(inputStream))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     var&lt;/span&gt;&lt;span&gt; line&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; b.readLine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     var&lt;/span&gt;&lt;span&gt; list&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; ListBuffer&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;B&lt;/span&gt;&lt;span&gt;]()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;     while&lt;/span&gt;&lt;span&gt;(line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;!= null&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        list &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;+=&lt;/span&gt;&lt;span&gt; (rcont2(line))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        line &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&lt;/span&gt;&lt;span&gt; b.readLine&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      list.toList&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  resource(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; FileInputStream&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;test.txt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)).flatMap(rcont1)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So... delimited continuations certainly use far less code here.  The understanding of what&#39;s going on under-the-covers perhaps needs some good terminology/consistent paradigms to be useful in library-form (i.e. if I make each_line or reflective part of a standard library).   Also, it&#39;s my opinion that the type-inference barrier for the @cps annotation should be looked into.  Perhaps we can invent creative ways to infer the types in an inverted fashion when using delimited continuations.  In the meantime, the compiler will complain until you figure out the correct types, so it could be worse ;)&lt;/p&gt;
&lt;p&gt;I plan to post a bit more about delimited continuations after I have some useful I/O related tools built up around them.   Let me know if you find this post helpful.  I&#39;d also like to clarify that my &quot;hand-reification&quot; is not really how scala does reification, but close enough for understanding the concepts.&lt;/p&gt;
&lt;p&gt;Also, there&#39;s some interesting continuation-based actors code in the paper that I&#39;m going to try to dig into here when I have the time!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>New Blog Site</title>
        <published>2009-02-20T00:00:00+00:00</published>
        <updated>2009-02-20T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/new-blog-site/"/>
        <id>https://jsuereth.com/posts/new-blog-site/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/new-blog-site/">&lt;p&gt;Hey, just thought I&#39;d make an initial post to announce my switch to a statically generated blog site.  Hopefully I can keep the content kicking AND still have neato features like RSS feeds, etc.&lt;/p&gt;
&lt;p&gt;The new syte is done in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt; and makes it &lt;em&gt;way&lt;/em&gt; easier for me to write content, so hopefully I&#39;ll be doing so more frequently!&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Leveraging Annotations in Scala</title>
        <published>2009-02-05T00:00:00+00:00</published>
        <updated>2009-02-05T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/leveraging-annotations-in-scala/"/>
        <id>https://jsuereth.com/posts/leveraging-annotations-in-scala/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/leveraging-annotations-in-scala/">&lt;p&gt;This post began as a discussion on #scala about how people define a public &quot;getter&quot; method and private &quot;setter&quot; method using only defs and vars in scala.  The usual method in scala is:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MyClass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;   private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; var&lt;/span&gt;&lt;span&gt; x_private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x_private&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The downside to this is that you have two names to express one concept.  I&#39;m at the point where I&#39;m no longer caring too much about public/private parts of classes, but I thought I&#39;d tackle the problem to exercise my new-found compiler skills.  It turns out this feat is pretty easy to accomplish via a scalac plugin.&lt;/p&gt;
&lt;p&gt;To create your first plugin you should follow the guide &lt;a rel=&quot;external&quot; href=&quot;http://www.scala-lang.org/node/140&quot;&gt;here&lt;/a&gt;.  We&#39;ll be using the Maven as our build tool, since to do otherwise would be blasphemy (for me).&lt;/p&gt;
&lt;p&gt;To start off with, here&#39;s the POM file for our plugin:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xmlns&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://maven.apache.org/POM/4.0.0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xmlns&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;xsi&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xsi&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;schemaLocation&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;modelVersion&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;4.0.0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;modelVersion&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;private-setter-scalac-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;packaging&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;jar&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;packaging&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;1.0-SNAPSHOT&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Var definition extensions for the scala compiler&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://suereth.blogspot.com&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;scala-tools.org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Scala-tools Maven2 Repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://scala-tools.org/repo-releases&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;scala-tools.org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Scala-tools Maven2 Repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://scala-tools.org/repo-releases&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;scala-compiler&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;2.7.3&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-tools&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;maven-scala-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;add-source&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;compile&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;testCompile&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;configuration&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArgs&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;-Xms64m&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;-Xmx1024m&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;jvmArgs&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;configuration&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.codehaus.mojo&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;shitty-maven-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;clean&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;install&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;test&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There&#39;s a few things in this pom, mostly to ensure that the scala-tools repositories are available too us.  Also, ignore the shitty (Super-Helpful-Integration-Testing-ThingY) plugin for now.  The most important part is that we&#39;re compiling to a jar file, and we&#39;re depending on the scala-compiler.   The version of the compiler we depend on is the &lt;em&gt;only&lt;/em&gt; version of scala your plugin should be used with.  In all reality, the scala-compiler should probably be deifned as a &#39;provided&#39; dependency and the scala-library should explicitly be depended on, but for now we&#39;ll cheat on completeness.&lt;/p&gt;
&lt;p&gt;Next we need to make sure there is a &lt;code&gt;scalac-plugin.xml&lt;/code&gt; file in the created JAR. This is simple in maven, just place one in the &lt;code&gt;src/main/resources&lt;/code&gt; directory.   Here is what my &lt;code&gt;src/main/resources/scalac-plugin.xml&lt;/code&gt; looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;private-setters&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;classname&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala_lang.privateSetter.internal.privateSetterPlugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;classname&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&#39;ll notice I&#39;m naming my plugin &quot;private-setters&quot; and placing it in a &quot;internal&quot; package.   This could be my eclipse plugin development rubbing off, but this helps me know what only the compiler should see.&lt;/p&gt;
&lt;p&gt;Next we need a way for clients of our plugin to &quot;notify&quot; us that they want a var with a private setter method (@varname_=@), but public getter (@varname@).   Here&#39;s my initial cut at client syntax:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;scala_lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;privateSetter&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TestWidget&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  @&lt;/span&gt;&lt;span&gt;privateSetter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  var&lt;/span&gt;&lt;span&gt; myVar&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pretty simple really, but effective.  (Also I already know how to look up annotations from my scala-mojo-support project, so it only took me a few hours to work out the scalac plugin details.   This post actually took the most amount of time in the whole venture).&lt;/p&gt;
&lt;p&gt;Now we need to define the @privateSetter@ annotation we can use in our classes.  It&#39;s a fairly simple file:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;scala_lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;privateSetter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; privateSetter&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StaticAnnotation&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we need to write the plugin itself.  All plugins contain some boiler-plate code, so we&#39;ll ignore that for the time being. &lt;strong&gt;(see the &lt;a rel=&quot;external&quot; href=&quot;http://www.scala-lang.org/node/140&quot;&gt;documentation mentioned above&lt;/a&gt;)&lt;/strong&gt;.  The truly interesting part of this plugin is the newly defined phase.&lt;/p&gt;
&lt;p&gt;For some history, the Scala Compiler (scalac) is composed of various &quot;phases&quot;.  Each phase has a responsibility it performs.  Some phases are easy to identifier e.g. &lt;code&gt;icode&lt;/code&gt; which converts the AST to icode for each &quot;compilation unit&quot;.  In the Scala Compiler a compilation unit corresponds to a source code file and may produce multiple class files.  Here&#39;s the &lt;code&gt;scalac -Xlist-phases&lt;/code&gt; output on my machine:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellsession&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;$&lt;/span&gt;&lt;span&gt; scalac -Xshow-phases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;namer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;typer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;superaccessors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pickler&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;refchecks&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;liftcode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;uncurry&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;tailcalls&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;explicitouter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;erasure&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lazyvals&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;lambdalift&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;constructors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;flatten&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;mixin&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cleanup&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;icode&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;inliner&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;closelim&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dce&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;jvm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sample-phase&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;On to our implementation!  The basic structure of our phase looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; MakeSettersPrivatePhase&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;prev&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Phase&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Phase&lt;/span&gt;&lt;span&gt;(prev)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; VarAccessChanger&lt;/span&gt;&lt;span&gt;.this.name&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;      import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; global&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;      override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; run&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; for&lt;/span&gt;&lt;span&gt; (unit &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; global.currentRun.units; &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; !&lt;/span&gt;&lt;span&gt;unit.isJava)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           unit.body &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TreeTransformer&lt;/span&gt;&lt;span&gt;.transform(unit.body)      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&#39;ll mention that this class is nested inside an outer &quot;Plugin&quot; class which is passed the &#39;global&#39; object.  For those of you unfamiliar with the compiler, the &quot;Global&quot; object is the outer layer of the &quot;cake&quot; pattern used by the Scala Compiler.  I&#39;m reserving judgment on Global, but I have noticed that it&#39;s very hard to unit test any &quot;module&quot; you write for Global (as most of them have the &lt;code&gt;self:Global =&amp;gt;&lt;/code&gt; syntax).&lt;/p&gt;
&lt;p&gt;The Transformer is a very nice class for doing AST manipulation (thanks DRM for suggesting it).   This class simply transforms the AST from one form to another.  It&#39;s perhaps the easiest way to implement our plugin.  Let&#39;s set up a Tree Transformation that does absolutely nothing useful:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TreeTransformer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Transformer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;        override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; transform&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;tree&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Tree&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; tree &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;         case&lt;/span&gt;&lt;span&gt; t &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           super.transform(t)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The structure here is we override the transform method.  This method takes a tree and returns a tree.  We want to transform &lt;em&gt;only&lt;/em&gt; the setter part of a var method if it contains the privateSetter annotation.   Let&#39;s apply our pattern matching skills to the test with an extractor:  The &quot;AnnotationSetterShouldBePrivate&quot; extractor.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AnnotatedSetterShouldBePrivate&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; unapply&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;node&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Tree&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Option&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DefDef&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; hasPrivateSetterAnnotation&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;annotations&lt;/span&gt;&lt;span&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; List&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Annotation&lt;/span&gt;&lt;span&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Boolean&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      for&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           annotation &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; annotations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;           if&lt;/span&gt;&lt;span&gt; annotation.tpe.safeToString &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; classOf&lt;/span&gt;&lt;span&gt;[privateSetter].getName&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      } {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      false&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    node &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; DefDef&lt;/span&gt;&lt;span&gt;(mods,name,_,_,_,_)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; if&lt;/span&gt;&lt;span&gt; name.toString.endsWith(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;_$eq&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;         if&lt;/span&gt;&lt;span&gt;(hasPrivateSetterAnnotation(mods.annotations))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;            Some&lt;/span&gt;&lt;span&gt;(x)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;            None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; _ &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; None&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, note that our extractor (unapply method) takes in a tree and returns a DefDef.  DefDef is the AST class for a &quot;def&quot; node.   All var&#39;s are parsed into dual &quot;def&quot; methods (setter and getter).  We define a helper method that takes a list of annotations and looks for our &quot;privateSetter&quot; annotation.   The actually implementation of the extractor matches against the tree node, checks to see if it&#39;s a DefDef and has a tailing name of &quot;&lt;em&gt;$eq&quot;.   &quot;&lt;/em&gt;$eq&quot; is the mangled form of &quot;_=&quot; which is the convention for scala setter methods.  Note that I probably could move the if/else statement into the pattern match on the DefDef, but wasn&#39;t feeling adventurous enough this evening.  In the case where we find a valid annotated setter DefDef method, we return it, otherwise return None.&lt;/p&gt;
&lt;p&gt;Now that we have our extractor, writing the tree transformation becomes fairly simple:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;object&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; TreeTransformer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Transformer&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;  override&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; def&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; transform&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;tree&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; Tree&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; tree &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;match&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; AnnotatedSetterShouldBePrivate&lt;/span&gt;&lt;span&gt;(d &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; DefDef&lt;/span&gt;&lt;span&gt;(mods,name,tparams,vparams,tpt,impl))&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;          import&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; symtab&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;Flags&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;          val&lt;/span&gt;&lt;span&gt; tree&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; copy.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DefDef&lt;/span&gt;&lt;span&gt;(d, mods &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; PRIVATE&lt;/span&gt;&lt;span&gt;,name,tparams,vparams,tpt,transform(impl))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          tree.symbol.setFlag(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;PRIVATE&lt;/span&gt;&lt;span&gt;)              &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          tree&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;      case&lt;/span&gt;&lt;span&gt; t &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          super.transform(t)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, we&#39;re combining our &quot;AnnotatedSetterShouldBePrivate&quot; extractor with the natural extractor for the DefDef case class so that we can pull out all the constructor variables (along with the DefDef itself using the &lt;code&gt;d @&lt;/code&gt; syntax).  Initially I tried returning just a newly constructed DefDef with &quot;mods | PRIVATE&quot; instead of &quot;mods&quot; in the constructor.  You&#39;ll find this blows up horribly.   The main issue is that the AST nodes contain &lt;em&gt;more&lt;/em&gt; than just their constructor values (types and symbols being the two things I found).   The Transformer class provides a &quot;copy&quot; value/object that you can use to &quot;copy&quot; various parts of the tree.   The copy class contains a method for every tree node that takes an original tree and overriding constructor values.  For our purposes, we&#39;re applying the PRIVATE flag to the &quot;mods&quot; attribute of the &lt;code&gt;DefDef&lt;/code&gt; and two the symbol flags for the &lt;code&gt;DefDef&lt;/code&gt;.  The symbol flags are what are eventually used in the icode-&amp;gt;bytecode conversion code.&lt;/p&gt;
&lt;p&gt;Next we should choose what phase to run this plugin after.  I&#39;ve chosen the &lt;code&gt;typer&lt;/code&gt; phase, as this ensures we at least have an AST and the types are correct.  When defining a class in isolation, this is working perfectly.  However when defining the class and using it with other classes, I&#39;m running into the difficulty where the methods I&#39;m modifying are eventually being replaced with public methods of a differing names.  To look into this, we should set up some integration tests.  NOW we can use the SHITTY plugin!&lt;/p&gt;
&lt;p&gt;The maven shitty plugin allows you to execute &quot;integration&quot; projects that depend on the currently &quot;building&quot; project.   You simple create a directory in src/it, add a pom.xml that depends on your project (with a version of &quot;testing&quot;), and a goal.txt that describes which maven goals should be executed.   If the integration project&#39;s maven build succeeds, the overall project&#39;s maven build continues.  If an integration project&#39;s maven build fails, the entire build fails.  This works great for &quot;positive&quot; tests (or test where you make sure that things compile with your plugin).   Let&#39;s define a relatively simple positive test.   First, we take the class outlined earlier as our example syntax:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;scala_lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;privateSetter&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; privateSetter&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; extends&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; StaticAnnotation&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next we create a pom file for this integration test:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xmlns&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://maven.apache.org/POM/4.0.0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xmlns&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;xsi&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; xsi&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;schemaLocation&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;modelVersion&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;4.0.0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;modelVersion&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-tools&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;testPrivateSetter&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;packaging&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;jar&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;packaging&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;1.0-SNAPSHOT&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;test-privateSetter-scalac-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;private-setter-scalac-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;testing&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;junit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;junit&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;4.5&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;test&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependencies&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-tools&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;maven-scala-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;add-source&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;compile&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;goals&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;execution&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;executions&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;configuration&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;compilerPlugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;org.scala-lang&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;groupId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;private-setter-scalac-plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;artifactId&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;       &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;testing&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;version&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dependency&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;compilerPlugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;      &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;arg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;-verbose&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;arg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;configuration&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugin&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;plugins&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;scala-tools.org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Scala-tools Maven2 Repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://scala-tools.org/repo-releases&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;repositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;scala-tools.org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Scala-tools Maven2 Repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://scala-tools.org/repo-releases&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;snapshots.scala-tools.org&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;Scala-tools Maven2 Snapshot Repository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;http://scala-tools.org/repo-snapshots&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;url&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepository&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;pluginRepositories&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;project&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You&#39;ll notice we&#39;re making us of the &quot;testing&quot; version of our plugin AND the &quot;compilerPlugin&quot; configuration option of the maven-scala-plugin.   This option is new to the (not yet released except as a SNAPSHOT) 2.10 version, and allows you to depend on any number of scalac plugins during your build.  We&#39;re using it now to depend on our build.  Our goals.txt simply consists of &quot;clean compile&quot;.&lt;/p&gt;
&lt;p&gt;Another thing that the SHITTY plugin lets you do is provide a &#39;validate.groovy&#39; file with your pom.xml and goals.txt.   This file is run after a build to ensure things were successful.   We can use this to ensure our generated classfiles have private setters.  We&#39;ll tackle that problem another day (I&#39;m currently being lame and running javap on the .class files).&lt;/p&gt;
&lt;p&gt;I think I&#39;ve typed as much as I can for one night, I&#39;ll try to cover the remaining pieces (after I code/finish them) later.  Once again, there are some issues with the plugin as I&#39;m confusing the hell out of some of the compiler phases (not to mention being confused myself as to where things happen in some cases).  If you&#39;d like to look at the source (and perhaps contribute? ), it&#39;s available on github: &lt;a href=&quot;http://github.com/jsuereth/private-setter-scalac-plugin/tree/master&quot;&gt;private-setter-scalac-plugin&lt;/a&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Subclassing in ExtJS</title>
        <published>2008-12-08T00:00:00+00:00</published>
        <updated>2008-12-08T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/subclassing-in-extjs/"/>
        <id>https://jsuereth.com/posts/subclassing-in-extjs/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/subclassing-in-extjs/">&lt;p&gt;It&#39;s been a while since I posted about ExtJS, so I thought I&#39;d take a crack at another post here.  This is going to cover the &lt;code&gt;Ext.extend&lt;/code&gt; method, and how you go about sub-classing an Ext component.&lt;/p&gt;
&lt;p&gt;The basic just is that we are going to create a &quot;constructor&quot; method for the widget and bind it to a location (e.g. &lt;code&gt;Ext.ux.data.MyDataStore&lt;/code&gt;).  Then we&#39;re going to use &lt;code&gt;Ext.extend&lt;/code&gt; to plug in the methods of the parent objects, and override some at the same time.&lt;/p&gt;
&lt;p&gt;For our example, I&#39;ll be using the &lt;code&gt;Ext.data.DataProxy&lt;/code&gt; class as what we&#39;re extending.&lt;/p&gt;
&lt;p&gt;So... first, let&#39;s define our constructor in a location:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//Ensures there are at least dummy objects leading into Ext.ux.data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;namespace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Ext.ux.data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;superclass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The code doesn&#39;t do a whole lot, but we have our constructor.  However when executing &lt;code&gt;new Ext.ux.data.MyProxy()&lt;/code&gt; we get an error, superclass is &lt;em&gt;not&lt;/em&gt; defined.  So... where does superclass come from?  It will actually be injected into our prototype via the &lt;code&gt;Ext.extend&lt;/code&gt; method.   In fact, &lt;code&gt;Ext.extend&lt;/code&gt; will also inject the methods &quot;override&quot; and &quot;extend&quot; into our class.  Here&#39;s our new class with the &lt;code&gt;Ext.extend&lt;/code&gt; call:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//Ensures there are at least dummy objects leading into Ext.ux.data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;namespace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Ext.ux.data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;superclass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;DataProxy)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can make use of DataProxy methods from within MyProxy using &lt;code&gt;this.methodName(args)&lt;/code&gt;.   This isn&#39;t very exciting yet because DataProxy is mostly an empty class (besides configuring the Observable class for us).&lt;/p&gt;
&lt;p&gt;The convention for adding methods into a subclass is to use the override parameter from &lt;code&gt;Ext.extend&lt;/code&gt;.   This parameter is essentially an anonymous class containing methods to inject &quot;over-top&quot; of the parent class.  In the case where the method did not exist before, it is merely inserted.  Let&#39;s implement the &lt;code&gt;load&lt;/code&gt; method (required for &lt;code&gt;DataProxy&lt;/code&gt; subclasses) using this approach:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//Ensures there are at least dummy objects leading into Ext.ux.data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;namespace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Ext.ux.data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;    //Calls our super-class (Ext.data.DataProxy) constructor for full initialization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;superclass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;DataProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;   //Load function as defined in HttpProxy, and needed for the &amp;quot;DataProxy interface...&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;   load&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; reader&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; || {};&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //Ensure params exists as an object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;         var&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;         try&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;            //Use record reading to read &amp;quot;stock&amp;quot; data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; reader&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;readRecords&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; zombieKid&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;like&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;, &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;turtles&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; catch&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           //We had an issue pulling in stock data, fire appropriate events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           //use callback in fail mode and bail.  This function is actually coming from&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           // the Observable class which is our &amp;quot;grandparent&amp;quot; class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;            this.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;fireEvent&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;loadexception&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;, this,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, null,&lt;/span&gt;&lt;span&gt; e&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, null,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FF9CAC;&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;         //Inform the callback of the result of parsing our &amp;quot;stock&amp;quot; data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FF9CAC;&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see... our load method is basically taking the reader (Record reader) passed in and attempting to read some stock data (calling appropriate callbacks as necessary).&lt;/p&gt;
&lt;p&gt;Notice the use of the &lt;code&gt;this.fireEvent&lt;/code&gt; inside our method.  Because of how JavaScript handles resolution, this will refer to our fully constructed class at the time of invocation ( as opposed to definition), therefore the &lt;code&gt;fireEvent&lt;/code&gt; method from &lt;code&gt;Observable&lt;/code&gt; will be accessable from our object.&lt;/p&gt;
&lt;p&gt;The next important thing in defining your own widget is adding information to take in during construction.  Our superclass constructor does not take any arguments, hence the &lt;code&gt;Ext.ux.data.MyProxy.superclass.constructor.call(this);&lt;/code&gt; in our constructor.  If perhaps our superclass constructor took a config object, we would have to change it.   Luckily our current widget constructor expects a config object, so we can just pass it through to our super (assuming the configs are complementary).&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;    //Calls our super-class (Ext.data.DataProxy) constructor for full initialization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;superclass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since the &lt;code&gt;DataProxy&lt;/code&gt; class doesn&#39;t take anything in its constructor, we&#39;ll remove this, but it&#39;s an important feature to make note of.  Also remember you can make subclasses which could add/change configuration information sent to parents.  That&#39;s the nice part about having control of the config obejct before passing it to the superclass constructor.&lt;/p&gt;
&lt;p&gt;Now onto taking in our own information.  Let&#39;s mimic the &lt;code&gt;MemoryProxy&lt;/code&gt; and take in some data during construction that we using during our &quot;load&quot; method.  We&#39;ll call this element &#39;data&#39; in the config and our object.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//Ensures there are at least dummy objects leading into Ext.ux.data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;namespace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Ext.ux.data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;){&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;    //Pull out our &amp;quot;data&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    this.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;    //Calls our super-class (Ext.data.DataProxy) constructor for full initialization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;superclass&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;extend&lt;/span&gt;&lt;span&gt;(Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ux&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;MyProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; Ext&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;DataProxy&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;   //Load function as defined in HttpProxy, and needed for the &amp;quot;DataProxy interface...&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;   load&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; reader&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; params&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; || {};&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //Ensure params exists as an object&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;         var&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;         try&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;            //Use record reading to read &amp;quot;passed in&amp;quot; data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; reader&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;readRecords&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;this.&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; catch&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;e&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           //We had an issue pulling in stock data, fire appropriate events&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           //use callback in fail mode and bail.  This function is actually coming from&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;           // the Observable class which is our &amp;quot;grandparent&amp;quot; class&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;            this.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;fireEvent&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;loadexception&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;, this,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, null,&lt;/span&gt;&lt;span&gt; e&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, null,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FF9CAC;&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;         }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;         //Inform the callback of the result of parsing our &amp;quot;stock&amp;quot; data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         callback&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;call&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;(&lt;/span&gt;&lt;span&gt;scope&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; callbackArg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FF9CAC;&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;   }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above is an almost exact duplicate of &lt;code&gt;MemoryProxy.js&lt;/code&gt; from the ExtJS source.  I thought about having a more clever tutorial about a custom widget, but I&#39;m trying to avoid assuming too much knowledge of ExtJS internals.  Anyway, I hope this makes you feel more comfortable creating your own classes and extending ExtJS widgets with custom functionality.   As always, let me know if you find any errors or typos.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Masochism</title>
        <published>2008-09-24T00:00:00+00:00</published>
        <updated>2008-09-24T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/masochism/"/>
        <id>https://jsuereth.com/posts/masochism/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/masochism/">&lt;p&gt;&lt;strong&gt;Note: Due to excessive comments about my grasp of the english languages, I&#39;ve done a second round of editing for this post.  It may show up on the RSS feed again, but there isn&#39;t any new content.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I&#39;ve recently had to jump into a bit of (very minor) C++ development at work.  This started bringing back memories of all the ?fun? it was to play around in the C++ type system.  Mostly you use the C++ type system in an attempt to prevent yourself from doing bad things. Sometimes though, it was a way of doing conditional compilation in a type-safe manner.&lt;/p&gt;
&lt;p&gt;I recently decided to outline how I tend to over-engineer my C++ code.  I&#39;ll give a few ground rules/goals first:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I won&#39;t use boost.  In real life, I do use boost whenever I can.  For the purposes of illustration I&#39;m doing a lot by hand.  If you&#39;re a C++ developer and you don&#39;t use boost, go learn it. Now.  Really, I mean right now go to &lt;a rel=&quot;external&quot; href=&quot;http://www.boost.org&quot;&gt;www.boost.org&lt;/a&gt; and enter the 21st century of coding.  No really, GO&lt;/li&gt;
&lt;li&gt;I&#39;m not doing detailed explanations (although I do some explanations) of the C++ language or its compiler workings.&lt;/li&gt;
&lt;li&gt;This could be an attempt to ramp myself to attack learning scala&#39;s (and maybe haskell&#39;s) type sytem.  C++ is the closest typed language I know that could prepare me&lt;/li&gt;
&lt;li&gt;C++/Java feels more like masochism the more new languages I learn.  I&#39;m hoping this &quot;simple&quot; post about using the C++ type system in a &quot;simple&quot; way will prove this to you.  At some point I hope to post a Scala/Haskell equivalent to this post (or perhaps someone else would like to?) just so you can see the amount of work C++ really requires (and the level of understanding required to write even a simple line).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Alright! so first off, the premise:  &lt;strong&gt;We&#39;re trying to create a class (&lt;code&gt;DynamicLibrary&lt;/code&gt;) that will let us dynamic load a shared library and access symbols.&lt;/strong&gt; I&#39;m only including code for linux (although porting to windows or &quot;cross-platforming&quot; isn&#39;t too bad. look into the LoadLibrary function in windows and dlopen in linux).&lt;/p&gt;
&lt;p&gt;Before we begin, I&#39;m going to construct something (available in boost) that should be in the utility kit of every C++ developer... a base class that removes the copy-constructor and operator= from visibility (effectively making it non-copyable)&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;#ifndef&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; NONCOPYABLE_H_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; NONCOPYABLE_H_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//============================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;// This class prevents copying on subclasses&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//============================================================================&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; NonCopyable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;() {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; ~NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;() {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; other&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; operator&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; other&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&#39;s a very simple class.  Most of its functions are purposefully inlined.  It should get compiled out during optimization phases, AND it can reduce typing for me later when I don&#39;t want a class copied around on the stack.
(Note: I could have saved one line of typing by changing to a struct and getting rid of the public: line. or putting all private functions first.  Sue me, but I prefer this look).&lt;/p&gt;
&lt;p&gt;Ok, next we&#39;re going to create a very small Meta-Programming Library.  This library will be used to tell us all sorts of information about the types in C++.  For now, please assume all these examples are surrounded in a &lt;code&gt;namespace mpl {}&lt;/code&gt; block.&lt;/p&gt;
&lt;p&gt;Here&#39;s our first bout of magic:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; true_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; false_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;  //Not really needed, but easier to understand if we have it&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;WTF? Why make two completely silly classes?  This is so I can represent the notion of a boolean in the type system. From now on, I want you to think of &lt;code&gt;true_&lt;/code&gt; as &quot;true in the meta-programming library&quot; and similarly for &lt;code&gt;false_&lt;/code&gt;. &lt;strong&gt;Note: In actuality I could probably use bool as a template parameter and get away with it, but this is more fun&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Ok, now let&#39;s make a simple method just so we can print the boolean value of a type.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; is_true&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; enum&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;true_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; enum&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; {&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So..... what is that?   That, my friends, is a C++ meta-programming function.  The first template applies to every type and &quot;returns&quot; a value of &lt;code&gt;0&lt;/code&gt; (or &lt;code&gt;false&lt;/code&gt;).  The second is a specialization for the &lt;code&gt;true_&lt;/code&gt; type that will return &lt;code&gt;1&lt;/code&gt;.  This means if we ever use the &lt;code&gt;is_true&lt;/code&gt; structure with &lt;code&gt;true_&lt;/code&gt; as the argument, the &quot;member&quot; value will be &lt;code&gt;1&lt;/code&gt;.  Its a little convoluted (or convolved?), but it works!  How do you call it?  Simply like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;some type&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That will return an actually value we can assign, test in an if statement or display on the console.  We&#39;re going for displaying on the console here (console = stdout).&lt;/p&gt;
&lt;p&gt;Ok, so let&#39;s get more interesting meta-functions.  The most important for what I&#39;d like to do later is the &lt;code&gt;is_ptr&lt;/code&gt; function.  Here&#39;s a look at it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; is_ptr&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; typedef&lt;/span&gt;&lt;span&gt; false_ value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; typedef&lt;/span&gt;&lt;span&gt; true_ value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So before we saw &quot;template specialization&quot;.  That is, template&amp;lt;&amp;gt; with all types completely specified in an alternative implementation of the function.  This time, we see &quot;partial template specialization&quot; (YAY!!?).  This is where we have an alternative implementation that still has some wildcards (i.e. partially specialized).  When used, the compiler will select the function from most specialized to most general based on our types, so we can get away with this.&lt;/p&gt;
&lt;p&gt;Now, what&#39;s that funny type-def doing and why isn&#39;t &quot;value&quot; part of an enum?  Well this meta-function takes a type as argument and returns a type (not a value).  You can string together meta-functions of this kind to make even stranger (and horrendous to look at) meta-functions.&lt;/p&gt;
&lt;p&gt;As a simple example, let&#39;s make an &quot;if&quot; meta-function:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; COND&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; if_&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; typedef&lt;/span&gt;&lt;span&gt; F value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; if_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;true_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; F&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; typedef&lt;/span&gt;&lt;span&gt; T value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above is a structure that will select either type &lt;code&gt;T&lt;/code&gt; or &lt;code&gt;F&lt;/code&gt; based on the valiue of &lt;code&gt;COND&lt;/code&gt; (much like an if statement).&lt;/p&gt;
&lt;p&gt;For now, let&#39;s test our first two meta-functions on a few combinations of the various incarnations of ints in C++.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;int*&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;int&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;const int&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;const int*&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;volatile int&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;const volatile int&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;volatile int*&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; cout&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;mpl::is_ptr&amp;lt;const volatile int*&amp;gt;::value = &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_true&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the output is:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const volatile int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;::&lt;/span&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Neat-o huh?  Yeah I probably could change the output to really represent things, ie &lt;code&gt;mpl::is_true&amp;lt;mpl::is_ptr&amp;lt;....&amp;gt;::value&amp;gt;::value ? &quot;true_&quot; : &quot;false_&quot;&lt;/code&gt;.  However it was already way to much typing.&lt;/p&gt;
&lt;p&gt;Ok, now onto why we were making meta-functions to begin with.  I want a somewhat type-safe way of importing symbols from a dynamicly loaded library and casting them to their appropriate types. This idea came from an old colleague at APL.  I hope this solution is different enough, as I don&#39;t recall his solution.&lt;/p&gt;
&lt;p&gt;Let&#39;s start off with our basic class structure for DynamicLib:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//ASSUME LIBRARY_HANDLE_TYPE and SYMBOL_POINTER_TYPE are defined somewhere above (os-specific)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; NonCopyable&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const char*const&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //implementation is OS-specific&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; ~DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;();&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;                     //implementation is os-specific&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; static&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; SYMBOL_POINTER_TYPE&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; get_symbol_internal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const char*const&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; LIBRARY_HANDLE_TYPE&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //implementation is OS-specific.  Didn&amp;#39;t feel like figuring out to privatize this.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; LIBRARY_HANDLE_TYPE&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //handle to library (os-specific)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First off, notice the use of NonCopyable.  This ensures the class cannot be copied (i.e. you have to use pointers to share it).  This helps us ensure that the library is loaded once and destroyed once.  You could still construct it twice for the same library, but if you do that... well a maniacal penguin dressed in a business suit will come steal your fish.&lt;/p&gt;
&lt;p&gt;Next, the &lt;code&gt;get_symbol_internal&lt;/code&gt; method is static.  This is because the public (local) method has yet to be written and needs to use the MPL library (YAY!!!?).  What we&#39;d like to do is find a way to return primitives by value, and everything else by pointer.  I&#39;m going to call anything a &quot;primitive&quot; if it has a valid copy constructor (boy would this be wrong in a large app).  Instead of pulling in &lt;a rel=&quot;external&quot; href=&quot;http://stlsoft.com&quot;&gt;Matthew Wilson&lt;/a&gt;&#39;s &lt;a rel=&quot;external&quot; href=&quot;http://www.synesis.com.au/software/stlsoft/doc-1.9/unionstlsoft_1_1must__be__pod.html&quot;&gt;&quot;must_be_pod&quot;&lt;/a&gt; meta-function-constraint, I think I&#39;ll just assume proper usage (like a fool).  &lt;strong&gt;Note: A maniacal penguin did show up at my door, but I was able to dissuade it with a tennis racket and tuna fish.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Ok, so onto the symbol loading...  Here&#39;s what our public symbol loading function looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;//function for external use&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; get_symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const char*const&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;  //Call appropriate function based on whether type is pointer or not.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;  typedef&lt;/span&gt;&lt;span&gt; get_symbol_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;is_ptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;value&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; get_symbol_functor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  get_symbol_functor&lt;/span&gt;&lt;span&gt; functor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //need an instance to use operator()...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; functor&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; isptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; get_symbol_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Well, that&#39;s simple.  Too bad it won&#39;t compile when someone actually tries to use it.  What&#39;s going on here?  Well the get symbol function is templatized on some type.  Then we try to call the &lt;code&gt;get_symbol_&lt;/code&gt; &lt;em&gt;functor struct&lt;/em&gt;.  What is a functor struct (besides a lame excuse for a closure/real functor)?  It&#39;s a structure that (sort of) looks like a function in code (as shown above).   We declare (but not define) the structure.  The structure is where we will specialize our implementation for pointer types vs. non-ptr types.  Since we&#39;re using the &lt;code&gt;is_ptr&lt;/code&gt; meta function above, all we need are two partial specialization for the &lt;code&gt;get_symbol_&lt;/code&gt; functor.  The first specialization will take &lt;code&gt;true_&lt;/code&gt; as its second type parameter. The second specialization will take &lt;code&gt;false_&lt;/code&gt; as its second type parameter.  The false variant should perform a copy of the value pointed at by the symbol.  The true variant can just cast and return the symbol pointer.  Here&#39;s the code:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //Repeated for clarity&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; isptr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; get_symbol_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; get_symbol_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;true_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;  T&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; operator&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const char*const&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; void*&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;   //This should fail to compile for types that don&amp;#39;t have copy-constructors&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   return&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; reinterpret_cast&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;get_symbol_internal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt; //Get symbol for non-ptr types&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;    struct&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; get_symbol_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; mpl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;false_&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;     T&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; operator&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;const char*const&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; void*&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;   //TODO - Fix const...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   return&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; *static_cast&amp;lt;&lt;/span&gt;&lt;span&gt;T&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;*&amp;gt;(&lt;/span&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;get_symbol_internal&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;name&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; handle&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So now you can see how a struct-functor is created.  Basically you just define an &lt;code&gt;operator()&lt;/code&gt; overload inside the structure, and it will start acting like a function (or at least looking like one...).&lt;/p&gt;
&lt;p&gt;You can see how on the one hand we&#39;re de-referencing the pointer, and on the other we&#39;re just casting.  I&#39;m also going to assume here (wrongly) that the get_symbol_internal will throw an exception on error, thereby ensuring that I will never dereference an invalid pointer (feel free to laugh, or send the penguin again).&lt;/p&gt;
&lt;p&gt;Ok, and now for the main method that uses this mess of code (note I&#39;m using &lt;code&gt;getopt.h&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; argc&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;argv&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;[]) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; extern char*&lt;/span&gt;&lt;span&gt; optarg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; extern int&lt;/span&gt;&lt;span&gt; optind&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; optopt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; string&lt;/span&gt;&lt;span&gt; library&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; string&lt;/span&gt;&lt;span&gt; symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; string&lt;/span&gt;&lt;span&gt; type&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = &amp;quot;&amp;quot;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; bool&lt;/span&gt;&lt;span&gt; usage&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = false;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; while&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; ((&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; getopt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;argc&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;,&lt;/span&gt;&lt;span&gt; argv&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;, &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;l:s:t:h&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;)) != -&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  switch&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; (&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;l&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   library&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; optarg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   break&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; optarg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   break&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   type&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; optarg&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   break&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   cerr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Unrecognized option: -&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt; static_cast&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; (&lt;/span&gt;&lt;span&gt;optopt&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;     &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  case&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;h&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   usage&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; = true;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;   break&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; (&lt;/span&gt;&lt;span&gt;usage&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #676E95;font-style: italic;&quot;&gt;  //TODO&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt; DynamicLib&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; lib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;library&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; == &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  cerr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Symbol value (as int) is: &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; lib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;get_symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt; (&lt;/span&gt;&lt;span&gt;symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;()) &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;(&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; == &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  cerr&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;lt;&amp;lt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;Symbol value (as return of function [int ()] ) is: &lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; lib&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span&gt;get_symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; (*)()&amp;gt; (&lt;/span&gt;&lt;span&gt;symbol&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;())() &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; endl&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; return&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The hardest one to understand here is possibly the function pointer.  Basically I&#39;m asking for a pointer to a no-argument function that returns an integer &lt;code&gt;int (*)()&lt;/code&gt;.  I then immediately call this function for a value to place in cerr.&lt;/p&gt;
&lt;p&gt;Ok, now we need a shared-library to test out.  Here&#39;s my dummy .cpp that I compiled into a .so (I leave that as an excersie to the user).&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt;extern&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;C&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;/span&gt;&lt;span&gt; MY_VAL&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F78C6C;&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #C792EA;&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt; MY_FUNC&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;() {&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt; return&lt;/span&gt;&lt;span&gt; MY_VAL&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Onto our outputs:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellsession&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;  Debug/cpp-types -l ../dummy-shared-lib/Debug/libdummy-shared-lib.so -s MY_VAL -t int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Symbol value (as int) is: 5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;  Debug/cpp-types -l ../dummy-shared-lib/Debug/libdummy-shared-lib.so -s MY_FUNC -t function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Symbol value (as return of function [int ()] ) is: 5&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, so this is a little bit limited, but we have it working! YAY!!!?&lt;/p&gt;
&lt;p&gt;In conclusion, I really want to get back into Scala again.  The type system seems a little bit less verbose, and the language itself is less clunky.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note: I later updated this to pull string-literals out of libraries.  The fun part there was realizing that a symbol pointer to  a string literal is &lt;em&gt;really&lt;/em&gt; a &lt;code&gt;[char**]&lt;/code&gt; that needs to be dereferenced (carefully).  Basically I wrote a specialized &lt;code&gt;std::string&lt;/code&gt; specialization of &lt;code&gt;get_symbol&lt;/code&gt; that would perform the checks/casts and return a &lt;code&gt;string&lt;/code&gt;.  The key to testing this was ensuring that the &lt;code&gt;char*&lt;/code&gt; literal would wind up in the symbol table of the output &lt;code&gt;.so&lt;/code&gt;.  This means you should make a dummy function that returns it, otherwise it may be optimized away.  Enjoy!&lt;/strong&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Ubuntu Dev. Server - Setting up hudson</title>
        <published>2008-08-21T00:00:00+00:00</published>
        <updated>2008-08-21T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/ubuntu-dev-server-hudson/"/>
        <id>https://jsuereth.com/posts/ubuntu-dev-server-hudson/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/ubuntu-dev-server-hudson/">&lt;p&gt;I&#39;ve decided to switch from using Continuum for Continuous Integration to Hudson.  This is based on a suggestion from a friend.  We&#39;ll see how things turn out.&lt;/p&gt;
&lt;p&gt;So first, I&#39;m going to list here (because I continually have to look it up) how to install Tomcat5.5 and Apache2 on Ubuntu server and wire the &lt;code&gt;mod_jk&lt;/code&gt; forwarding up.&lt;/p&gt;
&lt;p&gt;First, install &lt;code&gt;apache&lt;/code&gt;, &lt;code&gt;tomcat&lt;/code&gt; and &lt;code&gt;mod_jk&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; apt-get install apache2 tomcat5.5 libapache2-mod-jk&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next create a workers.properties file (mine is in &lt;code&gt;/etc/apache2/workers.properties&lt;/code&gt;).  Here&#39;s what mine looks like:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;worker.list=worker1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;worker.default.port=8009&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;worker.default.host=localhost&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;worker.default.type=ajp13&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;worker.default.lbfactor=1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, make sure there&#39;s a sym-link in &lt;code&gt;/etc/apache2/mods-enabled&lt;/code&gt; to &lt;code&gt;/etc/apache2/mods-available/jk.load&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now that we know mod-jk is loaded on apache startup, it&#39;s time to actually set up apache to forward appropriately.  I edited my &lt;code&gt;apache2.conf&lt;/code&gt; file (instead of &lt;code&gt;available-hosts/default&lt;/code&gt;).  Why?  No real reason.  In fact this should probably be moved into &lt;code&gt;available-hosts/default&lt;/code&gt;, but for now I&#39;ll just show you the relevant apache configuration to write in the worker you specified above:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Tomcat Configuration&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;JkWorkersFile /etc/apache2/workers.properties&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;JkMount /hudson/* worker1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that I&#39;m only mounting the hudson directory.  This means that I&#39;m only forwarding to tomcat for the tomcat application.  This is because I&#39;m using apache to host my subversion repository AND trac AND hudson (and archiva in the future).&lt;/p&gt;
&lt;p&gt;Finally, we have to turn off (or configure) java security for tomcat 5.5 in ubuntu.  I&#39;m turning it off because I&#39;m lazy and I don&#39;t plan to place my dev box outside my internal network.  The basic just to turn it off is to open your &lt;code&gt;/etc/init.d/tomcat5.5&lt;/code&gt; file and look for the following line: &lt;code&gt;TOMCAT5_SECURITY=yes&lt;/code&gt;.   Change the &quot;yes&quot; to &quot;no&quot; and you should be ready to go.  For more details check the link &lt;a rel=&quot;external&quot; href=&quot;http://www.nabble.com/Re%3A-java.lang.NoClassDefFoundError%3A-org-quartz-CronExpression-p13139289.html&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h3 id=&quot;installing-hudson&quot;&gt;Installing Hudson&lt;/h3&gt;
&lt;p&gt;First of, download the hudson war into tomcat&#39;s webapps directory -&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; wget http://hudson.gotdns.com/latest/hudson.war&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, set up a directory for hudson.  I made one in tomcat&#39;s directories:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /var/lib/tomcat5.5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; mkdir hudson&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; chown tomcat5.5 hudson&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, you need to add the following line somewhere in the beginning of &lt;code&gt;/etc/init.d/tomcat5.5&lt;/code&gt; -&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;JAVA_OPTS&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;=&amp;quot;${&lt;/span&gt;&lt;span&gt;JAVA_OPTS&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; -DHUDSON_HOME=/var/lib/tomcat5.5/hudson&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, restart tomcat and apache; Hudson should be working at &lt;code&gt;http://youraddress/hudson/&lt;/code&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setting up Apache and Subversion on Ubuntu</title>
        <published>2008-05-23T00:00:00+00:00</published>
        <updated>2008-05-23T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/setting-up-apache-and-subversion/"/>
        <id>https://jsuereth.com/posts/setting-up-apache-and-subversion/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/setting-up-apache-and-subversion/">&lt;p&gt;Now onto setting up SVN and apache.&lt;/p&gt;
&lt;p&gt;First off, make sure libapache2-svn is installed.  Now to the configuration in &lt;code&gt;/etc/apache2/mods-available/dav_svn.conf&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Mine looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;Location /svn/project&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  DAV svn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  SVNPath /path/to/my/project/svn&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  AuthType Basic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  AuthName &amp;quot;Subversion Repository&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  AuthUserFile /etc/apache2/dav_svn.passwd&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;LimitExcept GET PROPFIND OPTIONS REPORT&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Require valid-user&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &amp;lt;/LimitExcept&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;lt;/Location&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: I&#39;m doing something stupid here.  I&#39;m create &lt;em&gt;ONE&lt;/em&gt; repository for my project as opposed to using &lt;code&gt;SVNParentPath&lt;/code&gt; where you could have multiple repositories later.  Yes, this is just for one project.  We&#39;ll go back and fix later.  (It&#39;s a virtual machine remember!)&lt;/p&gt;
&lt;p&gt;Next, create a password:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;htpasswd2&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; -c /etc/apache2/dav_svn.passwd josh&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, onto our SVN repository.  Let&#39;s give apache access (while we&#39;re at it, let&#39;s fix the group from last time too)&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; chown www-data:www-data -R /projects&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, now to throw some random thing in the repostiory and make sure you can see it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; su www-data&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #82AAFF;&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /some/place/safe&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; tmp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;touch&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; tmp/README&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;svn&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; import /some/place/safe/tmp file:///path/to/my/project/svn&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alright... now we restart apache (&lt;code&gt;/etc/init.d/apache2 restart&lt;/code&gt;) and it should be up and running!  if you&#39;re like me shoot over to &lt;code&gt;http://localhost:8888/svn/project&lt;/code&gt; and you should see your SVN repostiory.  (you should also make sure TRAC sees it).&lt;/p&gt;
&lt;p&gt;Well, I think we accomplished something here!  Now (if your box is on the net) you can start adding code and tracking changes.  Unfortunately we have a little bit more to do before I&#39;m ready to start my project, and that little bit is the maven infrastructure.&lt;/p&gt;
&lt;h4 id=&quot;side-note&quot;&gt;Side note:&lt;/h4&gt;
&lt;p&gt;I was hoping Trac could manage our passwords for SVN (and create new repositories), but it looks like that&#39;s a no.  Oh well, once again we&#39;ll see how annoying it gets.  If it gets too annoying, I&#39;ll make an extension.  I&#39;m hoping to find an alternative to gforge that I can host internally to a company.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Setting up an Ubuntu Development Server</title>
        <published>2008-05-22T00:00:00+00:00</published>
        <updated>2008-05-22T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jsuereth.com/posts/setting-up-ubuntu-dev-server/"/>
        <id>https://jsuereth.com/posts/setting-up-ubuntu-dev-server/</id>
        
        <content type="html" xml:base="https://jsuereth.com/posts/setting-up-ubuntu-dev-server/">&lt;p&gt;Well, we&#39;re off to a rocky start.  First off, the Ubuntu server kernel expects a particular processor extension that does not exist in the virtualbox emulation:  &lt;a rel=&quot;external&quot; href=&quot;https://bugs.launchpad.net/ubuntu/+source/linux/+bug/216784&quot;&gt;see here for a fix&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, after a little shell magic, we&#39;re up and running.  NOW for software.  You need to apt-get the following (not exhaustive) list:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;trac apache2 libapache2-mod-wsgi svn python python-pysqlite2 openssl maven ant tomcat5.5 libapache2-mod-jk&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, to test out some of said software.  First, make sure you&#39;re using some reasonable networking scheme.  If you&#39;re lame like me and used NAT because you didn&#39;t want another interface just now (or you really didn&#39;t know what you were doing at the time), the Help for virtual box provides you a method of doing port forwarding from the host to the client.  You need to restart the virtual-box GUI after you&#39;re done.&lt;/p&gt;
&lt;h4 id=&quot;creating-a-directory-for-the-project&quot;&gt;Creating a directory for the project&lt;/h4&gt;
&lt;p&gt;Run the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /path/to/my/project&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4 id=&quot;setting-up-subversion&quot;&gt;Setting up Subversion&lt;/h4&gt;
&lt;p&gt;Run the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;svnadmin&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; create /path/to/my/project/svn&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Obviously replacing the path.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;There&#39;s some more magic we have to do here eventually (and integrate in with Apache).  I&#39;m going to cover this later, as I&#39;m running out of steam.&lt;/em&gt;&lt;/p&gt;
&lt;h4 id=&quot;installing-trac&quot;&gt;Installing Trac&lt;/h4&gt;
&lt;p&gt;you need to set up a trac project:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;mkdir&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /path/to/my/project/trac&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;trac-admin&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; /path/to/my/project/trac initenv&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Enter a project name.  I took all defaults except for Subversion location (&lt;code&gt;/path/to/my/project/svn&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;You then need to make a file for the WSGI module, mine looks like the following:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;import&lt;/span&gt;&lt;span&gt; os&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;os&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;environ&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;[&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;TRAC_ENV&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;] = &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;/path/to/my/project/trac&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;os&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;environ&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;[&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;PYTHON_EGG_CACHE&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;] = &amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt;/path/to/my/project/trac/eggs&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89DDFF;font-style: italic;&quot;&gt;import&lt;/span&gt;&lt;span&gt; trac&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;web&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;main&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;application&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt; =&lt;/span&gt;&lt;span&gt; trac&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;web&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #89DDFF;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F07178;&quot;&gt;dispatch_request&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My file is called &lt;code&gt;main.wsgi&lt;/code&gt; and is located in &lt;code&gt;/path/to/my/project/trac&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id=&quot;apache-configuration&quot;&gt;Apache Configuration&lt;/h4&gt;
&lt;p&gt;Now it&#39;s time to edit apache configuration (I&#39;m lazy soo....)&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;sudo&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; vi /etc/apache2/sites-available/default&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Ok, inside here we need to change the app to be our TRAC project...&lt;/p&gt;
&lt;p&gt;THEN we need to give apache ownership of the TRAC files...&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #BABED8; background-color: #292D3E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FFCB6B;&quot;&gt;chown&lt;/span&gt;&lt;span style=&quot;color: #C3E88D;&quot;&gt; -R www-data /path/to/my/project/trac&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You now have Trac running on Apache (with an SVN repository).&lt;/p&gt;
&lt;h4 id=&quot;trac-plugins&quot;&gt;Trac Plugins&lt;/h4&gt;
&lt;p&gt;Of course no software distribution is complete without the mandatory downloading of plugins.  Trac is no exception.  Let&#39;s pick out some fun ones:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://dev.rectang.com/projects/continutrac&quot;&gt;ContinuTrac&lt;/a&gt; - Continuum integration.  We&#39;ll work with this &lt;em&gt;after&lt;/em&gt; we install continuum on tomcat.&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://trac-hacks.org/wiki/GraphvizPlugin%22&quot;&gt;GraphViz&lt;/a&gt; - Yes... I can make intersting graph-like structures that will be pretty and amaze people from 1980! (Actually I &lt;em&gt;love&lt;/em&gt; graphviz wiki plugins)&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://trac.edgewall.org/wiki/WebAdmin&quot;&gt;WebAdmin&lt;/a&gt; - A necessary web admin console.  Has a lot of plugins into &lt;em&gt;it&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://trac-hacks.org/wiki/TagsPlugin&quot;&gt;TagsPlugin&lt;/a&gt; - Folksonomy, or Release Tagging or Neither... you figure it out&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://trac-hacks.org/wiki/ScreenshotsPlugin&quot;&gt;ScreenshotsPlugin&lt;/a&gt; - Can has Bling Pix?  Kthx&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http://trac-hacks.org/wiki/TracDownloaderPlugin&quot;&gt;TracDownloaderPlugin&lt;/a&gt; - For Actually releasing products for download.  (vs. libraries via Archiva)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So... if I ever get enough time I might place some script around all this to auto-generate the svn repostiory, trac directory and apache configuration, along with default installs of the plugins.  That way you can make new projects quickly and easily AND get all the nice management features (which is the major point of maven).&lt;/p&gt;
&lt;h4 id=&quot;so-what-did-we-get-this-episode&quot;&gt;So... What did we get this episode?&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Source Code Repository (At least visualizing. More work later for http/https access).&lt;/li&gt;
&lt;li&gt;Issue Tracker&lt;/li&gt;
&lt;li&gt;Project Wiki&lt;/li&gt;
&lt;li&gt;File Release System (with some plugin work)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Not bad eh?  If we add the &quot;magic project creation script&quot; with a web front end, we might even have something to fight sourceforge with.  But onto the &lt;em&gt;real&lt;/em&gt; meat of this server... the nice Maven integration.&lt;/p&gt;
</content>
        
    </entry>
</feed>
