I intend to start writing about Scala, a fairly new (circa 2003) programming language for the Java Virtual Machine. It tries to unify object-oriented and functional programming; it is multi-paradigm. Why trying to unify those two is worth doing would take a while to discuss, but the practical result is that you can do object-oriented programming when you want to but you’re always free to use any functional constructs that you like, such as map, filter, and the other usual suspects or their analogues that you might be familiar with from Ruby, Smalltalk, Haskell/OCaml, Lisp or what have you. And actually if you like any of those languages (or if you just use Java! Scala is similar to Java in a lot of ways, really, gimme a chance!) you should take a look at Scala. Scala is quite stable and worth using, so I’ll try to discuss a little bit more about it below. I’ll make the disclaimer now that I’m still learning myself but hopefully you’ll consider what I have to say just the same.
Scala runs on the Java Virtual Machine, so it can transparently use Java code, classes, and libraries. No foreign function interface gunk, no nothing. It’s effortless, and I’ll show an example in the next post of using java.util.regex to do simple regular expression stuff.
For now, let’s answer a simple question: what does Hello World look like in Scala? Well, here’s how it looks:
object HelloWorld {
def main(args: Array[String]) =
Console.println("Hello, world!")
}
OK, that’s not too bad. How do you install Scala? Well, first you’ll need either the JRE or the JDK installed and set up; head over to Sun’s website and git ‘er done. If you can compile HelloWorld.java for Java you should be OK. Alright, go ahead and download Scala for your platform (I’m on Windows but it should work on anything the JVM runs on). So, after you add the Scala bin directory to your path, you can compile and run the above Scala code by doing:
> scalac HelloWorld.scala
> scala HelloWorld
Hello, world!
>
You don’t have to use CamelCase, by the way. You could call everything helloworld and Scala just won’t give a damn.
Well, hokai, you’ve seen how to do a Hello World in Scala. It’s a little smaller than Java’s, huh? I’ll reprint it again just for convenience.
object HelloWorld {
def main(args: Array[String]) =
Console.println("Hello, world!")
}
So, the object keyword gives us a singleton (yeah, that pattern becomes invisible in Scala). We don’t need to declare anything static ’cause, well, just ’cause for now. The main method takes an array of strings as a parameter, just like in Java. Console is another convenience object that has stuff for reading from and printing to the console. In Scala, you declare things like var: type rather than type var like in C, C++, Java and the like. Hopefully it doesn’t look too foreign.
But here’s the cool thing: in larger programs, Scala can infer the types of a lot of stuff (it has type inference in the finest tradition of Haskell and O’Caml). Like, when you add two integers, it knows the result is an integer; and actually in our humble Hello World program Scala inferred that your program returned a unit, Scala’s version of void (it’s like O’Caml’s unit, if you’ve used that). So when you declare a variable in a function, you probably won’t have to supply type information. That winds up saving you a lot of typing. So you get brevity while still being static and typesafe. Oh, yeah, Scala is statically typed. But you get a REPL! But when you compile to native code, it’s still pretty fast. And you don’t have to give up using Eclipse/Ant/Maven/other Java tools you might be familiar with… Oh dear, I’m getting ahead of myself. Well, this is probably a good stopping point before I start rambling about all the neato crap in Scala, so stay tuned…
Ah christ, I can’t resist. You might head over to Scala’s website, but I think it’s pretty confusing and ugly, so try to not let it scare you away. Look in a subdirectory of your Scala installation (scala_dir/misc/scala-tool-support, where scala_dir is your Scala installation directory) for editor support (IntelliJ, Vim, Emacs support and so on), and there’s also a directory that has helpful documents which are at this point flawed, so don’t let that scare you either; I’m your helpful guide on the road to Scala. OK, hopefully you think Scala is worth spending a little more time on. Till next time.
[...] What is Scala, and why you should care « Metacircular thoughts Looks like one of my frequent IM buddies — and full-hog “little language” nut
— finally started his blog with this post on Scala. Lookin’ forward it more! (tags: scala programming wkh java littlelanguages) [...]
Pingback by People Over Process » Blog Archive » links for 2007-01-16 — January 15, 2007 @ 11:29 pm |