No powerful meta-programming capabilities
Biggest bummer - Scala doesn't seem to have any serious meta-programming capabilities that I could find. Nothing like Groovy and definitely nothing like Ruby. However, two approaches to extending the language are given on the website:
- any method which takes a single argument may be used as an infix or postfix operator
- closures are constructed automatically depending on the expected type (target typing)
var result = x or y
which is rather more readable than the Java equivalent, boolean result = x.or(y);
As you've probably guessed,
or
is a method defined on a user defined class of which both x
and y
are instances.The second option allows us to pass a block to a method in a syntactically clean way. Here's an example from the Scala website.
object TargetTest1 extends Application {
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
if (cond) {
body
whileLoop(cond)(body)
}
var i = 10
whileLoop (i > 0) {
println(i)
i -= 1
}
}
What we're interested in is the definition of the
whileLoop
construct and its usage. Note that (body: => Unit)
allows the whileLoop
method to be invoked with a parameterless function as the second argument. In the usage, this is the bit that's encased in curly braces right after whileLoop (i > 0)
. Pretty nifty. If Java supported blocks using curly braces, the method call would look like this: whileLoop((i > 0), {System.out.println(i); i-=1;})
This makes it possible to construct DSLs which are considerably more readable than those written in, say, Java or C#, because we can get rid of most of the comma/semi-colon noise as well as invoke methods in a eminently readable fashion.
There is also a section on the Scala wiki titled 'future:metaprogramming'. At this time, there are eight sub-sections - Definitions, Design Goals, Requirements, Constraints, Proposals, Examples from other languages, Research Papers and Discussion Threads. Only the last three links lead to any content, so I'm figuring that open classes, eval() and such-like can only be expected in the future.
In the course of my research, I found references to a DSL written in Scala here and discussions on achieving meta-programming on lang.scala here and here.
Functioning package repository
Scala has Scala Bazaar (a.k.a sbaz) which seems to function in a manner similar to RubyGems and Perl's CPAN. However, the list of available packages, at 243, is quite small.
Heavy emphasis on Java
Most of the libraries used for development in Scala are Java libraries. I found little written in pure Scala. It seems the two years of JVM-only development has caused some biasing - if the .Net Scala compiler had kept up with the JVM version, ther'd be a lot more pure Scala stuff available. Something I missed sorely was a pure Scala build tool, something like Ruby's rake. lift for example uses Maven. A lot of the thinking in these areas seems to be from primarily Java people - I came across a project to create a build tool in Scala, but it was based on Ant.
That's it from me on Scala thus far - more as it surfaces.
You may also want to read my previous post on why I'm messing around with Scala and lift: Scala, Lift and being cussed
4 comments:
it's true about the [lack of] metaprogramming. there's two old ruminations on the mailing list, but nothing concrete. you can write compiler plugins that manipulate the tree at any pass, but of course that's going to be a project in and of itself. its type system is nice, but mainly a compile time thing. at runtime, it's constrained by jvm type erasure
(i believe dotnet support was dropped when the compiler was rewritten, and the dotnet support in the new compiler is relatively recent)
-mind
you say it has a heavy emphasis on java as if it's a *bad* thing. there's nothing that precludes libraries and tools written natively in scala. however, given that scala is such a new language, bootstrapping off of existing java libraries and tools gives scala a lot of stuff "out of the box" that it wouldn't otherwise have. (remember, ruby has ~8years on scala.)
as for metaprogramming: you are correct. but in practice, it turns out you don't need metaprogramming as often as you think...
--j
It's worth noting that Java libraries are often seem easier to use in Scala than in Java-- implicit defs, methods as (partially applied) functions etc.
I find that almost anything I would have built with Ruby's meta-programming functionality can be built in Scala by combining Traits and Scala's Type system.
Plus, with Scala's way of doing things, there's a lot more "obvious" code than some class or instance where a method has been changed out from under you.
Post a Comment