Introduction to Scala

Posted by rob on July 31, 2009

I gave an Introduction to Scala talk yesterday for the software team I am a member of. I have included here the slides and code examples used in the presentation.

Presentation

Code Examples

Hello World

package org.breidecker.scalaexamples

object HelloWorld {
def main(args : Array[String]) : Unit = {
println("Hello World!")
}
}

// Notes:
// 1. main method is required to run
// 2. ": Unit =" is optional
// 3. Unit in Scala's is similar to Java's void



Hello World #2

package org.breidecker.scalaexamples

object HelloWorld2 extends Application {
println("Hello World!")
}

// Notes:
// 1. Application provides the main method
// 2. The println statement is in the object's constructor



Accessors

package org.breidecker.scalaexamples

object Accessors {
def main(args : Array[String]) {
val person = new Person;
person.firstName = ""   // Try different values here
person.lastName = "Smith"
println("Hello " + person.fullName + ".")
}

class Person {
private var theFirstName = ""
var lastName = ""

/* Overide the first name getter. */
def firstName = theFirstName.toUpperCase

/* Override the first name setter. */
def firstName_=(firstName : String) {
if (firstName != null && !firstName.trim.isEmpty) {
theFirstName = firstName
} else {
throw new IllegalArgumentException("First Name must contain a value.")
}
}

def fullName() : String = (firstName + " " + lastName).trim
}
}

// Notes:
// 1. The firstName property on the Person class is providing accessor method for the property
// 2. The instance variable for firstName had to be renamed to avoid a name conflict with the getter method
// 3. The lastName property is being referenced in the main method  with its default accessors provided by Scala



List Comprehension

package org.breidecker.scalaexamples

object ListComprehension {
def main(args : Array[String]) {
for (val color <- Colors.ALL_COLORS) {
println(color.name)
}
}

/* This is an immutable class */
class Color(newName : String) {
val name = newName
}

/* This is a Scala singleton object */
object Colors {
/* These are Scala constants. */
val blue = new Color("blue")
val green = new Color("green")
val red = new Color("red")
val yellow = new Color("yellow")
val ALL_COLORS = List(blue, green, red, yellow)
}
}

// Notes:
// 1. This example simply shows how to iterate over a list of values in Scala



Twitter Client

package org.breidecker.scalaexamples

import java.net._
import scala.xml._

object TwitterClient {
def main(args: Array[String]) {
val screenName = "robbr"    // Follow me! Try another Twitter name
val url =
new URL("http://twitter.com/users/show.xml?screen_name=" +
screenName)
val conn = url.openConnection
val xml = XML.load(conn.getInputStream)
val status = (xml\"status"\"text").text
println(screenName + ": " + status)
}
}

// Notes:
// 1. Scala uses an underscore instead of an asterisk for its package wildcard character
// 2. Java's network package is imported
// 3. Scala's built-in XML library is used
// 4. This example makes a URL request to Twitter for the current screen name
// 5. It then uses an XQuery like statement to reference status text

The Strange Loop

Posted by rob on July 29, 2009

Alex Miller just announced most of the sessions for The Strange Loop developer conference to be held in St. Louis, MO on October 23, 2009.I will be attending as well as a number of people I work with. I’m expecting a big turnout, as there is a lot of buzz in St. Louis right now about the conference. And there should be. The list of sessions and speakers is quite impressive.

The Strange Loop