Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 ·...

31
Under the Hood of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

-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

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

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

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...

Page 31: Under the Hood of Scala Implicitsdownloads.typesafe.com/website/presentations... · 2015-03-26 · of Scala Implicits by Alexander Podkhalyuzin ScalaDays San Francisco 2015. Agenda

Thank you!

twitter: Safelaemail: [email protected]