Scala

From no name for this wiki
Jump to: navigation, search

Meine Scala Notizen

Konsolentools

scala Konsole

scala Konsole aufstarten:

scala

scala compiler

Scala Compiler:

scalac WorldlyApp.scala WorldlyGreeter.scala

Scala Compiler Daemon:

fsc WorldlyApp.scala WorldlyGreeter.scala

Ausführen kann man Programme wie folgt:

scala classpath . HelloWorld

var und val

Wichtige Keywords:

  • var: Variable, wie ref.
  • val: Value, nicht mutierbar.

Methoden

Methode definieren:

 scala> def max(x: Int, y: Int): Int = if (x < y) y else x
 max: (Int,Int)Int
 
 scala> max(4,55)
 res5: Int = 55
 
 scala>

singletons

WorldlyGreeter ist als Klasse und Object definiert. worldify ist statisch (im Bytecode).

// The WorldlyGreeter class
class WorldlyGreeter(greeting: String) {
  def greet() = {
    val worldlyGreeting = WorldlyGreeter.worldify(greeting)
    println(worldlyGreeting)
  }
}
 
// The WorldlyGreeter companion object
object WorldlyGreeter {
  def worldify(s: String) = s + ", world!"
}

var x = new WorldlyGreeter("Hello testmaster.ch") x.greet()

imports

  • _: Ist analog zu Javas *
  • {Date, Locale}: Mehrere Klassen werden auf einer Zeile importiert.
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
 
object FrenchDate {
  def main(args: Array[String]) {
    val now = new Date
    val df = getDateInstance(LONG, Locale.FRANCE)
    println(df format now)
  }
}

Closures, Delegates

Uint ist wie void.

object Timer {
  def oncePerSecond(callback: () => Unit) {
    while (true) { callback(); Thread sleep 1000 }
  }
  def timeFlies() {
    println("time flies like an arrow...")
  }
  def main(args: Array[String]) {
    oncePerSecond(timeFlies)
  }
}

Anonyme Methoden:

object TimerAnonymous {
  def oncePerSecond(callback: () => Unit) {
    while (true) { callback(); Thread sleep 1000 }
  }
  def main(args: Array[String]) {
    oncePerSecond(() =>
    println("time flies like an arrow..."))
  }
}

traits, mixins

simple

trait Friendly {
  def greet() = "Hi"
}
 
class Dog extends Friendly {
  override def greet() = "Woof"
}
 
class HungryCat extends Friendly {
  override def greet() = "Meow"
}
 
class HungryDog extends Dog {
  override def greet() = "I'd like to eat my own dog food"
}
 
trait ExclamatoryGreeter extends Friendly {
  override def greet() = super.greet() + "!"
}
 
var pet: Friendly = new Dog
println(pet.greet())
 
pet = new HungryCat
println(pet.greet())
 
pet = new HungryDog
println(pet.greet())
 
pet = new Dog with ExclamatoryGreeter
println(pet.greet())
 
pet = new HungryCat with ExclamatoryGreeter
println(pet.greet())
 
pet = new HungryDog with ExclamatoryGreeter
println(pet.greet())

Mixin mit Nutzen

Demonstriert Mixins. Das Mixin Ord enthält Implementationen und Deklarationen. Eine Klasse, welches das Mixin implementiert, muss nur eine Methode implementieren und hat die anderen gratis.

trait Ord {
  def < (that: Any): Boolean
  def <=(that: Any): Boolean = (this < that) || (this == that)
  def > (that: Any): Boolean = !(this <= that)
  def >=(that: Any): Boolean = !(this < that)
}
 
class Person extends Ord { 
  var age = 0
  def < (that: Any): Boolean = {
    var p: Person = that.asInstanceOf[Person];
    if(p.age > this.age)
      true
    else
      false
  }
}
 
var simone: Person = new Person
var steff: Person = new Person
simone.age = 17
steff.age = 16
println(simone > steff) 
println(steff >= simone)

Resourcen