Sunday, December 30, 2012

What makes a language?

I've been thinking a bit about what makes a computer language. None of these thoughts are probably original, I only think they're worth writing down for me because I haven't picked them up from elsewhere. As I try to learn to use scala (not just write a simple program in it) this issue is substantially in the fore.

So here goes.

A language seems to have at least three important aspects: the language syntax itself, the environment (hardware/virtual machine/operating system) where programs in the language will execute, and the cultural norms built around the language. Each of these have a significant impact on the final utility of the language. And it isn't clear if any one of these has primacy over the others. I'm not going to dwell on any of these aspects, only present them through example.

For learning scala I've chosen to implement a simple program that merges data from two CSV files and then performs some calculations. The program's simple enough that, had I just wanted to solve this problem, I would have proceeded through trial and error to complete the program, using whatever tutorials are available out there regarding the language itself. In doing so, I would only have learned a bit about the language. I would have experienced the impact of the environment only minimally. I don't think one really experiences that until one implements a truly substantial piece of software. And I would have learned very little about the language ecosystem. I could implement such a project entirely through eclipse and the scala plugin.

Put another way, this would be a depth first approach to learning a language: learn one aspect of the programming language really well. Then backtrack, and learn another piece, perhaps re-implementing the original program with the new found knowledge.

Another way to approach learning a language, which I think is much more effective for an experienced programmer, is much more akin to best first search. I've written substantial code in Java, enough to know reasonably good programming practices. My goal is to produce scala programs of comparable quality. To do so, I have to find the parts of the scala language and ecosystem that will let me develop a program that's comparable in quality to my java programs.

If the goal is to practice test driven development, for example, I might learn enough of scala and ScalaTest to write a failing test case. And I'd learn to write and execute the test cases through eclipse. I'd also want a build tool that doesn't rely on my IDE. In the case of scala that would be sbt. These few tools would give me sufficient scaffolding to learn more of the language. And so on. My goal after all is to not just learn to program in scala, but learn to produce a high quality program using scala.

Tuesday, December 11, 2012

Scala + Eclipse + sbt

As much as I'd like to use emacs for scala, I'm wary of using packages for a fast changing language that haven't seen any recent changes. As far as I can tell, the emacs package for scala hasn't been updated since some time in 2011. And we're nearly at 2013.

So, eclipse it is.

The next problem was getting emacs to recognize and work with a scala project correctly. I like my emacs+ extension, so I haven't updated eclipse from indigo to juno. I installed the scala extension for eclipse. I installed sbt. And I set up a simple hello world application following the sbt tutorial.

I then installed sbteclipse in sbt, and generated an sbt project. The resultant project had a funny name with the source file in the wrong place. Apparently the scala ide for eclipse doesn't like source files in the package base.

To get a nicer looking project you need a build.sbt in your project. This seems to be the minimum:

name := "hello-world"

Also put a package declaration in your hello world program, and have the source file reside in the corresponding directory.

I've also found manually creating an eclipse run configuration doesn't function correctly. Eclipse isn't able to find candidate main methods automatically, as it does for Java programs. And then I found the easy way: with your main file open, click on the run command. Voila!

To find my way around I've found a simple github project to use as a guide. The language itself seems to have adequate tutorials to get started, but understanding the scala ecosystem is taking some time and effort.

Starting with Scala

I'm currently starting with Scala development. I'm sad I didn't make enough headway with Clojure. The tutorial is a pretty good overview of the potential power of the language, and I'm looking forward to diving in and putting together a little project. Here's what I have so far:
  1. Install scala.
  2. Play with the scala repl.
  3. Search online for a csv library, and discover an opencsv wrapper.
  4. Learn I know nothing about the library distribution culture around scala, and find something called sbt.
  5. Realize that scala development is equally feasible in emacs and eclipse.
So, emacs or eclipse? Given the portability and transparency of an emacs based development environment, I am strongly leaning in that direction. The downsides? Configuration is more complicated. And eclipse has really nice support for java development, which I've never tried with emacs. In theory sbt should make switching between the two environments relatively. Maybe I'll give both of them a spin.