C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

© 2006-2015 Andrew Cooke (site) / post authors (content).

Starting a Scala Project

From: andrew cooke <andrew@...>

Date: Sun, 27 Sep 2009 15:35:39 -0400

I just decided to do a quick + dirty project - I want to run through
my mp3s, read the tags, store everything in a database, and then pull
tags from the last.fm API so that I can automatically construct
playlists of related music.

I also decided the time has come to learn Scala.  I could do this
easily in Python, but I've been postponing Scala for too long.

which used sbt (Simple Build Tool).  I was thinking about looking at
ant+ivy (something else I need to learn), but I *think* sbt uses ivy
under the hood (or uses Maven, or all share the same basic
infrastructure).  So for now I am going with sbt.
yesterday).

So, first step was to create a new directory and run sbt.  It asked if
I wanted to create a new project and I said yes, and gave it a name.

OK, so that left me with some basic directories:

spu63 uykfd: ls
lib  project  src  target

OK, now how do I make that work with Eclipse?  No idea.
suggests using IvyDE (note - I have already installed the Scala plugin
in Eclipse).  So I install this via

reload + deliver-local in sbt (following instructions at

I still don't get this.  I think I need to create the Scala project
first in Eclipse, so move the directories generated with sbt above out
of the way, create the Scala project from within Eclipse, and then
move the sub-directories back.

But Eclipse is using Scala 2.7.6 and sbt is using 2.7.5

So delete those and in the project directory created by Eclipse run
sbt again, giving the correct Scala version.

That gives an error "unknown resolver sbt-chain" which I am going to
ignore, and otherwise seems to have worked.

F5 to refresh Eclipse and we seem to be cool.

Run reload and deliver-local again in sbt.  F5 again in Eclipse and

Next I create a HelloWorld class in the main.scala package, based on
http://www.scala-lang.org/node/166 but when I try to run it in Eclipse
it is treated as "HelloWorl" (no final "d") and so fails!

If I fix that in the run configurations I then get
Exception in thread "main" java.lang.NoSuchMethodError: main
which sucks somewhat.

This is not looking good.

Running via sbt gives something similar.  Does the "main" have to have
a special name?

I checked with the example and had "class" rather than "object";
fixing that doesn't seem to change anything in Eclipse, but now sbt
works:

> run main.scala.HelloWorld
[info]
[info] == copy-resources ==
[info] == copy-resources ==
[info]
[info] == compile ==
[info]   Source analysis: 1 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Compilation successful.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[info]
[info] == run ==
[info] Running main.scala.HelloWorld ...
hello world
[info] == run ==
[success] Successful.
[info]
[info] Total time: 2 s

Trying with the syntax at http://www.scala-lang.org/node/94 also works
with sbt, but not with Eclipse.

Looking at the comments for Application (via Eclipse F3) it seems it's
not recommended.  So revert to earlier syntax:

package main.scala

object HelloWorld {
def main(args: Array[String]) {
println("hello world")
}
}

Which seems identical to the example.

Andrew

Scala Bug Report

From: andrew cooke <andrew@...>

Date: Sun, 27 Sep 2009 16:01:55 -0400

https://lampsvn.epfl.ch/trac/scala/ticket/2402

Andrew

Trying Again (New Instructions)

From: andrew cooke <andrew@...>

Date: Sun, 27 Sep 2009 18:35:59 -0400

OK, as you can see via the bug report, it's not clear what went wrong,
but the following appears to work:

- First, install all software (sbt, Eclipse plugins for Scala and Ivy)
- Next, start Eclipse with a new workspace
- Create the project from inside Eclipse using the "new Scala project" wizard
- In the project directory, run sbt and create a new project with a
matching name (so that you end up with the "src" directory Eclipse
created matching the "src" directory sbt would also create)
- F5 in Eclipse to pick up new files
- At this point I created a HelloWorld object in main.scala, which
worked just fine
- run main.scala.HelloWorld in sbt also runs the hello world
- inside sbt, "reload" and "deliver-local" to generate the ivy file
- F5 again in Eclipse, then projects settings, Java build path,
libraries, and add the Ivy file (in the target directory)

The above creates a project that works both in Eclipse and sbt.

To add a dependency you need to open the ivy xml file using the
dedicated editor.  For example, right click on the ivy xml file in the
build part of the project explorer (NOT the file in target) and select
"edit".

The edit the dependency (the values seem to be as for Maven).  So, for
example, to include Empire DB -
<dependency org="org.apache.empire-db" name="empire-db"
rev="2.0.5-incubating"/>
and when I saved the file, it went off and downloaded that and a pile
of supporting libraries (not very quickly - I am in S America and the
net is not that fast, so perhaps that's the reason - but it did work).

Andrew

Scala in More Detail

From: andrew cooke <andrew@...>

Date: Sun, 27 Sep 2009 19:35:30 -0400

So now I'm at the point where I need to understand Scala more.  This
is a good overview of the main language features:
http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/files/ScalaOverview.pdf
(as I read it, I realised that some examples assume familiarity with
ideas from functional programming, so it may not be for everyone).

There's also more specific details (scroll down to the "table of
contents") on individual features here -
http://www.scala-lang.org/node/104

One thing that has already puzzled me is the use of "object", which is
illuminated by the first link: "In fact, every Java class is seen in
Scala as two entities, a class containing all dynamic members and a
singleton object, containing all static members."

There's a nice clear separation between Value and Reference types
(which in Python would help clarify things for newbies and in Java
helps unify equality).  And explicit Nothing and Null (reference)
types.

Oh and operators are methods (and vice versa!) with fixed precedence
(right associative if they end in a colon, which just happens to give
a sweet syntax for list consing!).  Parameterless methods are
possible.  And "override" is a modifier, not an annotation (ie it's
built-in).

Whoa.  Pass by name exists (lazy evaluation).  The syntax is a little
odd (a "=>" precedes the type).  Ah, because it's a think that "goes
to" a value, I guess.  Evaluated each time they are used (implemented
as parameterless function).

Mixins!  called traits.

I need to go cook dinner, but this is awesome :o)

Andrew

More on Scala

From: andrew cooke <andrew@...>

Date: Sun, 27 Sep 2009 21:06:34 -0400

Continuing with notes from
http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/files/ScalaOverview.pdf

Anonymous (using => again), curried and generally first class
functions.  And closures.  And partial evaluation:
forall(row, 0 ==)
page 6

Functions implemented via generic FunctionN classes.

Wow - arrays are treated as functions over integers (special handling
of assignment to functions via "update").

Sequences and list comprehensions.

Good discussion of co + contra variance.  Helps clarify some problems
I have had with generic types in Java.

Surprisingly, "new" still seems to exist.  I thought it was
unnecessary, so why keep it?

Enough type discussion to make my head explode.

Aha.  Traits must have empty constructor arguments.  That's a very
nice way to keep mixins clean.

The "case" modifier gives you constructor-like matching.

Native XML support including queries.

Hmmm.  Seem to have stalled at views.  Try again tomorrow.

Andrew

From: andrew cooke <andrew@...>

Date: Mon, 28 Sep 2009 09:09:43 -0400

The notes above on adding dependencies are incorrect.  The problem is
that the ivy file is generated by sbt.  So extending it is not
permanent.  It exists only as a mechanism to communicate from sbt to
Eclipse.  The actual configuration of dependencies is done in sbt.

It turns out that sbt is configured by writing a scala class:

- Under the project directory add a "build" directory
- In the build directory create a class named after your project (in
my case uykfd):

import sbt._

class uykfdProject(info: ProjectInfo) extends DefaultProject(info)
{
lazy val hi = task { println("Hello World"); None }
val empire_db = "org.apache.empire-db" % "empire-db" % "2.0.5-incubating"
}

- The "hi" above defines an action, so inside sbt you can type "hi"
and it will print "Hello World" (pointless, but a good test that
things are working)
- The "empire_db" defines a dependency.  The name of the value doesn't
matter and the various strings are as above (ie as Maven).
- If sbt is still running you need to "reload" to pick this up
- After that "compile" should work.  For example, I altered my test class to be:

package main.scala

import org.apache.empire.db.DBDatabase

object HelloWorld extends Application {
println("boo")
object db extends DBDatabase {
}
}

- To get this working in Eclipse you then use the ivy xml file, so
"deliver-local" and then click around in Eclipse to get the ivy.xml
file to be added to the build path
- It's not at all clear, but you can also delete the ivy build path
thing via the Eclipse build path dialog (under libraries).
- Once that is done, the libraries downloaded by sbt/ivy appear in the
Eclipse project too.

Andrew

Switched to Emacs

From: andrew cooke <andrew@...>

Date: Tue, 6 Oct 2009 09:02:43 -0400

This page is getting a few hits, so I should add that I have given up
on Eclipse and switched to emacs (for Scala - I still use Eclipse for
Python and Java). So much was broken that emacs does the same and uses
much less memory on my laptop.

To set up emacs follow the instructions at http://scala.sygneca.com/tools/emacs

Despite that having an sbt window, I use sbt in a separate console
(the emacs window shows some strange control characaters and I need to
exit sbt regularly to kill the in-memory database I am using).

In short, Scala tooling is not that great, still.  The language itself
appears to be fine, however.

Andrew