val name: String = "Scala" var count: Int = 0 val x = 42 // Int inferred Int, Long, Double, Float, Boolean, Char, String val t = (1, "hello", true)
t._1 // 1 val opt: Option[Int] = Some(42)
val none: Option[Int] = None val list = List(1, 2, 3) val map = Map("a" -> 1, "b" -> 2) s"Hello, $name!"
s"1 + 1 = ${1 + 1}" raw"Line1\nLine2" // no escape """|
|Multi
|Line
|""".stripMargin f"$name%s is $age%d years old" val result = if (x > 0) "positive" else "non-positive" if (x > 0) {
"positive"
} else if (x < 0) {
"negative"
} else {
"zero"
} x match {
case 1 => "one"
case 2 => "two"
case _ => "other"
} x match {
case n if n > 0 => "positive"
case _ => "non-positive"
} obj match {
case s: String => s.length
case i: Int => i
case _ => 0
} for (i <- 1 to 5) println(i) val doubled = for (i <- 1 to 5) yield i * 2 for (i <- 1 to 10 if i % 2 == 0) yield i for {
i <- 1 to 3
j <- 1 to 3
} yield (i, j) while (condition) {
// code
} list.foreach(println) def add(a: Int, b: Int): Int = a + b def greet(name: String): String = {
val greeting = s"Hello, $name"
greeting
} def greet(name: String = "World"): String = s"Hello, $name" greet(name = "Scala") def sum(nums: Int*): Int = nums.sum val add = (a: Int, b: Int) => a + b def add(a: Int)(b: Int) = a + b
val addFive = add(5)_ def apply(f: Int => Int, x: Int) = f(x) class Person(val name: String, var age: Int) class Person(val name: String) {
def greet(): String = s"Hello, $name"
} class Person(val name: String) {
def this() = this("Unknown")
} class Student(name: String, val grade: Int)
extends Person(name) abstract class Animal {
def speak(): String
} object Logger {
def log(msg: String): Unit = println(msg)
} class Person(val name: String)
object Person {
def apply(name: String) = new Person(name)
} case class Person(name: String, age: Int) val p2 = person.copy(age = 30) sealed trait Shape
case class Circle(r: Double) extends Shape
case class Rectangle(w: Double, h: Double) extends Shape trait Greeting {
def greet(): String
} trait Greeting {
def greet(): String = "Hello"
} class Person extends Greeting with Comparable trait UserService { this: DatabaseService =>
// can use database methods
} List(1, 2, 3).map(_ * 2) // List(2, 4, 6) List(1, 2, 3, 4).filter(_ % 2 == 0) // List(2, 4) List(1, 2).flatMap(x => List(x, x * 2)) List(1, 2, 3).foldLeft(0)(_ + _) // 6 List(1, 2, 3).reduce(_ + _) // 6 List(1, 2, 3, 4).groupBy(_ % 2) List(1, 2).zip(List("a", "b")) // List((1,"a"), (2,"b")) List(1, 2, 3, 4).partition(_ % 2 == 0) List(1, 2, 3, 4).take(2) // List(1, 2)
List(1, 2, 3, 4).drop(2) // List(3, 4) opt.getOrElse("default") opt.map(_.toUpperCase) opt.flatMap(s => Some(s.length)) for {
a <- optA
b <- optB
} yield a + b opt match {
case Some(v) => v
case None => "default"
} def greet(name: String)(implicit lang: String) =
s"Hello in $lang: $name" implicit val defaultLang: String = "English" implicit def intToString(i: Int): String = i.toString implicit class RichInt(val i: Int) extends AnyVal {
def squared: Int = i * i
} def greet(name: String)(using lang: String) = ... given defaultLang: String = "English"