Tuesday, July 23, 2013

OSCON Introduction to Scala

The official Introduction to Scala page on the OSCON site.

Dianne Marsh (Netflix), Bruce Eckel (Mindview, LLC)
9:00am Monday, 07/22/2013

Great presentation by Bruce and Dianne. They covered a lot of material in an easy to digest manner. After each nugget of information they had a great set of exercises that stretched the mind well beyond what they had explained. i.e. explain a concept and the accompanying syntax and then ask you to apply it to a typical real world problem.

(They call the nuggets atoms because it's the smallest amount of information you can explain about a corner of Scala without it being overwhelming and still being complete. I believe that this is the format that their book has followed.)

This was the first time that I've ever seen Scala and my immediate impression was that this is a language that is building on Java to play catchup with C#. Due to the lack of features being added to Java over the years C# had moved ahead with a great set of new features and to address this on the JVM Scala took up the challenge. Remember that I've spent all of 3 hours on Scala so this could be completely wrong.

 My notes on what I've picked up so far.

All objects are either a val or var. Vals are called const or constant in most other languages and are immutable. A var is variable that can be changed. Val and var declaration take data types as optional parameters otherwise the type is inferred.

val myNumber:Int = 1
val myFloat:Double = 1.1
val myBool:Boolean = false
val myWord:String = "A string"
val myLines:String = """Triple quotes let you have quotes" and
multiple lines under a another trifecta of quotes"""

Use semicolons to put multiple expressions on the same line:
var age=42; val color="blue"

Unit is the same as void in most C based languages or Nothing in Visual Basic.

The last line of code executed inside curly braces will be the value assigned to the variable, for example:

val area = {
     val pi = 3.14
     val radius = 5
     pi * pi * radius

Comments are the same as Java and other modern C languages: // and /* comment */

If statements are the same as Java:
if(x) {
if(!x) { // if x is not true

A difference with Scala is that the result of an if (and else) statement evaluation can be assigned to a val or var.

val result = {
    if(99 > 100) { 4 }
    else { 42 }

In Java/C# this would be:
int result = 99 > 100 ? 4 : 42;

Boolean && and || are as you'd expect.


No comments:

Post a Comment