programming_languages:scala

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
programmiersprachen:scala [2015/10/10 16:40] – angelegt phreazerprogramming_languages:scala [2017/02/19 15:46] (current) – ↷ Seite von programmiersprachen:scala nach programming_languages:scala verschoben phreazer
Line 1: Line 1:
-Scala+====== Scala ====== 
 + 
 + 
 +**Sca**lable **la**nguage 
 + 
 +===== Start =====
  
-====== Start ====== 
  
   * No primitive types, everything is an object   * 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
  
  
-===== Hello World =====+==== Hello World ====
 <code scala> <code scala>
-    object HelloWorld { +object HelloWorld { 
-      def main(args: Array[String]) { +  def main(args: Array[String]) { 
-        println("Hello, world!"+    println("Hello, world!"
-      +  } 
-<code> +
-    }+</code> 
 + 
 +===== Control structures ===== 
 + 
 +if/else-Constructs have a return value 
 + 
 +==== for loop ==== 
 +<code scala> 
 +for (i <- 1 to 10) println(i) 
 +</code> 
 + 
 + 
 +<code scala> 
 + 
 +// different steps 
 +for (i <- 4 to (17,3)) println(i) 
 +// other data types than int 
 +for (s <- myList) println(s) 
 + 
 +// multiple generators 
 +for (i <- 0 to 2; j <- 3 to 4) println(i+" "+j) 
 + 
 +// filter 
 +    for {i <- 1 to 6          
 +         j <- 1 to 6 
 +         if (i % 2 == 0) 
 +         if (j % 2 != 0)println(i+" "+j) 
 +// yield 
 +    val list: List[Int] = List(1,2,3,4) 
 +    val result = for (i <- list) yield i * 2 
 +    println(result) 
 +</code> 
 +==== match ==== 
 +Implizites break 
 + 
 +<code scala> 
 +val iValue = 3 
 +iValue match{ 
 +  case 1 => println("one"
 +  case _ => println("empty"
 +
 +</code> 
 + 
 +===== Singleton objects ===== 
 + 
 +There are no static methods/vars in scala and singleton objects can't be constructed using a constructor with new. 
 +<code scala> 
 +object ScalaObject { 
 + 
 +  val b = 5 
 +   
 +  def test(a: Double) : Double = { 
 +    3.* + b  
 +  } 
 +
 +</code> 
 + 
 +===== Companion object ===== 
 + 
 +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. 
 + 
 +===== Case classes ===== 
 + 
 +  * 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 
 + 
 +<code scala> 
 +case class Person (firstName : String, lastName : String, age : Int){ 
 +  def isAdult : Boolean = if (age >= 18) true else false 
 +
 +</code> 
 + 
 +===== Inheritance ===== 
 + 
 +Scala doesn't support 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. 
 + 
 + 
 + 
 +===== Identity and equality ===== 
 + 
 +==-Operator checks equality (identity in Java) 
 +eq-Operator checks identity 
 + 
 +eq and ne are only defined for clases of type scala.AnyRef. 
 + 
 + 
 + 
 +===== Learning sources ===== 
 + 
 +http://www.scalatutorial.de/topic143.html
  • programming_languages/scala.1444488009.txt.gz
  • Last modified: 2015/10/10 16:40
  • by phreazer