Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 ·...
Transcript of Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 ·...
![Page 1: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/1.jpg)
Under the Hood of Scala Implicits by Alexander PodkhalyuzinScalaDays San Francisco 2015
![Page 2: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/2.jpg)
Agenda● How implicits work● How IDE can help with them● Possibilities to improve performance of
compilation and IDE responsiveness
![Page 3: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/3.jpg)
Implicits: The Beginning
Implicits
Conversions
Parameters
![Page 4: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/4.jpg)
Implicits: Conversions
Implicits
Conversions
Parameters
Type conversions Extension methods
![Page 5: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/5.jpg)
Type conversions
Compiler will search implicits of type B => A.
implicit def b2a(a: B): A = new Aclass A; class Bval a: A = new B
![Page 6: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/6.jpg)
Search scopes1. Resolve scope
a. Declared implicitsb. Imported implicitsc. Implicitly imported implicits
2. Extended scopea. Companion objects of parts? of B => A type.
![Page 7: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/7.jpg)
If few implicits were found...class Base; object Base { implicit def clazz2str(c: Clazz): String = ""}class Clazz extends Base; object Clazz { implicit def base2str(b: Base): String = ""}val c: String = new Clazz
![Page 8: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/8.jpg)
PerformanceIf all implicits are in the resolve scope1. Harder to debug problems, when wrong
implicit is chosen2. Complexity to find right one
![Page 9: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/9.jpg)
Performance
![Page 10: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/10.jpg)
PerformanceSolution:1. Use “Extended scope” to encapsulate
implicits as much as possible2. Try to split implicits by priority
![Page 11: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/11.jpg)
Explicit return typeclass A; class Bdef toInt(x: B): Int = 123def goo(x: A) = toInt(new A)implicit def a2b(x: A) = new B
![Page 12: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/12.jpg)
Extension methodsWe can add new methods to existing types
class Typeclass TypeExt { def foo(x: Int) = 123}implicit def extType(a: Type): TypeExt = new TypeExt(new Type).foo(1)
![Page 13: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/13.jpg)
Algorithm...1. Find method without implicit conversion.2. All are not applicable
a. Find such conversions, which add applicable methodb. Choose most specific conversion of them
3. If in (1) we found something not applicable, then we can’t use implicits for args
4. We can’t choose most specific method among methods coming from different implicit conversions
![Page 14: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/14.jpg)
Implicit classesFor most cases we want implicit classes:
implicit class Foo(s: String) { def intValue: Int = s.toInt}
![Page 15: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/15.jpg)
Magnet pattern● Widely used in Spray routes● Originally was described in spray.io blog
![Page 16: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/16.jpg)
Parameter Info problemWith magnet pattern “Parameter Info” stops working in IDE, isn’t it?..
![Page 17: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/17.jpg)
Implicit parametersCompiler is able to fill missing implicit parameter list on the call site:
implicit val x: Int = 1def foo(implicit x: Int) = x + 1foo
![Page 18: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/18.jpg)
RecursivenessThe main feature, which gives us a step towards type-level programming:
class Ordering[T]implicit val intOrdering: Ordering[Int] = nullimplicit def listOrdering[T]( implicit t: Ordering[T] ): Ordering[List[T]] = null
![Page 19: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/19.jpg)
How it works?Implicit parameter search is completely the same as Implicit conversion search, just we will search not only function types.
![Page 20: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/20.jpg)
SOE...
Compiler is able to stop recursion. How?
implicit def a(implicit s: String): Int = 1implicit def b(implicit i: Int): String = "1"
![Page 21: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/21.jpg)
Another exampleimplicit def a[T](implicit t: T): List[T] = ???implicit def b[T](implicit t: T): Option[T] = ???implicit val s: String = ???def foo(implicit l: List[Option[List[String]]]) {} foo //foo(a(b(a(s))))
![Page 22: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/22.jpg)
Complexity of typeNumber of different parts of type:List[Int] complexity is 2Seq[Int, Option[String]] complexity is 4
Top level classes are List and Seq.
![Page 23: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/23.jpg)
Complexity of typeTo avoid SOE:● Have list of types to search● Do not add new search for some type if
○ List contains equivalent type○ List contains type with same top level class and
complexity of new type is bigger
![Page 24: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/24.jpg)
Local type inferenceIt’s not legal in Scala:
def foo[T](x: T, y: T => String) = y(x)foo("text", s => s + s)
![Page 25: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/25.jpg)
Local type inferenceScala compiler tries to solve type parameters for every single parameter list. Solution:
def foo[T](x: T)(y: T => String) = y(x)foo("text")(s => s + s)
![Page 26: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/26.jpg)
Type inference for implicitsSo is it the same for implicit parameters?
![Page 27: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/27.jpg)
-Xprint:typerIt’s good when everything is ok.
-Xprint:typer can help you to see implicits.
![Page 28: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/28.jpg)
Implicit conversionsIn IntelliJ IDEA you can use currently available tool to analyze available implicit conversions.
![Page 29: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/29.jpg)
Implicit parametersIn IntelliJ IDEA● Full analysis even in bad cases● It’s not including implicit conversions
![Page 30: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda](https://reader033.fdocuments.in/reader033/viewer/2022050606/5fadd45bf03b011a073967e4/html5/thumbnails/30.jpg)
What’s next● Use implicits as it’s most important feature of
Scala language● Now you know almost everything about
implicits, so use it safely● Choose right tools...