Scala Language
- Compiles to bytecodes
- Access to Java ecosystem
- Functional principles
- But with object-oriented concepts
- Developed by Martin Odersky (Pizza, GJ Languages)
- Scalable
Why Scala?
- Compatible
- Concise
- High Level
- Statically Typed
- Create highly scalable, concurrent data processors
- Take advantage of parallel hardware architectures
The Essence of Scala
- Concentrate on scalablity: same constructions for small or large programs
- Unify object-oriented and functional programming
Scalable
- For scripting, applications, and enterprise applications
- New types, ie: BigInt
- New control structures, ie: Actors
- Object-oriented
- Functional
From Java (and C#)
- Syntax
- Expressions, statements, blocks as in Java
- Classes, packages and imports
- Basic types
- Class library
- Execution model
From Other Languages
- Uniform object model from Smalltalk (and then Ruby)
- Universal nesting from Algol, Simula, ...
- Uniform access principle for method invocation and field selection from Eiffel
- Functional programming from ML family of languages (SML, OCaml, F#...)
- Higher-order functions from ML, Haskell
- Implicit parameters from Haskell's type classes
- Actors inspired by Erlang
## Hello world
```scala
object HelloWorld {
def main(args: Array[String]) = {
println("Hello, World")
}
}```
## Our tools
```
scalac HelloWorld.scala
```
```
scalac -print HelloWorld.scala
```
```
scalap HelloWorld
scalap HelloWorld$
```
```
javap -c HelloWorld
javap -c HelloWorld$
```
## Integer Factorial
```scala
object IntFactorial {
def factorial(x: Int): Int =
if (x == 0) 1 else x * factorial(x - 1)
def main(args: Array[String]): Unit =
println(factorial(10))
}
```
## Big Integer Factorial
```scala
object Factorial {
def factorial(x: BigInt): BigInt =
if (x == 0) 1 else x * factorial(x - 1)
def main(args: Array[String]): Unit =
println(factorial(30))
}
```
## Timer.scala
```scala
object Timer
{
def oncePerSecond(callback: () => Unit): Unit = {
while (true)
{
callback()
Thread.sleep(1000)
}
}
def timeFlies(): Unit =
{ Console.println("Time flies when you're having fun(ctionally)..."); }
def main(args: Array[String]): Unit = {
oncePerSecond(timeFlies)
}
}
```
## Our Application
```scala
object HelloApp extends App {
println("Hello, World")
}
```
Implemented with App.scala, DelayedInit.scala
## DelayedInit.scala
```scala
trait Helper extends DelayedInit {
def delayedInit(body: => Unit) = {
println("dummy text, printed before initialization of C")
body // evaluates the initialization code of C
}
}
```
## App.scala
```scala
private val initCode = new ListBuffer[() => Unit]
override def delayedInit(body: => Unit) {
initCode += (() => body)
}
def main(args: Array[String]) = {
this._args = args
for (proc <- initCode) proc()
if (util.Properties.propIsSet("scala.time")) {
val total = currentTime - executionStart
Console.println("[total " + total + "ms]")
}
}
```
## Defining a Trait
```scala
trait Similarity {
def isSimilar(x: Any): Boolean
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}
```
## Using a Trait
```scala
class Point(xc: Int, yc: Int) extends Similarity {
var x: Int = xc
var y: Int = yc
def isSimilar(obj: Any) =
obj.isInstanceOf[Point] &&
obj.asInstanceOf[Point].x == x
}
object TraitsTest extends Application {
val p1 = new Point(2, 3)
val p2 = new Point(2, 4)
val p3 = new Point(3, 3)
println(p1.isNotSimilar(p2))
println(p1.isNotSimilar(p3))
println(p1.isNotSimilar(2))
}
```
Actor Traits
- Reactor
- ReplyReactor
- Actor
## react
In Reactor.scala
```scala
// This method never returns
protected def react(handler: PartialFunction[Msg, Unit]): Nothing = {
synchronized { drainSendBuffer(mailbox) }
searchMailbox(mailbox, handler, false)
throw Actor.suspendException
}
// The $actor's behavior is specified by implementing this method.
def act(): Unit
```
## Factory method
In Actor.scala
```scala
def actor(body: => Unit): Actor = {
val a = new Actor {
def act() = body
override final val scheduler: IScheduler = parentScheduler
}
a.start()
a
}
```
THE END
BY Angel 'Java' Lopez / www.ajlopez.com / @ajlopez