Scala release





The final release of Scala 2.7.3 is out! You can download the release from the scala-download page.

Some background: Scala is a elegant language that runs on the JavaVM. Unlike languages like Ruby and Groovy, Scala is statically typed. One of the reasons a lot of people like dynamic languages more than Java, is because Java is very verbose. That verbosity is, amongst others, because of its type-system.
However, unlike Java the typing-system is much more elegant and flexible. Because Scala can infer types, you’ll have to define types only when necessary. Look for example at the following code (taken from the Scala website):

object Maps {
  val colors = Map("red" -> 0xFF0000,
                   "turquoise" -> 0x00FFFF,
                   "black" -> 0xFFFFFF,
                   "orange" -> 0xFF8040,
                   "brown" -> 0x804000)

  def main(args: Array[String]) {
    for (name <- args) println(
      colors.get(name) match {
        case Some(code) =>
          name + " has code: " + code
        case None =>
          "Unknown color: " + name
      }
    )
  }
}

There are no explicit type definitions, eventhough every defined value is statically typed. The scala compiler can infer that the val colors should have type scala.collection.immutable.Map[java.lang.String,Int]. Compare that to Java, where you’d  have to use Map<String,Integer> several times.
Minor sidenote: there are languages that have an even more advanced typing system, see, this nice article.

Furthermore, another what’s very much to like about new dynamic languages is it’s extendibily. You can extent a language like Ruby or Groovy, and create a new internal language.  Eventhough Scala is statically typed, in Scala this is possible as well.
Let’s say you want a sort method for the general Array type. This can be accomplished with the following code:

/* Defines a new method 'sort' for array objects */
object implicits extends Application {
  implicit def arrayWrapper[A](x: Array[A]) =
    new {
      def sort(p: (A, A) => Boolean) = {
        util.Sorting.stableSort(x, p); x
      }
    }
  val x = Array(2, 3, 1, 4)
  println("x = "+ x.sort((x: Int, y: Int) => x < y))
}

There’s a lot more exciting, as a last example, I put a code-snippet that demonstrates how XML is integrated in Scala:

  val header =
    <head>
      <title>
        { "My Address Book" }
      </title>
    </head>;

  val people = new AddressBook(
    Person("Tom", 20),
    Person("Bob", 22),
    Person("James", 19));

  val page =
    <html>
      { header }
      <body>
       { people.toXHTML }
      </body>
    </html>;

Finally, an example of a quicksort taken from this sample website. Impressive, but I am to much unexperienced in Scala to grasp that code immediately:

  def sort[A <% Ordered[A]](xs: List[A]): List[A] = {
    xs match {
        case List() => xs
        case _ =>  sort(for(item <- xs.tail if item < xs.head) yield item) ::: List(xs.head) ::: sort(for(item <- xs.tail if item >= xs.head) yield item)
    }
  }

Below a book about Scala I recently read




Sr Software Consultant

Geplaatst in Technology Getagd met

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

*

De volgende HTML-tags en -attributen zijn toegestaan: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Google

Bol.com