programming_languages:scala

This is an old revision of the document!


Scala

Scalable langa

Start

  • No primitive types, everything is an object
  • No semicolon
  • Object is a singleton
  • Methods can be defined private or protected. If no modifer is given, method is public.
  • No static methods
object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

if/else-Constructs have a return value

for (i <- 1 to 10) println(i)

Implizites break

val iValue = 3
iValue match{
  case 1 => println("one")
  case _ => println("empty")
}

There are no static methods/vars in scala and singleton objects can't be constructed using a constructor with new.

object ScalaObject {
 
  val b = 5
 
  def test(a: Double) : Double = {
    3.* + b 
  }
}

Prerequisite: object and class have the same name and are defined in the same file. Companion objects can access their private variables. Singleton objects without companion clases are standalone objects.

  • Use a factory method for construction
  • All passed parameters are a val-variable of the class
  • Can be used for pattern matching
  • Implement toString, hashCode, equals and have a copy method
case class Person (firstName : String, lastName : String, age : Int){
  def isAdult : Boolean = if (age >= 18) true else false
}

Scala don't supports multiple inheritance.

class B(arg:Int) extends A {}

Functions can be overridden with overide before func definition.

Overridding classes, functions or variables can be forbidden with prefix final.

Abstract classes

Difference to Traits: Abstract classes can have constructors. A class which inherits from an abstract class can't inherit from another.

The usage of override is optional.

Packages object

Objects which are visible in a whole package.

  • programming_languages/scala.1444491057.txt.gz
  • Last modified: 2015/10/10 17:30
  • by phreazer