Successfully reported this slideshow.

Implicits Inspected and Explained

0

Share

Upcoming SlideShare
Datatype in JavaScript
Datatype in JavaScript
Loading in …3
×
1 of 123
1 of 123

Implicits Inspected and Explained

0

Share

Download to read offline

My presentation at ScalaDays 2016 NY on 11-05-2016.
Included are the REPLesent slides with the example code.

My presentation at ScalaDays 2016 NY on 11-05-2016.
Included are the REPLesent slides with the example code.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Implicits Inspected and Explained

  1. 1. Implicits Inspected and Explained ScalaDays 2016 New York Tim Soethout http://blog.timmybankers.nl
  2. 2. Implicits Inspected and Explained ScalaDays 2016 New York Tim Soethout Outline: Introduction - Implicits - Resolving - Type Classes - Wrap up http://blog.timmybankers.nl
  3. 3. About myself Tim Soethout Functional programmer at heart Scala/FP evangelist/trainer inside ING Bank PhD Candidate
  4. 4. What? Use values without explicit reference OO: is a + has a Implicits add: is viewable as a Loose Coupling, Tight Cohesion
  5. 5. Examples Akka Futures trait ScalaActorRef def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit = ... } actorRef ! SomeMessage("text") object Future { def apply[T](body: =>T)(implicit executor: ExecutionContext): Future[T] = ... } Future { doExpensiveComputation() }
  6. 6. Examples (2) Collections trait TraversableOnce[+A] { def sum[B >: A](implicit num: Numeric[B]): B = ... } List(1,2,3).sum res0: Int = 6
  7. 7. Examples (3) Finagle @implicitNotFound("Builder is not fully configured: Cluster: ${HasCluster}, Codec: ${HasCodec}, HostConnectionLimit: ${HasHostConnectionLimit}") private[builder] trait ClientConfigEvidence[HasCluster, HasCodec, HasHostConnectionLimit] class ClientBuilder[Req, Rep, HasCluster, HasCodec, HasHostConnectionLimit] private[finagle](...) { def build()( implicit THE_BUILDER_IS_NOT_FULLY_SPECIFIED_SEE_ClientBuilder_DOCUMENTATION: ClientConfigEvidence[HasCluster, HasCodec, HasHostConnectionLimit] ): Service[Req, Rep] = ... } val builder: ClientBuilder[Request, Response, Yes, Yes, Nothing] = ClientBuilder() .codec(Http()) .hosts("twitter.com:80") builder.build() Error:(24, 15) Builder is not fully configured: Cluster: com.twitter.finagle.builder.ClientConfig.Yes, Codec: com.twitter.finagle.builder.ClientConfig.Yes, HostConnectionLimit: Nothing builder.build() ^
  8. 8. Implicits enable DSLs Type evidence Reduce verbosity Type classes Dependency Injection at Compile time Extending libraries
  9. 9. But beware Resolution rules can be difficult Automatic conversions Do not overuse
  10. 10. Demo Implicit conversions (a.k.a. Implicit views) Implicit parameters Implicit classes Implicit declarations implicit def a2B(a : A) : B = ... def method(implicit x : Int) = ... implicit class X(y: Int) implicit val x = ...
  11. 11. Scoping Odersky Explains Lookup precedence: 1. By name only, without any prefix 2. In "implicit scope": companion/package object of the source type its parameters + supertype and supertraits
  12. 12. Demo Scoping and resolving
  13. 13. Typeclass uses Ad-hoc polymorphism Extension of libraries trait Numeric[T] extends Ordering[T] { def plus(x: T, y: T): T def minus(x: T, y: T): T def times(x: T, y: T): T ... }
  14. 14. Demo Typeclass for JSON Serialisation Naive with subtyping Typeclass + improvements
  15. 15. Recap Implicits are powerful Be careful with conversions Implicit precedence: first look local, then in companion/package object Typeclasses to extend libraries
  16. 16. Recap Implicits are powerful Be careful with conversions Implicit precedence: first look local, then in companion/package object Typeclasses to extend libraries Questions?
  17. 17. References / Scala documentation: Book: Blog , special thanks to @Ichoran and @som-snytt , for the demo slides Slides Code Java Converters Finding Implicits Scala In Depth Effective Scala All Things Runnable Scala Gitter channel REPLesent

×