The document discusses Scala implicits, including how they work, how IDEs can help with them, and possibilities to improve their performance. It covers implicit conversions, extension methods, implicit parameters, and how the compiler handles implicit resolution and type inference. The presenter advocates using implicits as a key Scala feature while also choosing tools that help debug issues and analyze available implicit conversions.
The document discusses Scala implicits, including how they work, how IDEs can help with them, and possibilities to improve their performance. It covers implicit conversions, extension methods, implicit parameters, and how the compiler handles implicit resolution and type inference. The presenter advocates using implicits as a key Scala feature while also choosing tools that help debug issues and analyze available implicit conversions.
This document discusses how implicits work in Scala, including how implicit conversions, parameters, and classes are resolved by the compiler. It explains how the compiler searches for implicits, potentially including extended implicit scopes, and how it handles recursion and complexity to avoid stack overflow errors. It also discusses how IDEs can help with implicits by analyzing available conversions and parameter types.
This document discusses transitioning a team from Java to Scala. It covers reasons for choosing Scala like its syntax and libraries. It also discusses challenges like testing and shared state in Scala. It provides recommendations to incrementally introduce Scala to avoid issues and constantly learn the new language through projects.
7. Case classes
case class A(x: Int)
于仂仄舒亳亠从亳 亟仂弍舒于仍磳 property 亟仍
仗舒舒仄亠仂于, 舒 舒从亢亠 仄亠仂亟:
equals, hashCode, toString
亠仂亟 companion object:
apply, unapply
8. By name parameters
亠 仂亟亳仆 亳仆舒从亳亠从亳亶 舒舒:
def foo(x: => Int) = {
print(x)
print(x)
}
foo(1)
9. By name parameters
舒 舒仄仂仄 亟亠仍亠 仂:
def foo(x: () => Int) = {
print(x())
print(x())
}
foo(() => 1)
仂亢仆仂 仍亳 仂弍磦仍 case class?
10. Default parameters
仍 仆亠从仂仂 仗舒舒仄亠仂于 仄仂亢仆仂
仂仗亠亟亠仍亳 亟亠仂仍仆仂亠 亰仆舒亠仆亳亠:
def foo(x: Int = 1, y: Int = 2) = x + y
foo(1)
11. Default parameters
丼仂 仗仂仍亳 亰亟亠?
class C {
def sum(x: Int = 1, y: Int = 2): Int = x + y
}
class D extends C {
override def sum(y: Int = 3, x: Int = 4): Int = super.sum(x, y)
}
val d: D = new D
val c: C = d
c.sum(x = 0)
d.sum(x = 0)
18. Functions shorthands
仂 仄仂仍舒仆亳 舒仆仂仆亳仄仆舒 仆从亳:
(x: Int) => x + 1
仂 亠仍亳 亠 仂亢亳亟舒亠仄仂亠 亰仆舒亠仆亳亠:
val fun: Int => Int = x => x + 1
仂亢仆仂 亰舒仗亳舒 亟舒亢亠 从仂仂亠:
val fun: Int => Int = _ + 1
19. Placeholder syntax
仂亢仆仂 亳仗仂仍亰仂于舒 仆亠从仂仍从仂 仗亠亠仄亠仆仆:
val sum: (Int, Int) => Int = _ + _
舒仗亳仄亠, 从舒从 仆亠从仂仍从仂 亳亰 仗舒舒仄亠仂于:
def foo(x: Int, y: Int, z: Int) = x + y + z
val z: (Int, Int) => Int = foo(_, y = 2, _)
z(1, 2)
仍亳 仂-仂 仂于亠仄 仄亟亠仆仂亠:
val x: (String, Int, Int) => String = _.substring(_, _)
20. Eta-expansion
亠亠 仂亟仆舒 于仂亰仄仂亢仆仂 仆舒仗亳舒
舒仆仂仆亳仄仆 仆从亳:
def inc(x: Int): Int = x + 1
val l: List[Int] = List(1, 2, 3)
l.map(x => inc(x))
l.map(inc(_))
l.map(inc _)
l map inc
21. Partial functions
弌仗亠亳舒仍仆亶 亳仆舒从亳:
val x: Int => Int = { case x => x + 1 }
val y: (Int, Int) => Int = {
case (x, y) => x + y
}
val f: PartialFunction[Any, String] = {
case i: Int => i.toString
}
22. Pattern matching
仂亠 仍舒亳 从舒从 舒亳亠仆仆亶 switch:
val x: Any
x match {
case 1 =>
case '1' =>
case "1" =>
case _ =>
}
23. Variables patterns
舒亠仆 于 于亳亟亠 亳亟亠仆亳亳从舒仂舒 仂
仂仆仂亶 弍从于 亰舒亟舒ム 仗亠亠仄亠仆仆:
"text" match {
case text =>
case text: String =>
case text@name =>
}
24. More...
仂亢仆仂 舒亳 从舒从 亞仂亟仆仂:
"scala.course@gmail.com" match {
case Email(name, domain) => //code
case _ =>
}
25. Unapply
object Email {
def unapply(s: String): Option[(String, String)] = {
val parts = s split '@'
if (parts.length == 2) {
Some(parts(0), parts(1))
} else None
}
}